nginx_配置文件说明
- 配置项介绍
红色字体的内容为基础配置,其他配置可以不写。
#运行用户
user www-data;
#启动进程,通常设置成和cpu的数量相等
#配置为2后进程管理器可以看到3个nginx进程
worker_processes 2;
#全局错误日志及PID文件
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
#工作模式及连接数上限
events {
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
use epoll;
#单个后台worker process进程的最大并发链接数
worker\_connections 1024;
\# multi\_accept on;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
\#设定mime类型,类型由mime.type文件定义
# include /etc/nginx/mime.types;
include /opt/nginx/nginx_install/conf/mime.types;
default\_type application/octet-stream;
\#设定日志格式
access_log /var/log/nginx/access.log;
#Nginx默认限制为1M,需要添加配置:
client_max_body_size 10m;
\#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
\#tcp\_nopush on;
\#连接超时时间
keepalive\_timeout 65;
tcp\_nodelay on;
\#开启gzip压缩
gzip on;
gzip\_disable "MSIE \[1-6\]\\.(?!.\*SV1)";
\#设定请求缓冲
client\_header\_buffer\_size 128k;
large\_client\_header\_buffers 4 512k;
include /etc/nginx/conf.d/\*.conf;
include /etc/nginx/sites-enabled/\*;
\#设定负载均衡的服务器列表
upstream openhb\_server\{
\#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
\}
#微商城
upstream gianttrip_server{
server 127.0.0.1:8081;
}
server {
#侦听80端口
listen 80;
\#当配置了多个server组,都监听80端口时,定义使用如下域名访问80端口时进此server,
server\_name gianttrip.cn www.gianttrip.cn;
\#设定本虚拟主机的访问日志
access\_log logs/www.xx.com.access.log main;
#默认请求
#即:当用户访问http://www.gianttrip.cn时,将跳到http://127.0.0.1:8081/gianttrip,因此在8081的tomcat中部署一个gianttrip.war包。
location / {
proxy\_pass\_header Server;
proxy\_set\_header Host $http\_host;
proxy\_redirect off;
proxy\_set\_header X-Real-IP $remote\_addr;
proxy\_set\_header X-Scheme $scheme;
proxy\_pass http://gianttrip\_server/gianttrip/;
\}
#即:当用户访问http://www.gianttrip.cn/openhb/xxx时,将跳到http://ip:port/openhb/xxx
location ^~ /openhb/ {
proxy_pass http://openhb\_server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
# 定义错误提示页面,当出现500等时,直接访问/opt/nginx/htmlerror/error.html
proxy_intercept_errors on;
error_page 500 502 503 504 404 /htmlerror/error.html;
location ^~ /htmlerror/ \{
root /opt/nginx;
\}
#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/htdocs;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}
#静态文件,当访问http://www.gianttrip.cn/roomZZpc/xxx.css时,
#将跳到当前机器的:/opt/view/roomZZpc/xxx.css
location ^~ /roomZZpc {
index index.html index.htm;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy\_redirect off;
proxy_set_header X-Scheme $scheme;
root /opt/view/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
include fastcgi_params;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus”;
auth_basic_user_file conf/htpasswd;
}
#禁止访问 .htxxx 文件
location ~ /\.ht {
deny all;
}
\}
}
- Location说明
~ #波浪线表示执行一个正则匹配,区分大小写
~* #表示执行一个正则匹配,不区分大小写
^~ #^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
= #进行普通字符精确匹配
@ #“@” 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
location 匹配的优先级(与location在配置文件中的顺序无关)
= 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
最后匹配理带有”~”和”~*“的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
如:
location = / {
# 只匹配”/“.
proxy_pass http://tomcat:8080/index
}
location / {
# 匹配任何请求,因为所有请求都是以”/“开始
# 但是更长字符匹配或者正则表达式匹配会优先匹配
[ configuration B ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
[ configuration C ]
}
location ~* .(gif|jpg|jpeg)$ {
# 匹配以 gif, jpg, or jpeg结尾的请求.
# 但是所有 /images/ 目录的请求将由 [Configuration C]处理.
[ configuration D ]
}
location ~* .(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
#防盗链
rewrite ^/ http://$host/logo.png;
}
}
location ~* .(txt|doc)${
#禁止访问某个目录
root /data/www/wwwroot/linuxtone/test;
deny all;
}
- ReWrite语法
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
1、下面是可以用来判断的表达式:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行
2、下面是可以用作判断的全局变量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
还没有评论,来说两句吧...