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

Deeply optimize nginx Services in CentOS system

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Nginx in-depth optimization

Nginx (engine x) is a high-performance HTTP and reverse proxy server, lightweight, highly concurrent web server. In a production environment, it is still not possible to directly use the default configuration of Nginx to act as a server. After all, in order to make better use of the Nginx server, we should all make some necessary adjustments to the default configuration of nginx according to our actual needs.

Main optimizations: hiding version numbers, modifying users and groups, web page cache time, connection timeout, number of change processes, web page compression

Warm Tip: this article uses yum to install nginx with one button, except that the configuration file path and the source package installation are not the same, the optimized configuration is the same.

Hidden version number

The hidden version can reduce the risk of being attacked JI and protect the server from running safely.

Yum install nginx-y # install nginx

Systemctl start nginx # launch nginx

Curl-I http://192.168.0.107/ # View version number

Change the configuration file

Vim / etc/nginx/nginx.conf

Server_tokens off; # add the line and hide the version number

Restart the service and verify:

Nginx-s reload

Curl-I http://192.168.0.107/

Modify users, group change profile

Vim / etc/nginx/nginx.conf

User nginx nginx; # modification

Verify after restart:

Change the number of processes change profile

Vim / etc/nginx/nginx.conf

Worker_processes 2; # modified to have the same number of cores or 2 times worker_cpu_affinity 0001 0010

Verify after restart:

Note: the default number of processes in centos7 is 1, and it is recommended to modify it, but the number of processes in centos 8 is automatic or can not be changed!

Add picture cache time change profile

Vim / etc/nginx/nginx.conf

Location ~\. (gif | jpg | jepg | png | bmp | ico) ${root / usr/share/nginx/html; expires 1d;}

Add page connection timeout to change profile

Vim / etc/nginx/nginx.conf

Comment on the line keepalive_timeout first, then add keepalive_timeout 65 180; # set a timeout of 180 seconds client_header_timeout 80

Add Web Page Compression change profile

Vim / etc/nginx/nginx.conf

Gzip on; gzip_buffers 4 64k; gzip_http_version 1.1; gzip_comp_level 2; gzip_min_length 1k; gzip_vary on; gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss text/jpg text/png

Web page grab package verification compression, timeout, cache:

Nginx-s reload nginx # restart the service

Cd / usr/share/nginx/html

Put the test picture into this directory as gg.jpg

Visit the website http://192.168.0.107/gg.jpg

Bag grab test

Note: after gzip compression, the bag grabbing tool may not be able to catch the relevant information, but this setting is correct!

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