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
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)
Step 2: Hide version information
- Hide nginx version
[ root@localhost ~]#Vi/usr/local/nginx/conf/nginx.conf//Edit the main configuration file
Add the following:
Save Exit
[ root@localhost ~]#Systemctl restart nginx//Restart nginx service
- Hide php information
[ root@localhost ~]# vi /usr/local/php/lib/php.ini
Modify the following:
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
Method 2: Wireshark packet capture
visit: http://192.168.80.40/index.php
//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:
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
//Successful camouflage
Part II Keep connected
1: Wireshark packet capture view connection configuration
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
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
Grab the bag with wireshark
//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
[ root@localhost ~]# ps aux | grep nginx
[ root@localhost ~]#Vi/usr/local/nginx/conf/nginx. conf//Edit nginx main configuration file
Edit the following:
Save Exit
[ root@localhost ~]#Nginx – t//Check for syntax errors
[ 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
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
- 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
- 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
- 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
- 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
Save Exit
- Modify Page File Size
[ root@localhost ~]#Ls - lh/usr/local/nginx/html//View the size of the html file
[ 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
[ root@localhost ~]#Service nginx restart//Restart the nginx service
- verification
Win7 Access http://192.168.80.40
Validate the results with wireshark packet capture
//Compression takes effect
(Note that if the results do not come out, clear the browser cache)