Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Hidden version number of Nginx, optimized cache, log segmentation

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Hidden version number configuration of nginx nginx [root@localhost ~] # yum install pcre-devel zlib-devel gcc gcc-c++-y # # installation environment package [root@localhost ~] # useradd-M-s / sbin/nologin nginx # # create programmatic user [root@localhost] # mkdir / chen # # create mount point [root@localhost] # mount.cifs / / 192.168.100.23/LNMP / chen # # mount Password for root@//192.168 .100.23 / LNMP: [root@localhost chen] # tar zxvf nginx-1.12.2.tar.gz-C / opt/ # # decompress [root@localhost chen] # cd / opt/ [root@localhost opt] # lsnginx-1.12.2 rh [root@localhost opt] # cd nginx-1.12.2/ [root@localhost nginx-1.12.2] # lsauto CHANGES.ru configure html man srcCHANGES conf contrib LICENSE README./configure\ # # install nginx components-- prefix=/usr/local/nginx\-- user=nginx\-- group=nginx\-- with-http_stub_status_ module [root @ localhost nginx-1.12.2] # make & & make install # # compile [root@localhost nginx-1.12.2] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ # # soft link to enable the system to recognize the owner of nginx command [root@localhost nginx-1.12 .2] # nginx- t # # check syntax errors nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful write nginx script in system startup script to facilitate service manager to manage [root@localhost nginx-1.12.2] # cd / etc/init.d/ # # to system startup script [root@localhost init.d] # vim nginx # # Write a nginx script #! / bin/bash#chkconfig:-99 20 # comment message # description: Nginx Service Control ScriptPROG= "/ usr/local/nginx/sbin/nginx" # this variable Point to my command file PIDF= "/ usr/local/nginx/logs/nginx.pid" # this variable, and point to the nginx process number 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 1esacexit 0 [root@localhost init.d] # chmod + x nginx # # increase the authority of Nginx [root@localhost init.d] # chkconfig-- add nginx # # add nginx [root@localhost init.d] # service nginx start [root@localhost init. D] # netstat-ntap | grep nginx tcp 00 0.0.0.0grep nginx tcp 80 0.0.0.0LISTEN 17544/nginx: master [root@localhost init.d] # systemctl stop firewalld.service [root@localhost init.d] # setenforce 0 check the version number of our nginx now [root@localhost ~] # curl-I root@localhost 200 OKServer: nginx/1.12.2Date: Tue 05 Nov 2019 07:30:14 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 05 Nov 2019 07:13:26 GMTConnection: keep-aliveETag: "5dc12116-2019" Accept-Ranges: bytes closed version number check again [root@localhost ~] # vim / usr/local/nginx/conf/nginx.confhttp {# # add include mime.types at http level Default_type application/octet-stream; server_tokens off # # close version number [root@localhost init.d] # service nginx stop # # disable service [root@localhost init.d] # service nginx start # # enable service [root@localhost init.d] # curl-I http://192.168.136.163/ # # 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 to modify the Nginx source package file [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 # # modify the file # define NGINX_VERSION "1.1.2" # # where the false version number causes 1.1.2 to recompile and install [root@localhost core] # cd / opt/ Nginx-1.12.2/ # # to Nginx [root@localhost nginx-1.12.2] # / configure\ # # reinstall components >-- prefix=/usr/local/nginx\ >-- user=nginx\ >-- group=nginx\ >-- with-http_stub_status_ module [root @ localhost nginx-1.12.0] # make # # recompile. [root@localhost nginx-1.12.0] # make install # # reinstall. Restart the Nginx service View version information [root@localhost nginx-1.12.2] # service nginx stop # # close [root@localhost nginx-1.12.2] # service nginx start # # turn on [root@localhost nginx-1.12.2] # curl-I http://192.168.136.163/ # # View Nginx information OK Server: nginx/1.1.2 # # the version number at this time 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: bytesNginx optimized cache time

When Nginx returns the web page data to the client, the cache time can be set to facilitate direct return when making requests for the same content in the future, avoiding repeated requests and speeding up the access speed.

Generally, it is set for static web pages, but not for dynamic web pages.

You can use fiddler to view the page caching time in the Windows client

Let's first prepare a picture in the shared directory of the host and put the picture on the nginx site [root@localhost ~] # cd / chen/ [root@localhost chen] # lsDiscuz_X3.4_SC_UTF8.zip nginx-1.12.0.tar.gz php-7.1.20.tar.gzfang.png nginx-1.12.2.tar.gz shu.jpgmysql-boost-5.7.20.tar.gz php- 7.1.10.tar.bz2 [root@localhost chen] # cp shu.jpg / usr/local/nginx/html/ [root@localhost chen] # cd / usr/local/nginx/html/ [root@localhost html] # ls50x.html index.html shu.jpg [root@localhost html] # vim index.html # # enter the page to make a picture 15

# # add image caching time in line 15 [root@localhost html] # vim / usr/local/nginx/conf/nginx.conf## at server server level 76 location ~\. (gif | jepg | jpg | icp | bmp | png) ${# # add 77 root html; # # administrator page 78 expires 1d; # # caching time 1 day 16 user nginx nginx # # 16 join nginx users and groups [root@localhost html] # service nginx start client to test access to the website

Use the bag grab tool to take a look at the cache time

Log cutting of Nginx

As the Nginx elapsed time, so did the logs. In order to easily grasp the running status of Nginx, you need to pay attention to the log files all the time.

Too large log files are a disaster for monitoring.

Cut log files regularly

Nginx itself does not have the function of log segmentation, but it can be automatically cut through the script of Nginx signal control function, and log cutting can be carried out periodically through the planned task of Linux.

Write a log partition script file [root@localhost ~] # vim fenge.sh # # write a script file #! / bin/bash#Filename:fenge.shd=$ (date-d "- 1 day"+% Y%m%d") # # display the time one day ago logs_path= "/ var/log/nginx" # the save path of the split log pid_path= "/ usr/local/nginx/ Logs/nginx.pid "# # path of pid [- d $logs_path] | | mkdir-p $logs_path # # create a directory if there is no directory mv / usr/local/nginx/logs/access.log ${logs_path} / test.com-access.log-$d generate the original log file to the new path kill-USR1 $(cat $pid_path) # # end regenerating the new pid file find $logs_path-mtime + 30 | xargs rm-rf # # Delete the log file from 30 days ago [root@localhost ~] # chmod + x fenge.sh # # give the execution permission [root@localhost ~] #. / fenge.sh # # execute the script file to view the log partition [root@localhost ~] # cd / var/log/nginx/ # # switch to [root@localhost nginx] # lstest.com-access.log-20191112 [root@localhost nginx] # date-s 2019-in the log directory of Nginx 11-14 # # the modification date is tomorrow's time. Thursday, November 14, 2019, 00:00:00 CST [root@localhost nginx] # cd ~ [root@localhost ~] #. / fenge.sh # # re-execute the script [root@localhost] # cd / var/log/nginx/ [root@localhost nginx] # ls # # View the log partition log file test.com-access.log-20191112 test.com-access.log -20191113 set periodic scheduled task [root@localhost nginx] # crontab-e # # periodic scheduled task 01 * / opt/fenge.sh is all we have Thanks for watching

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report