Nginx is a powerful, high-performance Web and reverse proxy service with many excellent features that make it a good alternative to Apache services in cases of high concurrency connections. It is characterized by less memory and strong concurrency. In fact, Nginx’s concurrency is better in the same type of web server. Therefore, well-known domestic big companies such as Taobao, JINGdong, Baidu, Sina, netease, Tencent and so on are using Nginx website.How should we optimize our Nginx server in our daily work and learning? How should we deal with the following problems?

1. Customize the 404 error page returned to the client

1) Before optimization, the client will use the browser to access the page that does not exist, prompting 404 file not found

# Firefox http://192.168.4.5/xxxxx // Visit a page that doesn't existCopy the code

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

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf.. . charset utf-8; // Change this option only if Chinese is required error_page 404/404.html; // Custom error page.. . # vim/usr/local/nginx/HTML / 404 HTML / / generates error page Oops, No No No page... Please ensure # nginx -s reload# nginx is launching state, otherwise will run the command error, error message is as follows: # [error] open () "/ usr/local/nginx/logs/nginx. Pid" failed (2: No such file or directory)Copy the code

3) After optimization, the client will be prompted with its own defined 40x.html page when accessing non-existent pages using the browser

# Firefox http://192.168.4.5/xxxxx // Visit a page that doesn't existCopy the code

Common HTTP status codes are listed in the reference table

2. View the server status

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

Gz# CD nginx-1.12.2#./configure \> --with-http_ssl_module // enable SSL encryption > --with-stream // enable TCP/UDP proxy module > --with-http_stub_status_module // enable status page # make && make install // compile and installCopy the code

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

You can use the ss command to view information about started ports. The common options of the ss command are as follows:

  • -a Displays information about all ports
  • -n Displays the port number in numeric format
  • -t Displays the TCP connection port
  • -u Displays UDP connection ports
  • -l Displays the information about the port that the service is listening on. For example, the service listens on port 80 after HTTPD is started
  • -p shows what the service name of the listening port is (that is, the program name)

Note: in RHEL7 you can use ss instead of netstat with the same functions and options.

# / usr/local/nginx/sbin/nginx# netstat anptu | grep nginxtcp 0 0 0.0.0.0:0.0.0.0:80 * 10441 / nginx# ss LISTEN - anptu | grep nginxCopy the code

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

# cat/usr/local/nginx/conf/nginx. Conf... ... location /status { stub_status on; #allow IP address; #deny IP address; }... ... # / usr/local/nginx/sbin/nginx -s reload after 4) optimization, to check the status page informationCopy the code
# curl  http://192.168.4.5/statusActive connections: 1 server accepts handled requests 10 10 3 Reading: 0 Writing: 1 Waiting: 0
Copy the code

Active Connections: Indicates the number of Active connections. Accepts: The total number of connections from clients already accepted. Handled: The total number of client connections that have been Handled. The configuration is generally the same as accepts, unless the server limits the number of connections. Requests: Number of Requests sent by the client. Reading: The current server is Reading the number of client request headers. Writing: Number of responses currently being written by the server. Waiting: How many clients are currently Waiting for a response from the server.

Optimize Nginx concurrency

1) Use AB high concurrency test before optimization

# ab - 2000 - c n 2000 http://192.168.4.5/Benchmarking 192.168.4.5 (be patient) socket: Too many Open files (24) // Too many open files are displayedCopy the code

2) Modify the Nginx configuration file to increase concurrency

# vim /usr/local/nginx/conf/nginx.conf.. . worker_processes 2; Events {worker_connections 65535; // Maximum number of concurrent connections per worker}.. . # /usr/local/nginx/sbin/nginx -s reloadCopy the code

3) Optimize Linux kernel parameters (maximum file number)

# ulimit - a / / view all attribute values # ulimit Hn - 100000 / / set the hard limit rules (temporary) # ulimit - 100000 / Sn/set soft limit rules (temporary) # vim/etc/security/limits. Conf . . * soft nofile 100000* hard nofile 100000# This configuration file is divided into 4 columns as follows: 10Copy the code

4) Test server concurrency after optimization (test in proxy because the client did not adjust kernel parameters)

# ab-n 2000-c 2000 http://192.168.4.5/Copy the code

4. Optimize Nginx data header cache

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

[root@proxy ~]# cat lnmp_soft/buffer.sh #! / bin/bashURL = http://192.168.4.5/index.html? for i in {1.. 5000}do URL=${URL}v$I =$idonecurl $URL // Generate a long URL address bar after 5000 times [root@proxy ~]#./buffer.sh.. . <center><h1>414 request-uri Too Large</h1></center> // Prompt header information is Too Large 2) Modify the Nginx configuration file to increase the packet header cache sizeCopy the code
# vim /usr/local/nginx/conf/nginx.conf... . http {client_header_buffer_size 1k; Large_client_header_buffers 4 4k; // The number and capacity of the cache for the large request header information.. . }# /usr/local/nginx/sbin/nginx -s reloadCopy the code

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

1.[root@proxy ~]# cat buffer.sh 2.#! / bin/bash3. URL = HTTP: / / http://192.168.4.5/index.html? 4.for i in {1.. 5000}5.do6. URL=${URL}v$i=$i7.done8.curl $URL9.[root@proxy ~]# ./buffer.shCopy the code

5. The browser caches static data locally

1) Use Firefox to view the cache

For example, enter About :cache in the Firefox address bar to display the cache information of the Firefox browser, as shown in the figure. Click List cache Entries to view the details.

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

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

# vim /usr/local/nginx/conf/nginx.confserver { listen 80; server_name localhost; location / { root html; index index.html index.htm; }location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {expires 30d; / / define the client cache time for 30 days}} # cp/usr/share/backgrounds/day. JPG/usr/local/nginx/html# / usr/local/nginx/sbin/nginx -s Reload# please ensure nginx is launching state, otherwise will run the command error, error message is as follows: 16. # [error] open () "/ usr/local/nginx/logs/nginx pid" failed (2: No such file or directory)Copy the code

4) After optimization, use Firefox browser to access the image and view the cache information again

# firefox http://192.168.4.5/day.jpg
Copy the code

Type about:cache in the Firefox address bar to view the local cache data to see if there are images and the expiration time is correct.