Nginx-faq handling

1 problem
This case requires proper optimization of the Nginx server to solve the following problems to improve the processing performance of the server:
  • How to customize the 404 error page returned to the client
  • How to view server status information
  • How to solve if the client accesses the server prompt "Too many open files"
  • How to solve the problem that the client access header information is too long
  • How to make the client browser cache data
  • Client access to this Web server to verify the effect:
  • Use ab stress testing software to test concurrency
  • Write a test script to generate an access request for long header information
  • The client visits a page that does not exist, and tests whether the 404 error page is redirected
2 steps

To implement this case, you need to follow the steps below.
Step 1: Customize the error page

1) Before optimization, when the client uses a browser to access a page that does not exist, it will prompt 404 file not found

[root@client ~]# firefox http://192.168.4.5/xxxxx        //访问一个不存在的页面

2) Modify the Nginx configuration file and customize the error page

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
        charset utf-8;                    //仅在需要中文时修改该选项
error_page   404  /404.html;    //自定义错误页面
.. ..
[root@proxy ~]# vim /usr/local/nginx/html/404.html        //生成错误页面
Oops,No NO no page …
[root@proxy ~]# nginx -s reload

#Please make sure that nginx is started, otherwise an error will be reported when you run this command. The error message is as follows:
#[error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory )

3) After optimization, the client will use the browser to visit a page that does not exist, and it will prompt the 40x.html page defined by itself

[root@client ~]# firefox http://192.168.4.5/xxxxx        //访问一个不存在的页面

4) Common http status codes
Common http status codes can be used as shown in Table-1.
Table-1 Host list
Insert picture description here
Step 2: How to view server status information (very important function)

1) Use –with-http_stub_status_module to open the status page module when compiling and installing

[root@proxy ~]# tar  -zxvf   nginx-1.12.2.tar.gz
[root@proxy ~]# cd  nginx-1.12.2
[root@proxy nginx-1.12.2]# ./configure   \
> --with-http_ssl_module                        //开启SSL加密功能
> --with-stream                                //开启TCP/UDP代理模块
> --with-http_stub_status_module                //开启status状态页面
[root@proxy nginx-1.12.2]# make && make install    //编译并安装

2) Enable Nginx service and view the listening port state
ss command to view port information system boot, the command commonly used options are as follows:
-a displays information about all ports
-n port number is displayed in a digital format
-t display port TCP connection
- u Display the port of the UDP connection
-l Display the port information that the service is monitoring. For example, after httpd starts, it will always listen to port 80
-p Display the service name of the listening port (that is, the program name)
Note: It can be used in RHEL7 system The ss command replaces the netstat command, with the same function and the same options.

[root@proxy ~]# /usr/local/nginx/sbin/nginx
[root@proxy ~]# netstat  -anptu  |  grep nginx
tcp        0        0 0.0.0.0:80        0.0.0.0:*        LISTEN        10441/nginx
[root@proxy ~]# ss  -anptu  |  grep nginx

3) Modify the Nginx configuration file and define the status page

[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
… …
location /status {
    
    
                stub_status on;
                 #allow IP地址;
                 #deny IP地址;
        }
… …
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

4) After optimization, check the status page information

[root@proxy ~]# curl  http://192.168.4.5/status
Active connections: 1 
server accepts handled requests
 10 10 3 
Reading: 0 Writing: 1 Waiting: 0
Active connections:当前活动的连接数量。
Accepts:已经接受客户端的连接总数量。
Handled:已经处理客户端的连接总数量。
(一般与accepts一致,除非服务器限制了连接数量)。
Requests:客户端发送的请求数量。
Reading:当前服务器正在读取客户端请求头的数量。
Writing:当前服务器正在写响应信息的数量。
Waiting:当前多少客户端在等待服务器的响应。

Step 3: Optimize Nginx concurrency

1) Use ab high concurrency test before optimization

[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/     //-n 人数 -c 次数
Benchmarking 192.168.4.5 (be patient)
socket: Too many open files (24)                //提示打开文件数量过多

2) Modify the Nginx configuration file to increase the amount of concurrency

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
worker_processes  2;                    //与CPU核心数量一致
events {
    
    
worker_connections 65535;        //每个worker最大并发连接数
}
.. ..
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

3) Optimize Linux kernel parameters (maximum number of files)

[root@proxy ~]# ulimit -a                        //查看所有属性值
[root@proxy ~]# ulimit -Hn 100000                //设置硬限制(临时规则)
[root@proxy ~]# ulimit -Sn 100000                //设置软限制(临时规则)
[root@proxy ~]# vim /etc/security/limits.conf
    .. ..
*               soft    nofile            100000
*               hard    nofile            100000
#该配置文件分4列,分别如下:
#用户或组    硬限制或软限制    需要限制的项目   限制的值

4) Test the server concurrency after optimization (because the client did not adjust the kernel parameters, it was tested in proxy)

[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/

Step 4: Optimize Nginx packet header cache

1) Before optimization, use the script to test whether the long header request can get a response

[root@proxy ~]# cat lnmp_soft/buffer.sh 
#!/bin/bash
URL=http://192.168.4.5/index.html?
for i in {
    
    1..5000}
do
    URL=${URL}v$i=$i
done
curl $URL                                //经过5000次循环后,生成一个长的URL地址栏
[root@proxy ~]# ./buffer.sh
.. ..
<center><h1>414 Request-URI Too Large</h1></center>        //提示头部信息过大

2) Modify the Nginx configuration file to increase the size of the packet header cache

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
    
    
client_header_buffer_size    1k;        //默认请求包头信息的缓存    
large_client_header_buffers  4 4k;        //大请求包头部信息的缓存个数与容量
.. ..
}
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

3) After optimization, use the script to test whether the long header request can get a response

[root@proxy ~]# cat buffer.sh 
#!/bin/bash
URL=http://192.168.4.5/index.html?
for i in {
    
    1..5000}
do
    URL=${URL}v$i=$i
done
curl $URL
[root@proxy ~]# ./buffer.sh

Step 5: The browser caches static data locally

1) Use the Firefox browser to view the cache.
Take Firefox as an example. Entering about:cache in the Firefox address bar will display the cache information of the Firefox browser, as shown in Figure-3. Click List Cache Entries to view the detailed information.
Insert picture description here
Figure-3
2) Clear firefox local cache data, as shown in Figure-4.

Insert picture description here
Figure-4
3) Modify the Nginx configuration file to define the cache time for static pages

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
server {
    
    
        listen       80;
        server_name  localhost;
        location / {
    
    
            root   html;
            index  index.html index.htm;
        }
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
    
    
expires        30d;            //定义客户端缓存时间为30天
}
}
[root@proxy ~]# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

#Please make sure that nginx is started, otherwise an error will be reported when you run this command. The error message is as follows:
#[error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory )
4) After optimization, use the Firefox browser to access the picture and check the cache information again

[root@client ~]# firefox http://192.168.4.5/day.jpg

Enter about:cache in the firefox address bar, check the local cache data, check whether there are pictures and whether the expiration time is correct

Guess you like

Origin blog.csdn.net/weixin_45942735/article/details/104567504