【1】默认配置文件
安装完路径默认为:/usr/local/nginx/conf/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #user nobody; worker_processes 1;
#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
#pid logs/nginx.pid;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on;
server { listen 80; server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { root html; index index.html index.htm; }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}
# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
# another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias;
# location / { # root html; # index index.html index.htm; # } #}
# HTTPS server # #server { # listen 443 ssl; # server_name localhost;
# ssl_certificate cert.pem; # ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on;
# location / { # root html; # index index.html index.htm; # } #}
}
|
【2】配置文件说明
配置文件主要由四部分组成:main(全局设置),server(主机配置),upstream(负载均衡服务器设置)和location(URL匹配特定位置设置)。
① 全局变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| //Nginx用户及组:用户 组。window下不指定 #user nobody nobody; //工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。 worker_processes 4;
#error_log logs/error.log; #error_log logs/error.log notice; //日志文件与级别 #error_log logs/error.log info; //指定存储nginx主进程进程号码的文件路径; 文件 #pid logs/nginx.pid;
worker_rlimit_nofile 65535; /* 指定进程可以打开的最大描述符:数目。 这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除; 但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。 现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。 */
|
② 事件配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| events { use epoll; #use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] /*使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。 补充说明:与apache相比,nginx针对不同的操作系统,有不同的事件模型 A)标准事件模型 select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll B)高效事件模型 kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。 epoll:使用于Linux内核2.6版本及以后的系统。 /dev/poll:使用于Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。 eventport:使用于Solaris 10。 为了防止出现内核崩溃的问题, 有必要安装安全补丁。*/ worker_connections 1024; //每个工作进程的最大连接数量。理论值:worker_rlimit_nofile/worker_processes。 //理论上每台nginx服务器的最大连接数为:worker_processes*worker_connections //注意:最大客户数也由系统的可用socket连接数限制(~ 64K) }
|
③ http参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| http{ #文件扩展名与文件类型映射表 include mime.types; #默认文件类型 default_type application/octet-stream; #日志相关定义 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #定义日志的格式。后面定义要输出的内容。 #1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址; #2.$remote_user :用来记录客户端用户名称; #3.$time_local :用来记录访问时间与时区; #4.$request :用来记录请求的url与http协议; #5.$status :用来记录请求状态; #6.$body_bytes_sent :记录发送给客户端文件主体内容大小; #7.$http_referer :用来记录从那个页面链接访问过来的; #8.$http_user_agent :记录客户端浏览器的相关信息 #连接日志的路径,指定的日志格式放在最后。 #access_log logs/access.log main; #只记录更为严重的错误日志,减少IO压力 error_log logs/error.log crit; #关闭日志 #access_log off;
#默认编码 #charset utf-8; server_tokens off; #服务器名字的hash表大小 server_names_hash_bucket_size 128; #客户端请求单个文件的最大字节数,比如文件上传时,如果文件超过该参数限制,就会直接被挡回去 client_max_body_size 8m; #指定来自客户端请求头的hearerbuffer大小 client_header_buffer_size 32k; #指定客户端请求中较大的消息头的缓存最大数量和大小。 large_client_header_buffers 4 64k; #开启高效传输模式。sendfile 指向sendfile()函数。 #sendfile()在磁盘和TCP端口(或者任意两个文件描述符)之间复制数据。 #sendfile()直接从磁盘上读取数据到操作系统缓冲,因此会更有效率。 sendfile on; #防止网络阻塞:配置nginx在一个包中发送全部的头文件,而不是一个一个发送。 tcp_nopush on; #配置nginx不要缓存数据,快速发送小数据。 tcp_nodelay on; #客户端连接超时时间,单位是秒 keepalive_timeout 60; #客户端请求头读取超时时间 client_header_timeout 10; #设置客户端请求主体读取超时时间 client_body_timeout 10; #响应客户端超时时间 send_timeout 10;
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。 fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k;
#gzip模块设置 #开启gzip压缩输出 gzip on; #前端缓存服务器缓存经过压缩的页面 gzip_vary on; #为指定的客户端禁用 gzip 功能。 gzip_disable "msie6" #允许或禁止基于请求、响应的压缩。设置为any,就可以gzip所有的请求。 gzip_proxied any; #最小压缩文件大小 gzip_min_length 1k; #压缩缓冲区 gzip_buffers 4 16k; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0) gzip_http_version 1.0; #压缩等级 1-9 等级越高,压缩效果越好,节约宽带,但CPU消耗大 gzip_comp_level 2; #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
open_file_cache max=102400 inactive=20s; #这个指令指定缓存是否启用。 open_file_cache_errors on 语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误. open_file_cache_min_uses 语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache中总是打开状态. open_file_cache_valid 语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.
例: open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; }
|
④ 虚拟主机基本设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #虚拟主机定义 server { #监听端口 listen 80; #访问域名 #域名可以有多个,用空格隔开 server_name localhost; #编码格式,若网页格式与此不同,将被自动转码 #charset koi8-r; #虚拟主机访问日志定义 #access_log logs/host.access.log main; #对URL进行匹配 location / { #访问路径,可相对也可绝对路径 //设置web资源路径映射;用于指明用户请求的url所对应的本地文件系统上的文档所在目录路径; root html; #首页文件。以下按顺序匹配 index index.html index.htm; }
#错误信息返回页面 #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
#访问URL以.php结尾则自动转交给127.0.0.1 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#location ~ \.php$ { # proxy_pass http://127.0.0.1; #} #php脚本请求全部转发给FastCGI处理 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}
#禁止访问.ht页面 (需ngx_http_access_module模块) # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } #HTTPS虚拟主机定义 # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}
|
root path:
1 2 3 4 5 6 7
| 设置web资源路径映射;用于指明用户请求的url所对应的本地文件系统上的文档所在目录路径; 可用的位置:http, server, location, if in location 如: server_name www.magedu.com root /vhosts/www/htdocs/ 则: http://www.uilucky.com/index.html --> /vhosts/www/htdocs/index.html
|
location详细说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... }
在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射; ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置;
五种修饰说明如下:
① =:对URI做精确匹配;例如, http://www.magedu.com/, http://www.magedu.com/index.html location = / { ... } ② ~:对URI做正则表达式模式匹配,区分字符大小写; ③ ~*:对URI做正则表达式模式匹配,不区分字符大小写; ④ ^~:对URI的左半部分做匹配检查,不区分字符大小写; ⑤ 不带符号:匹配起始于此uri的所有的url;
匹配优先级:=, ^~, ~/~*,不带符号;
server { root /vhosts/www/htdocs/ location /admin/ { root /webapps/app1/data/ } }
|
alias path
定义路径别名,文档映射的另一种机制;仅能用于location上下文.
注意:location中使用root指令和alias指令的意义不同:
(a) root,给定的路径对应于location中的/uri/左侧的/;
|
(b) alias,给定的路径对应于location中的/uri/右侧的/
示例一:
1 2 3 4 5
| location /admin/{ root /opt/hh/app/spider/; } 则浏览器访问http://192.168.88.220:80/admin/index.jsp, 对应服务器路径为:/opt/hh/app/spider/admin/index.jsp
|
示例二:
1 2 3 4 5
| location /admin/{ alias /opt/hh/app/spider/; } 则浏览器访问http://192.168.88.220:80/admin/index.jsp, 对应服务器路径为:/opt/hh/app/spider/index.jsp
|
⑤ Nignx状态监控
1 2 3 4 5 6 7 8 9 10 11 12 13
| #Nginx运行状态,StubStatus模块获取Nginx自启动的工作状态(编译时要开启对应功能) #location /NginxStatus { # #启用StubStatus的工作访问状态 # stub_status on; # #指定StubStaus模块的访问日志文件 # access_log logs/Nginxstatus.log; # #Nginx认证机制(需Apache的htpasswd命令生成) # #auth_basic "NginxStatus"; # #用来认证的密码文件 # #auth_basic_user_file ../htpasswd; #} 访问:http://IP/NginxStatus(测试就不加密码验证相关)
|
⑥ 反向代理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #以下配置追加在HTTP的全局变量中
#nginx跟后端服务器连接超时时间(代理连接超时) proxy_connect_timeout 5; #后端服务器数据回传时间(代理发送超时) proxy_send_timeout 5; #连接成功后,后端服务器响应时间(代理接收超时) proxy_read_timeout 60; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffer_size 16k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_buffers 4 32k; #高负荷下缓冲大小(proxy_buffers*2) proxy_busy_buffers_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 proxy_temp_file_write_size 64k; #反向代理缓存目录 proxy_cache_path /data/proxy/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=1g; #levels=1:2 设置目录深度,第一层目录是1个字符,第2层是2个字符 #keys_zone:设置web缓存名称和内存缓存空间大小 #inactive:自动清除缓存文件时间。 #max_size:硬盘空间最大可使用值。 #指定临时缓存文件的存储路径(路径需和上面路径在同一分区) proxy_temp_path /data/proxy/temp
#服务配置 server { #侦听的80端口 listen 80; server_name localhost; location / { #反向代理缓存设置命令(proxy_cache zone|off,默认关闭所以要设置) proxy_cache cache_one; #对不同的状态码缓存不同时间 proxy_cache_valid 200 304 12h; #设置以什么样参数获取缓存文件名 proxy_cache_key $host$uri$is_args$args; #后7端的Web服务器可以通过X-Forwarded-For获取用户真实IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #代理设置 proxy_pass http://IP; #文件过期时间控制 expires 1d; } #配置手动清楚缓存(实现此功能需第三方模块 ngx_cache_purge) #http://www.123.com/2017/0316/17.html访问 #http://www.123.com/purge/2017/0316/17.html清楚URL缓存 location ~ /purge(/.*) { allow 127.0.0.1; deny all; proxy_cache_purge cache_one $host$1$is_args$args; } #设置扩展名以.jsp、.php、.jspx结尾的动态应用程序不做缓存 location ~.*\.(jsp|php|jspx)?$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://http://IP; }
|
⑦ 负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #负载均衡服务器池 upstream my_server_pool { #调度算法 #1.轮循(默认)(weight轮循权值) #2.ip_hash:根据每个请求访问IP的hash结果分配。(会话保持) #3.fair:根据后端服务器响应时间最短请求。(upstream_fair模块) #4.url_hash:根据访问的url的hash结果分配。(需hash软件包) #参数: #down:表示不参与负载均衡 #backup:备份服务器 #max_fails:允许最大请求错误次数 #fail_timeout:请求失败后暂停服务时间。 server 192.168.1.109:80 weight=1 max_fails=2 fail_timeout=30; server 192.168.1.108:80 weight=2 max_fails=2 fail_timeout=30; } #负载均衡调用 server { ... location / { proxy_pass http://my_server_pool; } }
|
⑧ URL重写
1 2 3 4 5 6 7 8 9 10 11 12
| #根据不同的浏览器URL重写 if($http_user_agent ~ Firefox){ rewrite ^(.*)$ /firefox/$1 break; } if($http_user_agent ~ MSIE){ rewrite ^(.*)$ /msie/$1 break; }
#实现域名跳转 location / { rewrite ^/(.*)$ https://web8.example.com$1 permanent; }
|
⑨ IP限制
1 2 3 4 5 6 7
| #限制IP访问 location / { deny 192.168.0.2; allow 192.168.0.0/24; allow 192.168.1.1; deny all; }
|