nginx之主配置文件解读

主配置文件解读

过滤conf文件的内容

1 grep -v "^#" nginx.conf | grep -v "^$"

nginx.conf主配置文件

 1 # 工作进程数 根据cpu 查看cpu信息 cat /proc/cpuinfo 或者top 再按下数字1看核数
 2 worker_processes  4;
 3 # 事件连接数
 4 events {
 5     worker_connections  1024;
 6 }
 7 http {
 8     # 关键字
 9     include       mime.types;
10     default_type  application/octet-stream;
11     # 定义日志格式化     客服端请求的相关信息 远程ip 用户等等
12     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
13                       '$status $body_bytes_sent "$http_referer" '
14                       '"$http_user_agent" "$http_x_forwarded_for"';
15     # 开启访问日志功能参数 指定日志路径
16     #access_log  logs/access.log  main;
17     sendfile        on;
18     #tcp_nopush     on;
19     #keepalive_timeout  0;
20     # 保持存活65s 保持一个长连接
21     keepalive_timeout  65;
22     # 支持图片 gif等等资源压缩之后在进行传输  节省网络带宽
23     gzip  on;
24     # server标签 控制着nginx的虚拟主机(web站点)
25     server {
26         # 定义nginx的入口端口80
27         listen       80;
28         # 填写域名或者ip地址
29         server_name  localhost;
30         # charset定义编码
31         charset utf8;
32         #access_log  logs/host.access.log  main;
33         # location参数定义网页的访问url
34         # 就代表 用户的请求是192.168.1.13/
35         location / {
36             # root定义网页根目录
37             root   html;
38             # index定义的网页首页文件,的名字
39             index  index.html index.htm;
40         }
41         # 定义客服端的错误页面
42         error_page  404              /404.html;
43         # redirect server error pages to the static page /50x.html
44         # 定义服务器的错误页面
45         error_page   500 502 503 504  /50x.html;
46         location = /50x.html {
47             root   html;
48         }
49         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
50         #
51         #location ~ \.php$ {
52         #    proxy_pass   http://127.0.0.1;
53         #}
54         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
55         #
56         #location ~ \.php$ {
57         #    root           html;
58         #    fastcgi_pass   127.0.0.1:9000;
59         #    fastcgi_index  index.php;
60         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
61         #    include        fastcgi_params;
62         #}
63         # deny access to .htaccess files, if Apache's document root
64         # concurs with nginx's one
65         #
66         #location ~ /\.ht {
67         #    deny  all;
68         #}
69     }
70     # another virtual host using mix of IP-, name-, and port-based configuration
71     #
72     #server {
73     #    listen       8000;
74     #    listen       somename:8080;
75     #    server_name  somename  alias  another.alias;
76     #    location / {
77     #        root   html;
78     #        index  index.html index.htm;
79     #    }
80     #}
81     # HTTPS server
82     #
83     #server {
84     #    listen       443 ssl;
85     #    server_name  localhost;
86     #    ssl_certificate      cert.pem;
87     #    ssl_certificate_key  cert.key;
88     #    ssl_session_cache    shared:SSL:1m;
89     #    ssl_session_timeout  5m;
90     #    ssl_ciphers  HIGH:!aNULL:!MD5;
91     #    ssl_prefer_server_ciphers  on;
92     #    location / {
93     #        root   html;
94     #        index  index.html index.htm;
95     #    }
96     #}
97 }
  1 #定义Nginx运行的用户和用户组
  2 user www www;
  3 
  4 #nginx进程数,建议设置为等于CPU总核心数。
  5 worker_processes 8;
  6  
  7 #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
  8 error_log /usr/local/nginx/logs/error.log info;
  9 
 10 #进程pid文件
 11 pid /usr/local/nginx/logs/nginx.pid;
 12 
 13 #指定进程可以打开的最大描述符:数目
 14 #工作模式与连接数上限
 15 #这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。
 16 #现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。
 17 #这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。
 18 worker_rlimit_nofile 65535;
 19 
 20 
 21 events
 22 {
 23     #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
 24     #是Linux 2.6以上版本内核中的高性能网络I/O模型,linux建议epoll,如果跑在FreeBSD上面,就用kqueue模型。
 25     #补充说明:
 26     #与apache相类,nginx针对不同的操作系统,有不同的事件模型
 27     #A)标准事件模型
 28     #Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
 29     #B)高效事件模型
 30     #Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
 31     #Epoll:使用于Linux内核2.6版本及以后的系统。
 32     #/dev/poll:使用于Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
 33     #Eventport:使用于Solaris 10。 为了防止出现内核崩溃的问题, 有必要安装安全补丁。
 34     use epoll;
 35 
 36     #单个进程最大连接数(最大连接数=连接数*进程数)
 37     #根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。
 38     worker_connections 65535;
 39 
 40     #keepalive超时时间。
 41     keepalive_timeout 60;
 42 
 43     #客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
 44     #分页大小可以用命令getconf PAGESIZE 取得。
 45     #[root@web001 ~]# getconf PAGESIZE
 46     #4096
 47     #但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。
 48     client_header_buffer_size 4k;
 49 
 50     #这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
 51     open_file_cache max=65535 inactive=60s;
 52 
 53     #这个是指多长时间检查一次缓存的有效信息。
 54     #语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.
 55     open_file_cache_valid 80s;
 56 
 57     #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。
 58     #语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location  这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache中总是打开状态.
 59     open_file_cache_min_uses 1;
 60     
 61     #语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.
 62     open_file_cache_errors on;
 63 }
 64  
 65  
 66  
 67 #设定http服务器,利用它的反向代理功能提供负载均衡支持
 68 http
 69 {
 70     #文件扩展名与文件类型映射表
 71     include mime.types;
 72 
 73     #默认文件类型
 74     default_type application/octet-stream;
 75 
 76     #默认编码
 77     #charset utf-8;
 78 
 79     #服务器名字的hash表大小
 80     #保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.
 81     server_names_hash_bucket_size 128;
 82 
 83     #客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。
 84     client_header_buffer_size 32k;
 85 
 86     #客户请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,如果header过大,它会使用large_client_header_buffers来读取。
 87     large_client_header_buffers 4 64k;
 88 
 89     #设定通过nginx上传文件的大小
 90     client_max_body_size 8m;
 91 
 92     #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
 93     #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
 94     sendfile on;
 95 
 96     #开启目录列表访问,合适下载服务器,默认关闭。
 97     autoindex on;
 98 
 99     #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
100     tcp_nopush on;
101      
102     tcp_nodelay on;
103 
104     #长连接超时时间,单位是秒
105     keepalive_timeout 120;
106 
107     #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
108     fastcgi_connect_timeout 300;
109     fastcgi_send_timeout 300;
110     fastcgi_read_timeout 300;
111     fastcgi_buffer_size 64k;
112     fastcgi_buffers 4 64k;
113     fastcgi_busy_buffers_size 128k;
114     fastcgi_temp_file_write_size 128k;
115 
116     #gzip模块设置
117     gzip on; #开启gzip压缩输出
118     gzip_min_length 1k;    #最小压缩文件大小
119     gzip_buffers 4 16k;    #压缩缓冲区
120     gzip_http_version 1.0;    #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
121     gzip_comp_level 2;    #压缩等级
122     gzip_types text/plain application/x-javascript text/css application/xml;    #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
123     gzip_vary on;
124 
125     #开启限制IP连接数的时候需要使用
126     #limit_zone crawler $binary_remote_addr 10m;
127 
128 
129 
130     #负载均衡配置
131     upstream jh.w3cschool.cn {
132      
133         #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
134         server 192.168.80.121:80 weight=3;
135         server 192.168.80.122:80 weight=2;
136         server 192.168.80.123:80 weight=3;
137 
138         #nginx的upstream目前支持4种方式的分配
139         #1、轮询(默认)
140         #每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
141         #2、weight
142         #指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
143         #例如:
144         #upstream bakend {
145         #    server 192.168.0.14 weight=10;
146         #    server 192.168.0.15 weight=10;
147         #}
148         #2、ip_hash
149         #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
150         #例如:
151         #upstream bakend {
152         #    ip_hash;
153         #    server 192.168.0.14:88;
154         #    server 192.168.0.15:80;
155         #}
156         #3、fair(第三方)
157         #按后端服务器的响应时间来分配请求,响应时间短的优先分配。
158         #upstream backend {
159         #    server server1;
160         #    server server2;
161         #    fair;
162         #}
163         #4、url_hash(第三方)
164         #按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
165         #例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
166         #upstream backend {
167         #    server squid1:3128;
168         #    server squid2:3128;
169         #    hash $request_uri;
170         #    hash_method crc32;
171         #}
172 
173         #tips:
174         #upstream bakend{#定义负载均衡设备的Ip及设备状态}{
175         #    ip_hash;
176         #    server 127.0.0.1:9090 down;
177         #    server 127.0.0.1:8080 weight=2;
178         #    server 127.0.0.1:6060;
179         #    server 127.0.0.1:7070 backup;
180         #}
181         #在需要使用负载均衡的server中增加 proxy_pass http://bakend/;
182 
183         #每个设备的状态设置为:
184         #1.down表示单前的server暂时不参与负载
185         #2.weight为weight越大,负载的权重就越大。
186         #3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
187         #4.fail_timeout:max_fails次失败后,暂停的时间。
188         #5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
189 
190         #nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
191         #client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
192         #client_body_temp_path设置记录文件的目录 可以设置最多3层目录
193         #location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
194     }
195      
196      
197      
198     #虚拟主机的配置
199     server
200     {
201         #监听端口
202         listen 80;
203 
204         #域名可以有多个,用空格隔开
205         server_name www.w3cschool.cn w3cschool.cn;
206         index index.html index.htm index.php;
207         root /data/www/w3cschool;
208 
209         #对******进行负载均衡
210         location ~ .*.(php|php5)?$
211         {
212             fastcgi_pass 127.0.0.1:9000;
213             fastcgi_index index.php;
214             include fastcgi.conf;
215         }
216          
217         #图片缓存时间设置
218         location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
219         {
220             expires 10d;
221         }
222          
223         #JS和CSS缓存时间设置
224         location ~ .*.(js|css)?$
225         {
226             expires 1h;
227         }
228          
229         #日志格式设定
230         #$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
231         #$remote_user:用来记录客户端用户名称;
232         #$time_local: 用来记录访问时间与时区;
233         #$request: 用来记录请求的url与http协议;
234         #$status: 用来记录请求状态;成功是200,
235         #$body_bytes_sent :记录发送给客户端文件主体内容大小;
236         #$http_referer:用来记录从那个页面链接访问过来的;
237         #$http_user_agent:记录客户浏览器的相关信息;
238         #通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
239         log_format access '$remote_addr - $remote_user [$time_local] "$request" '
240         '$status $body_bytes_sent "$http_referer" '
241         '"$http_user_agent" $http_x_forwarded_for';
242          
243         #定义本虚拟主机的访问日志
244         access_log  /usr/local/nginx/logs/host.access.log  main;
245         access_log  /usr/local/nginx/logs/host.access.404.log  log404;
246          
247         #对 "/" 启用反向代理
248         location / {
249             proxy_pass http://127.0.0.1:88;
250             proxy_redirect off;
251             proxy_set_header X-Real-IP $remote_addr;
252              
253             #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
254             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
255              
256             #以下是一些反向代理的配置,可选。
257             proxy_set_header Host $host;
258 
259             #允许客户端请求的最大单文件字节数
260             client_max_body_size 10m;
261 
262             #缓冲区代理缓冲用户端请求的最大字节数,
263             #如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常。如果注释该指令,使用默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,问题就出现了。
264             #无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,都返回500 Internal Server Error错误
265             client_body_buffer_size 128k;
266 
267             #表示使nginx阻止HTTP应答代码为400或者更高的应答。
268             proxy_intercept_errors on;
269 
270             #后端服务器连接的超时时间_发起握手等候响应超时时间
271             #nginx跟后端服务器连接超时时间(代理连接超时)
272             proxy_connect_timeout 90;
273 
274             #后端服务器数据回传时间(代理发送超时)
275             #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
276             proxy_send_timeout 90;
277 
278             #连接成功后,后端服务器响应时间(代理接收超时)
279             #连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
280             proxy_read_timeout 90;
281 
282             #设置代理服务器(nginx)保存用户头信息的缓冲区大小
283             #设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小
284             proxy_buffer_size 4k;
285 
286             #proxy_buffers缓冲区,网页平均在32k以下的设置
287             #设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k
288             proxy_buffers 4 32k;
289 
290             #高负荷下缓冲大小(proxy_buffers*2)
291             proxy_busy_buffers_size 64k;
292 
293             #设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长
294             #设定缓存文件夹大小,大于这个值,将从upstream服务器传
295             proxy_temp_file_write_size 64k;
296         }
297          
298          
299         #设定查看Nginx状态的地址
300         location /NginxStatus {
301             stub_status on;
302             access_log on;
303             auth_basic "NginxStatus";
304             auth_basic_user_file confpasswd;
305             #htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
306         }
307          
308         #本地动静分离反向代理配置
309         #所有jsp的页面均交由tomcat或resin处理
310         location ~ .(jsp|jspx|do)?$ {
311             proxy_set_header Host $host;
312             proxy_set_header X-Real-IP $remote_addr;
313             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
314             proxy_pass http://127.0.0.1:8080;
315         }
316          
317         #所有静态文件由nginx直接读取不经过tomcat或resin
318         location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|
319         pdf|xls|mp3|wma)$
320         {
321             expires 15d; 
322         }
323          
324         location ~ .*.(js|css)?$
325         {
326             expires 1h;
327         }
328     }
329 }
nginx.conf中文最全配置详情
1 CoreModule核心模块
2 
3 user www;                       #Nginx进程所使用的用户
4 worker_processes 1;             #Nginx运行的work进程数量(建议与CPU数量一致或auto)
5 error_log /log/nginx/error.log  #Nginx错误日志存放路径
6 pid /var/run/nginx.pid          #Nginx服务运行后产生的pid进程号
1 $remote_addr    记录客户端ip
2 $remote_user    远程用户,没有就是 “-3 $time_local    时间相关
4 $request     对应请求信息"GET /favicon.ico HTTP/1.1"
5 $status      状态码
6 $body_bytes_sent  571字节 请求体的大小
7 $http_referer  对应“-”  由于是直接输入浏览器就是 -
8 $http_user_agent  客户端身份信息
9 $http_x_forwarded_for  记录客户端的来源真实ip 192.168.1.13
日志参数详情
1 events事件模块
2 
3 events {            
4     worker_connections  //每个worker进程支持的最大连接数
5     use epool;          //事件驱动模型, epoll默认
6 }
 1 http内核模块
 2 
 3 //公共的配置定义在http{}
 4 http {  //http层开始
 5 ...    
 6     //使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
 7     'server' {
 8         listen       80;        //监听端口, 默认80
 9         server_name  localhost; //提供服务的域名或主机名
10         access_log host.access.log  //访问日志
11         //控制网站访问路径
12         'location' / {
13             root   /usr/share/nginx/html;   //存放网站代码路径
14             index  index.html index.htm;    //服务器返回的默认页面文件
15         }
16         //指定错误代码, 统一定义错误页面, 错误代码重定向到新的Locaiton
17         error_page   500 502 503 504  /50x.html;
18     }
19     ...
20     //第二个虚拟主机配置
21     'server' {
22     ...
23     }
24     
25     include /etc/nginx/conf.d/*.conf;  //包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件
26 
27 }   //http层结束

nginx虚拟主机

架构图:

 

虚拟主机就是将一台服务器分割成多个“虚拟服务器”,每个站点使用各自的硬盘空间,由于省资源,省钱,众多网站都使用虚拟主机来部署网站。 

虚拟主机的概念就是在web服务里的一个独立的网站站点,这个站点对应独立的域名(IP),具有独立的程序和资源目录,可以独立的对外提供服务。

这个独立的站点配置是在nginx.conf中使用server{}代码块标签来表示一个虚拟主机。

Nginx支持多个server{}标签,即支持多个虚拟主机站点。

配置多虚拟主机

 1 worker_processes  1;
 2 events {
 3     worker_connections  1024;
 4 }
 5 http {
 6     include       mime.types;
 7     default_type  application/octet-stream;
 8     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 9                       '$status $body_bytes_sent "$http_referer" '
10                       '"$http_user_agent" "$http_x_forwarded_for"';
11     access_log  logs/access.log  main;
12     sendfile        on;
13     keepalive_timeout  65;
14     server {
15         listen       80;
16         server_name  www.mchot1.cn;
17         location /{
18             root   html/mchot1;
19             index  index.html index.htm;
20         }
21 }
22     server {
23         listen       80;
24         server_name  www.mchot2.cn;
25         location /{
26             root   html/mchot2;
27             index  index.html index.htm;
28         }
29 }
30     }

配置信息配置完后测试:

1 ./sbin/nginx -t

平滑启动nginx

1 ./sbin/nginx -s reload

检查nginx端口,进程,访问虚拟站点

1 netstat -tunlp|grep nginx
2 
3 ps -ef|grep nginx
4 curl www.hot1.cn
5 curl www.hot2.cn

 nginx状态信息配置

1 Nginx状态信息(status)配置及信息详解
2     nginx与php-fpm一样内建了一个状态页,对于想了解nginx的状态以及监控nginx非常有帮助。为了后续的zabbix监控,我们需要先了解一下nginx的状态页。
3 
4 Nginx状态信息(status)介绍
5     Nginx软件在编译时又一个with-http_stub_status_module模块,这个模块功能是记录Nginx的基本访问状态信息,让使用者了解Nginx的工作状态。
6 要想使用状态模块,在编译时必须增加--with-http_stub_status_module参数。

 监测你的nginx是否安装了status模块

1 /opt/nginx/sbin/nginx -V
1 #在访问ip/status的时候,进入状态功能        
2 location /status {
3         #开启nginx状态功能
4              stub_status on;
5 }   
1 # 平滑启动nginx
2 ./sbin/nginx -s reload

访问status页面

192.168.1.13/status

还可以ab压测命令检测

1 yum -y install httpd-tools
2 
3 -n requests #执行的请求数,即一共发起多少请求。
4 
5 -c concurrency #请求并发数。
6 
7 -k #启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。
1 ab -kc 1000 -n 100000 http://192.168.1.13/

 nginx限制网站来源ip访问

1 限制ip或ip段访问
2 禁止访问/xx/底下的资源
3 
4 location /xx {
5 deny 122.71.240.254;
6 #alias /opt/nginx112/html/xx;
7 allow 10.1.1.0/16;  
8 }

错误页面也可以进行优化, 

配置error_page即可 

猜你喜欢

转载自www.cnblogs.com/Alexephor/p/11368355.html