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

Nginx depth optimization-hiding version number, log segmentation, cache processing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Main points of content:

Hidden version number

Web caching

Log segmentation

Hidden version number

There are two ways to configure:

Modification of configuration file method

Modified source code method

I. the method of modifying the configuration file:

[root@localhost init.d] # curl-I http://192.168.13.140/ # # 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 version number [root@localhost init.d] # service nginx stop # # close service [root@localhost init.d] # service nginx start # # enable service [root@localhost init.d] # curl-I http://192.168.13.140/ # # 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

Using the curl-I command to detect, you can see the version number

Use curl-I to query again

Second, forge the version number (need to recompile and install, or you can operate before compilation and installation)

[root@localhost ~] # cd / opt/nginx-1.12.1/src/core [root@localhost core] # vim nginx.h enter the configuration file to modify the version number you want to display [root@localhost core] # cd. /.. / [root@localhost nginx-1.12.2] # / configure\ and then recompile >-- prefix=/usr/local/nginx\ >-- user=nginx\ > -group=nginx\ >-with-http_stub_status_module

Restart the nginx service to 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.13.140/ # # View Nginx information OK Server: nginx/1.1.1 # # 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: bytes

Web page caching 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

Configuration method:

You can modify the configuration file to add expiration parameters for specific content in the http section, or the server section, or the location section.

1. Copy the test image to the directory of the nginx web site.

[root@localhost mnt] # cp test.jpg / usr/local/nginx/html/ # # copy pictures to the site [root@localhost mnt] # cd / usr/local/nginx/html/ # # switch to [root@localhost html] # lstest.jpg 50x.html index.ht under the site

2. Modify the web page information and add the test picture to the index.html file.

[root@localhost html] # vim index.html # # modify web page information Welcome to nginx!

# # adding images to web pages

3. Modify configuration file information and add cache time

[root@localhost html] # vim / usr/local/nginx/conf/nginx.conf # # modify configuration file events {worker_connections 1024;} user nginx nginx; # # modify Nginx user and group # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~\. (gif | jepg | jpg | bmp | png) ${# support image format root html; # # site expires 1D # # Cache for one day} [root@localhost html] # service nginx stop # # disable enabling service [root@localhost html] # service nginx start

4. Visit the web page and use fiddler to view the cache

Log Segmentation 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 for monitoring is a catastrophe to 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.

1. Write a log segmentation script file

[root@localhost ~] # vim fenge.sh # # script file #! / bin/bash#Filename:fenge.shd=$ (date-d "- 1 day"+% Y%m%d") # # shows the time of the day before logs_path= "/ var/log/nginx" # the path to save the split log pid_path= "/ usr/local/nginx/logs/nginx.pid" # # Path to 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## the original log files are generated to the new path kill-USR1 $(cat $pid_path) # # end the re-generation of the new pid file find $logs_path-mtime + 30 | xargs rm-rf # # delete 30 days ago Log file [root@localhost ~] # chmod + x fenge.sh # # give execution permission [root@localhost ~] #. / fenge.sh # # execute script file

2. View log segmentation

[root@localhost ~] # cd / var/log/nginx/ # # switch to [root@localhost nginx] # lstest.com-access.log-20191112 [root@localhost nginx] # date-s 2019-11-14 # # modify date tomorrow on Thursday, November 14, 2019, 00:00:00 CST [root@localhost nginx] # cd ~ [root@localhost ~] #. / fenge.sh # # re-execute foot This [root@localhost ~] # cd / var/log/nginx/ [root@localhost nginx] # ls # # View the log file test.com-access.log-20191112 test.com-access.log-20191113 of log segmentation

3. Set up periodic scheduled tasks and divide them periodically.

[root@localhost nginx] # crontab-e # # periodically schedule task 0 1 * / opt/fenge.sh

For more nginx optimization status statistics, access control, etc., please visit my home page.

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