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

The method of Nginx Service Optimization

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points about the method of Nginx service optimization. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Nginx service optimization can be optimized from the following aspects: hiding version number, changing users and groups, configuring web page caching time, log cutting, and setting connection timeout. 1. Hidden version number

The version number of Nginx needs to be hidden in the production environment to avoid revealing the version of Nginx, so that people who are × × can not carry out × × for a specific version. To see the version of Nginx, use the command curl-I http://172.16.10.10/ in CentOS.

[[email protected] ~] # curl-I http://172.16.10.10/ HTTP/1.1 200 OK Server: nginx/1.12.0 # Nginx version Information Date: Fri, 29 Jun 2018 08:52:27 GMT Content-Type: text/html Content-Length: 483Last-Modified: Fri, 29 Jun 2018 06:56:20 GMT Connection: keep-alive ETag: "5b35d814-1e3" Accept-Ranges: bytes

There are two ways to hide the version number, one is to modify the source file of Nginx to specify that the version number is not displayed, and the second is to modify the main configuration file of Nginx.

The main configuration file can be modified as follows:

Set the server_tokens option value in Nginx's configuration file to off, and if there is no such configuration item, add it.

[email protected] ~] # vim / usr/local/nginx/conf/nginx.conf. # omit the content http {include mime.types; default_type application/octet-stream; server_tokens off; # close version number. # omit content

[[email protected] ~] # nginx-t # Test configuration file nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful

Visit the URL again and only show Nginx. The version number has been hidden.

[[email protected] ~] # service nginx restart # restart the nginx service [[email protected] ~] # curl-I http://172.16.10.10/ HTTP/1.1 OK Server: nginx # nginx hides the version number Date: Fri, 29 Jun 2018 09:09:36 GMT Content-Type: text/html Content-Length: 483Last-Modified: Fri, 29 Jun 2018 06:56:20 GMT Connection: keep-alive ETag: "5b35d814-1e3" Accept-Ranges: bytes

The source file of Nginx contains version information, which can be set up at will, and then recompiled and installed, which will hide the version information.

[[email protected] ~] # vim / opt/nginx-1.12.0/src/core/nginx.h # Edit source file # define NGINX_VERSION "1.1.1" # modify version # define NGINX_VER "IIS" NGINX_VERSION # modify server type

Recompile installation

[[email protected] ~] # cd / opt/nginx-1.12.0/ [[email protected] nginx-1.12.0] #. / configure-prefix=/usr/local/nginx-user=nginx-group=nginx-with-http_stub_status_module & & make & & make install

Visit the URL again and display only the modified version information.

[[email protected] nginx-1.12.0] # service nginx restart # restart the nginx service [[email protected] nginx-1.12.0] # curl-I http://172.16.10.10/HTTP/1.1 200 OK Server: IIS1.1.1 # nginx version information Date: Fri, 29 Jun 2018 09:30:09 GMT Content-Type: text/html Content-Length: 483Last-Modified: Fri 29 Jun 2018 06:56:20 GMT Connection: keep-alive ETag: "5b35d814-1e3" Accept-Ranges: bytes

two。 Modify users and groups

The Nginx runtime process needs the support of users and groups to realize the access control when reading the files of the website. The main process is created by root, and the child processes are created by specified users and groups. Nginx uses nobody user account and group account by default, which usually needs to be modified.

(1) specify users and groups when compiling Nginx, that is, when configuring nginx, specify the parameters of users and groups after. / configure. [[email protected] ~] # cd / opt/nginx-1.12.0/ [[email protected] nginx-1.12.0] #. / configure-- prefix=/usr/local/nginx-- user=nginx # specifies that the user name is nginx--group=nginx # specifies the group name is nginx--with- & & make & & make install

(2) modify the Nginx configuration file nginx.conf to specify users and groups. [[email protected] ~] # vim / usr/local/nginx/conf/nginx.conf user nginx nginx; # modify user to nginx and group to nginx

Restart nginx to check the running of the process. The main process is created by the root account and the child process is created by nginx.

[[email protected] ~] # ps aux | grep nginx root 14923 0.0 20540 624? Ss 17:30 0:00 nginx: master process / usr/local/nginx/sbin/nginx # main process created by root nginx 14925 0.00.1 22984 1412? S 17:30 0:00 nginx: worker process # child process created by nginx root 19344 0.0 112720 984 pts/0 R + 17:47 0:00 grep-- color=auto nginx

3. Configure web page caching time

When Nginx returns the web page data to the client, the caching time can be set to facilitate the direct return of the same content request in the future, so as to avoid repeated requests and speed up the access speed. Generally, it is only set for static resources, but not for dynamic web pages. The steps are as follows:

(1) use the picture as the cache object and place the game.jpg in the website directory of Nginx. [[email protected] ~] # cd / usr/local/nginx/html/ # Nginx website directory [[email protected] html] # ls 50x.html error.png game.jpg index.html test.html

(2) visit http://172.16.10.10/game.jpg, and then use the Fidder tool to grab the package and view the response message. There is no cache information for the image.

(3) modify the configuration file, add the expire parameter to the new location segment, and specify the cache time. 1 day represents one day. [[email protected] ~] # vim / usr/local/nginx/conf/nginx.conf location ~\. (gif | jpg | jepg | png | bmp | ico) ${# add new location root html; expires 1d; # specify cache time}

(4) restart the nginx service and visit the URL to grab the package. The response message contains an Expire parameter, indicating the cache time. [email protected] ~] # service nginx restart

[

These are all the contents of this article entitled "methods of Nginx Services Optimization". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report