Five common application scenarios of Nginx, please bookmark Linux operation and maintenance~

Insert picture description here

Nginx is a very powerful high-performance Web and reverse proxy service. It has many very superior features. In the case of high concurrency connections, Nginx is a good alternative to Apache services. Its characteristics are that it occupies less memory and has strong concurrency capabilities. In fact, nginx's concurrency capabilities perform better in the same type of web server. Therefore, well-known domestic manufacturers such as Taobao, Jingdong, Baidu, Sina, Netease, Tencent, etc. Use the Nginx website.

In our daily work and study, how will we optimize our Nginx server? How should we deal with the following problems?

1. Customize the 404 error page returned to the client

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

# 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;    //自定义错误页面
.. ..
# vim /usr/local/nginx/html/404.html  //生成错误页面
Oops,No NO no page …
# nginx -s reload
# 请先确保 nginx 是启动状态,否则运行该命令会报错,报错信息如下:
#[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

# firefox http://192.168.4.5/xxxxx //访问一个不存在的页面

Common http status codes can be shown in the reference table

Insert picture description here

Two, view server status information

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

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

2) Enable the Nginx service and view the status of the listening port

The ss command can view the information of the ports started in the system. Common options of this command are as follows:

-a displays all the information ports
-n port number is displayed in a digital format
-t display port TCP connection
-u display UDP port connected
-l displays the service is listening on port information, such as the httpd starts up, and listening on port 80
- p Shows what the service name of the listening port (that is, the program name) is.
Note: In RHEL7 system, you can use the ss command to replace the netstat command, with the same function and the same options.

# /usr/local/nginx/sbin/nginx
# netstat  -anptu  |  grep nginx
tcp        0        0 0.0.0.0:80        0.0.0.0:*        LISTEN        10441/nginx
# ss  -anptu  |  grep nginx

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

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

4) After optimization, check the status page information

# 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: The number of currently active connections.
Accepts: The total number of client connections that have been accepted.
Handled: The total number of client connections that have been processed.
(Generally consistent with accepts, unless the server limits the number of connections).
Requests: The number of requests sent by the client.
Reading: The current server is reading the number of client request headers.
Writing: The number of response messages currently being written by the server.
Waiting: How many clients are currently waiting for a response from the server.

3. Optimize Nginx concurrency

1) Use ab high concurrency test before optimization

# ab -n 2000 -c 2000 http://192.168.4.5/
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

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

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

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

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

# ab -n 2000 -c 2000 http://192.168.4.5/

Fourth, optimize Nginx packet header caching

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

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

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

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

Five, the browser caches static data locally

1) Use Firefox to view the cache

Taking the Firefox browser as an example, entering about:cache in the Firefox address bar will display the cache information of the Firefox browser, as shown in the figure, click List Cache Entries to view the detailed information.

Insert picture description here

2) Clear the firefox local cache data, as shown in the figure.

Insert picture description here

3) Change the Nginx configuration file to define the cache time for static pages

# 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天
}
}
# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html
# /usr/local/nginx/sbin/nginx -s reload
#请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:16.#[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

# 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.

Insert picture description here

Guess you like

Origin blog.csdn.net/liuxingjiaoyu/article/details/112674758