Nginx tuning under Linux

Nginx Tuning
catalog
Part I Optimized Version Information
Part II Keep connected
Part III Number of optimization processes
Part IV Log Segmentation
Part V Web Page Compression

Part I Optimized Version Information
----------Hide version information----------
Step 1: View version information
Method 1:
[ root@localhost ~]# curl -I 192.168.80.40
Linux 下 nginx 调优
Method 2: Wireshark packet capture
Win7 Access http://192.168.80.40/index.php
(Note: PHP information can only be displayed if the suffix is accompanied by a php file)
Linux 下 nginx 调优
Linux 下 nginx 调优

Step 2: Hide version information

  1. Hide nginx version
    [ root@localhost ~]#Vi/usr/local/nginx/conf/nginx.conf//Edit the main configuration file
    Add the following:
    Linux 下 nginx 调优
    Save Exit
    [ root@localhost ~]#Systemctl restart nginx//Restart nginx service
  2. Hide php information
    [ root@localhost ~]# vi /usr/local/php/lib/php.ini
    Modify the following:
    Linux 下 nginx 调优
    Save Exit
    [ root@localhost ~]#Systemctl restart nginx//Restart nginx service

Step 3: Test and verify
Method 1:
[ root@localhost ~]# curl -I 192.168.80.40
Linux 下 nginx 调优
Method 2: Wireshark packet capture
visit: http://192.168.80.40/index.php
Linux 下 nginx 调优
//Successfully hidden

----------Modify nginx version information---------
(Note that this method will delete all existing configurations. It is recommended to use this method when you have just built nginx)
1: Edit Profile
[ root@localhost ~]# vi /opt/nginx-1.13.9/src/core/nginx. h
Modify information in the source code directory:
Linux 下 nginx 调优
Save Exit
2: Delete the installation file directory and recompile the installation
[ root@localhost ~]# cd /usr/local/
[ root@localhost Local] # rm - rf nginx//Delete nginx installation directory
[ root@localhost Local] # cd/opt/nginx - 1.13.9//Enter the directory of the nignx unzipped package
[ root@localhost nginx-1.13.9]#./ Configure -- prefix=/usr/local/nginx -- user=nginx -- group=nginx -- with http_stub_status_module//Define the configuration
[ root@localhost Nginx - 1.13.9] # Make//Compile
[ root@localhost Nginx - 1.13.9] # make install//Install
[ root@localhost Nginx - 1.13.9] # kill - 1 nginx//Safely restart nginx service
[ root@localhost Nginx - 1.13.9] # nginx//Start nginx service
[ root@localhost Nginx - 1.13.9] # netstat - anpt | grep nginx//View the running status of nginx
3: Re access http://192.168.80.10 , packet capturing verification
Linux 下 nginx 调优
Linux 下 nginx 调优
//Successful camouflage

Part II Keep connected
1: Wireshark packet capture view connection configuration
Linux 下 nginx 调优
2: Edit Profile
[ root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
Edit the following:
Keepalive_timeout 65 180;//The later one prevails, which can be set in http server location
Client_header_timeout 80;//When the timeout period for waiting for the client to send the request header expires, 408 errors will be sent
Client_body_timeout 80;//Set the timeout for the client to send the request body
Linux 下 nginx 调优
Save Exit
(Note: The first parameter of keepalive_timeout specifies the timeout of the keep alive connection with the client, after which the server will close the connection. The optional second parameter specifies the time value in the response header Keep Alive: timeout=time. This header enables some browsers to actively close the connection, so that the server does not have to close the connection.). If this parameter is not available, Nginx will not send a Keep Alive response header)
[ root@localhost ~]#Service nginx restart//Restart the nginx service
3: Packet capturing verification results
visit http://192.168.80.40
Linux 下 nginx 调优
Grab the bag with wireshark
Linux 下 nginx 调优
//Configuration succeeded

Part III Number of optimization processes
The nginx service was originally processed by one CPU core, but was configured to be processed by multiple cores to increase processing efficiency
[ root@localhost ~]#Cat/proc/cpuinfo | grep "processor"//View the native processor
Linux 下 nginx 调优
[ root@localhost ~]# ps aux | grep nginx
Linux 下 nginx 调优
[ root@localhost ~]#Vi/usr/local/nginx/conf/nginx. conf//Edit nginx main configuration file
Edit the following:
Linux 下 nginx 调优
Save Exit
[ root@localhost ~]#Nginx – t//Check for syntax errors
Linux 下 nginx 调优
[ root@localhost ~]#Kill - 9 nginx//End the nginx process
[ root@localhost ~]#Service nginx start//Start nginx service
[ root@localhost ~]#Ps aux | grep nginx//View nginx process
Linux 下 nginx 调优

Part IV Log Segmentation
Nginx does not have a special command to perform log splitting. It can only be implemented through scripts
[ root@localhost ~]#Ls/usr/local/nginx/logs//View the default log

  1. Write log split script
    [ root@localhost ~]# cd /usr/local/nginx/logs/
    [ root@localhost Logs] # vi fenge.sh//Create a log splitting script
    #!/ bin/bash
    #filename: fenge.sh
    D=$(date -d "-1 day" "+%Y%m%d")
    LOGS_PATH="/var/log/nginx"//Specify the partition log directory
    PID_PATH="/usr/local/nginx/logs/nginx.pid"
    [- d $LOGS_PATH] | | mkdir - p $LOGS_PATH//Create the split log file directory (if it exists, it will not be created; if it does not exist, it will be created)
    Mv/usr/local/nginx/logs/access.log ${LOGS_PATH}/aa.com-access.log - $D//Move and rename the split log file
    Kill - USR1 $(cat $PID_PATH)//Rebuild the split log file
    Find $LOGS_PATH - mtime+30 | xargs rm – rf//Automatically delete after 30 days
    Save Exit
  2. Execute the script to test whether the log is split
    [ root@localhost ~]#Sh fenge.sh//Execute the script
    Win7 Access http://192.168.80.40
    [ root@localhost Logs] # ls/var/log/nginx///View the split log and split the log file by date
    Linux 下 nginx 调优
  3. Plan tasks and divide logs regularly
    [ root@localhost logs]# crontab –e
    30 1 * sh /root/fenge.sh
    (Execute the/opt/fenge.sh script at 1:30 every morning to split the log)

Part V Configure Web Page Compression

  1. Modify Nginx configuration file and add compression function parameters
    [ root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
    Gzip on;//Enable gzip compression
    Gzip_buffers 4 64k;//It means that four 16KB units of memory are requested as the compression result stream cache. The default value is to request the same size of memory as the original data to store the gzip compression results
    Gzip_http_version 1.1;//Set to identify the http protocol version
    Gzip_comp_level 2;//Specify the gzip compression ratio. The compression ratio 1 is the smallest and the processing speed is the fastest; The compression ratio is 9, and the transmission speed is the fastest, but the processing speed is the slowest. Use the default
    Gzip_min_length 1k;//Set the minimum number of bytes allowed to compress the page
    Gzip_vary on;//Let the front-end cache server cache gzip compressed pages
    Gzip_types text/main text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;//The compression type refers to which web pages enable compression
    Linux 下 nginx 调优
    Save Exit
  2. Modify Page File Size
    [ root@localhost ~]#Ls - lh/usr/local/nginx/html//View the size of the html file
    Linux 下 nginx 调优
    [ root@localhost ~]# truncate -s 10k /usr/local/nginx/html/index.html
    //Because the compression configuration defaults to a minimum 1KB file, the index.html is magnified to more than 1K
    Linux 下 nginx 调优
    [ root@localhost ~]#Service nginx restart//Restart the nginx service
  3. verification
    Win7 Access http://192.168.80.40
    Linux 下 nginx 调优
    Validate the results with wireshark packet capture
    Linux 下 nginx 调优
    //Compression takes effect
    (Note that if the results do not come out, clear the browser cache)

 Watson Blog
  • This article is written by Published on April 13, 2019 14:44:41
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/1699.html

Comment

//Prohibit copying