Nginx optimized caching and compression, version hiding
Install and configure Nginx
Mount the remote source package locally
Mount.cifs / / 192.168.100.10/LNMP-C7 / mnt / / Mount to / mnt directory
Decompress the source code package to / opt directory
[root@localhost ~] # cd / abc / / switch to the mount point directory [root@localhost abc] # lsDiscuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gzmysql-boost-5.7.20.tar.gz php-7.1.10.tar.gz [root@localhost abc] # tar zxvf nginx-1.12.2.tar.gz -C / opt/ / decompress the Nginx source code package to [root@localhost abc] # cd / opt/ under / opt and switch to [root@localhost opt] # lsnginx-1.12.2 rh under the decompressed directory
Install the environment package required for compilation
[root@localhost opt] # yum-y install\ gcc\ / / c language gcc-c++\ / / C++ language pcre-devel\ / / pcre language tool zlib-devel / / function library for data compression
Create a user named nginx and compile Nginx
[root@localhost opt] # useradd-M-s / sbin/nologin nginx / / Creator user Limit its [root@localhost opt] # cd nginx-1.12.2/ switch to [root@localhost nginx-1.12.2] # / configure\ / / configure nginx >-- prefix=/usr/local/nginx\ / / installation path under the nginx directory >-- user=nginx\ / / user name >-- group=nginx\ / / user group >-- with-http_stub_status_module / / access status statistics module
Compile and install
[root@localhost nginx-1.12.0] # make & & make install / / compilation and installation
Make Nginx management script, easy to manage and use
[root@localhost nginx] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ create a soft connection [root@nginx nginx-1. 12.2] # vim / etc/init.d/nginx / / Edit startup script #! / bin/bash# chkconfig:-99 2 starting description: Nginx Service Control ScriptPROG= "/ usr/local/nginx/sbin/nginx" PIDF= "/ usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ; stop) kill-s QUIT $(cat $PIDF);; restart) $0 stop $0 start;; reload) kill-s HUP $(cat $PIDF) *) echo "Usage: $0 {start | stop | restart | reload}" exit 1 esac exit 0 [root@nginx nginx-1.12.2] # chmod + x / etc/init.d/nginx / / give script execution permission [root@nginx nginx-1.12.2] # chkconfig-- add nginx / / add to service Manager [root@nginx nginx-] 1.12.2] # yum install elinks-y / / [root@nginx nginx-1.12.2] # service nginx start / / start the Nginx service [root@nginx nginx-1.12.2] # netstat-ntap | grep 80tcp 00 0.0.0.0 service nginx start 80 0.0.0.0 service nginx start * LISTEN 42028 / nginx: master [root@nginx nginx-1.12.2] # systemctl stop firewalld.service / / turn off the firewall [root@nginx nginx-1.12.2] # setenforce 0 / / turn off enhanced security features [root@nginx nginx-1.12.2] # elinks http://192.168.131.133/ Configure Nginx web page caching
Copy the picture to the site directory
[root@localhost nginx-1.12.0] # ls / abcDiscuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gztupian.png php-7.1.10.tar.bz2mysql-boost-5.7.20.tar.gz php-7.1.20.tar.gznginx-1.12.0.tar.gz [root@localhost nginx-1.12.0] # cp / abc/game.jpg / usr/local/nginx/ Html/ [root@localhost nginx-1.12.0] # cd / usr/local/nginx/html/ [root@localhost html] # ls50x.html game.jpg index.html
Modify the web page information to add pictures to the index.html file
[root@localhost html] # vim index.htmlWelcome to nginx!
/ / add the image path [root@localhost html] # vim / usr/local/nginx/conf/nginx.conf / / modify the configuration file events {worker_connections 1024;} user nginx nginx / / modify Nginx users and groups # deny access to .htaccess files, if Apache's document root # concurs with nginx's one location ~\. (gif | jepg | jpg | ico | png) ${/ / add supported image format root html; / / site expires 1D / / Cache for one day} [root@localhost html] # service nginx stop / / restart the service [root@localhost html] # service nginx start
Use a virtual machine to access web pages and use fiddler to view the cache
Nginx web page compression
Modify the Nginx.conf file
[root@localhost ~] # cd / usr/local/nginx/conf/ [root@localhost conf] # vim nginx.confgzip on; / / use the x key to delete the pound sign note gzip_min_length 1k before this line; / / compression threshold gzip_buffers 4 16k; / / buffers size is 4 16k buffer size gzip _ http_version 1.1k / / compressed version number: gzip_comp_level 6: impact / compression ratio: minimum is 1, processing is fast but transmission is slow; maximum is 9, processing is slow, but transmission is fast; set here 6, relatively moderate gzip_types text/plain application/x-javascript text/css image/jpg image/jpegimage/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json / / supported type format type gzip_disable "MSIE [1-6]\."; / / configure to disable gzip conditions and support regular expressions, indicating that gzipgzip_vary on; is not enabled below ie6 / / Let the front-end cache server cache gzip-compressed pages
Copy the picture to the site directory
[root@localhost conf] # cd.. / html/ [root@localhost html] # cp / abc/tupian.png/ [root@localhost html] # ls50x.html tupian.png index.html
Modify the content of the home page of the site
[root@localhost html] # vim index.htmlWelcome to nginx!
/ / add the image path under the H2 tag [root@localhost html] # systemctl stop nginx.service / / restart the service [root@localhost html] # systemctl start nginx.service [root@localhost html] # systemctl stop firewalld.service / / turn off the firewall [root@localhost html] # setenforce 0 / / disable the enhanced security feature
Use the test machine to access the web page and use fiddler to view the compression
Nginx version Hidden
Hidden version number
[root@localhost init.d] # curl-I http://192.168.131.133/ View Nginx information HTTP/1.1 200 OKServer: nginx/1.12.2 / / display version number Date: Tue, 12 Nov 2019 14:23:24 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264" Accept-Ranges: bytes [root@localhost init.d] # vim / usr/local/nginx/conf/nginx.conf # # modify the configuration file http {/ / add include mime.types under http Default_type application/octet-stream; server_tokens off / / close the version number [root@localhost init.d] # service nginx stop / / close the service [root@localhost init.d] # service nginx start / / enable the service [root@localhost init.d] # curl-I http://192.168.131.133/ View Nginx information HTTP/1.1 200OK Server: nginx / / version number is hidden Date: Tue 12 Nov 2019 14:22:00 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264" Accept-Ranges: bytes
Forged version number
[root@localhost init.d] # vim / usr/local/nginx/conf/nginx.confhttp {include mime.types; default_type application/octet-stream; server_tokens on / / Open the version number [root@localhost init.d] # cd / opt/nginx-1.12.2/src/core/ switch to the src source package directory [root@localhost core] # vim nginx.h / / enter the configuration file # define NGINX_VERSION "1.1.1" / / where the version number is false to create 1.1.1 [ Root@localhost core] # cd / opt/nginx-1.12.2/ change the directory to [root@localhost nginx-1.12.2] # / configure\ / / reconfigure >-- prefix=/usr/local/nginx\ >-- user=nginx\ >-- group=nginx\ >-- with-http_stub_status_module [root@localhost nginx-1.12.0] # make & & make install / / again New compilation and installation [root@localhost nginx-1.12.2] # service nginx stop / / restart service [root@localhost nginx-1.12.2] # service nginx start [root@localhost nginx-1.12.2] # curl-I http://192.168.131.133/ View Nginx information HTTP/1.1 200 OK Server: nginx/1.1.1 / / at this time The version number is the forged version number Date: Tue 12 Nov 2019 14:34:02 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264" Accept-Ranges: bytes