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

Detailed description of Nginx Web Page Optimization in Linux system (2)

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

Share

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

To change the number of Nginx running processes in high concurrency scenarios, you need to start more Nginx processes to ensure a quick response to process users' requests and avoid blocking. You can use the ps aux command to check the number of Nginx running processes.

Change the configuration method for the number of processes

Modify configuration file, modify process configuration parameters

Modify the worker_ processes parameter of the configuration file

Generally, the number of CPU or the number of cores can be set to the number of CPU or 2 times the number of cores in the case of high concurrency. In response to access requests, Nginx will not temporarily start new processes to provide services, which reduces the overhead of the system and improves the service speed.

Use ps aux to view the changes in the number of running processes

By default, multiple processes in Nginx may run on a single CPU, and different processes can be assigned to different CPU processing. Make full use of hardware multi-core and multi-CPU on a 4-core physical server, which can be configured as follows: assign processes to Worker_ cpu_affinity 0001 0010 0100 1000 configuration instance [root@localhost conf] # ps aux | grep nginx / / View the number of processes root 5278 0.0 20548 612? Ss 15:17 0:00 nginx: master process / usr/local/nginx/sbin/nginxnginx 5279 0.0 0.0 23076 1396? S 15:17 0:00 nginx: worker processroot 5295 0.0 112728 972 pts/0 S+ 15:18 0:00 grep-- color=auto nginx [root@localhost ~] # cd / proc/ enter the device directory [root@localhost proc] # cat cpuinfo / / View cpu information processor: 0 vendor_id: GenuineIntelcpu family: 6. / / the first cpu message clflush size: 64cache_alignment: 64address sizes: 43 bits physical 48 bits virtualpower management:processor: 1vendor_id: GenuineIntelcpu family: 6. Clflush size: 64 / / the second cpu message cache_alignment: 64address sizes: 43 bits physical, 48 bits virtualpower management: [root@localhost proc] # vim / usr/local/nginx/conf/nginx.conf / / enter the edit nginx configuration file # user nobody Worker_processes 2; / / increase the number of cpu worker_cpu_affinity 01 10; / / set the average distribution of access requests # error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024 }. / / omit part of the content.: wq [root@localhost proc] # systemctl restart nginx.service / / restart the service [root@localhost proc] # ps aux | grep nginx / / View the number of processes root 1813 0.0 20548? Ss 15:32 0:00 nginx: master process / usr/local/nginx/sbin/nginxnginx 1814 0.0 0.0 23076 1400? S 15:32 0:00 nginx: worker processnginx 1815 0.0 0.0 23076 1400? S 15:32 0:00 nginx: worker process// increase the number of processes root 1823 1823 112728 972 pts/0 S+ 15:32 grep-- color=auto nginx configure Nginx to implement web page compression Nginx's ngx_http_gzip_module compression module provides the ability to compress the file content allows the Nginx server to compress the output before sending the client, in order to save website bandwidth and enhance user access experience By default, you can add the corresponding compression function parameters to the configuration file to optimize the compression performance. Gzip on: enable gzip compression output gzip_min_length 1k: used to set the minimum number of bytes of pages allowed to be compressed gzip_buffers 4 16k: apply for 4 units of 16k memory as the compression result stream cache The default value is to apply for the same memory space as the original data to store the gzip compression result zip_http_version 1.00.It is used to set and identify the http protocol version. The default is 1.1.At present, most browsers already support gzip decompression, but the processing is the slowest and consumes server CPU resources gzip_comp_level 2: used to specify the gzip compression ratio, 1 compression ratio is the smallest and the processing speed is the fastest. 9 the compression ratio is the highest and the transmission speed is fast, but the processing speed is the slowest. You can use the default gzip_types text/plain: compression type Gzip_vary on: the option allows the front-end cache server to cache gzip-compressed pages and add the above compression parameters to the configuration instance in the middle section of the main configuration file httpd configuration [root@localhost proc] # cd / usr/local/nginx/conf/ enter the configuration file directory [root@localhost conf] # vim nginx.conf / / Edit the configuration file. / / omit part of the content. # keepalive_timeout 0 Keepalive_timeout 65 180; client_header_timeout 80; client_body_timeout 80; gzip on; / / enable compression function gzip_min_length 1k; / / Edit compression function entry gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 6 Gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json; gzip_disable "MSIE [1-6]\."; gzip_vary on; server {listen 80; server_name localhost . / / omit part of the content.: wq [root@localhost conf] # systemctl restart nginx.service / / restart the service to visit the web page in the client and use the packet grabbing tool to see if compression is enabled.

Configure Nginx to achieve hotlink protection in corporate website services, hotlink protection is generally configured to prevent website content from being illegally stolen, resulting in economic losses. Nginx hotlink protection is also very powerful. By default, only simple configuration is required The hotlink protection configuration instance [root@localhost ~] # mount.cifs / / 192.168.100.10/lamp-c7 / mnt/ mounts the prepared hotlink protection image directory to the Linux system Password for root@//192.168.100.10/lamp-c7: root@localhost mnt] # cd / mnt/ and enters the mount directory [root@localhost mnt] # lsapr-1.6.2.tar.gz Cronolog-1.6.2-14.el7.x86_64.rpm httpd-2.4.29.tar.bz2 mysql-5.6.26.tar.gzapr-util-1.6.0.tar.gz Discuz_X2.5_SC_UTF8.zip LAMP-php5.6.txt nginx-1.12.0.tar.gzawstats-7.6.tar.gz error.png miao.jpg Php-5.6.11.tar.bz2 [root@localhost mnt] # cp error.png / usr/local/nginx/html/ copy the hotlink protection image to the nginx site directory [root@localhost mnt] # cd / usr/local/nginx/html/ enter the site directory [root@localhost html] # ls / / View 50x.html error.png index.html miao.jpg / / successfully copy the picture [root@localhost html] # yum install bind-y / / install the DNS function loaded plug-in: fastestmirror LangpacksLoading mirror speeds from cached hostfile...// omits part of the content. Installed: bind.x86_64 32 bind-export-libs.x86_64 9.11.4-9.P2.el7 installed as a dependency: bind-export-libs.x86_64 32 VR 9.11.4-9.P2.el7 upgraded as a dependency: Bind-libs.x86_64 32 9.P2.el7 bind-utils.x86_64 9.11.4-9.P2.el7 bind-libs-lite.x86_64 32 9.P2.el7 bind-license.noarch 9.11.4-9.P2.el7 bind-utils.x86_64 32 9.P2.el7 bind-utils.x86_64 9.11.4-9.P2.el7 dhclient.x86_64 12 4.2.5-77.el7.centos Dhcp-common.x86_64 12 dhcp-common.x86_64 4.2.5-77.el7.centos dhcp-libs.x86_64 12 77.el7.centos 4.2.5-over! [root@localhost html] # vim / etc/named.conf / / Edit DNS main configuration file. / / omit some of the contents. Options {listen-on port 53 {any;}; listen-on-v6 port 53 {:: 1;}; directory "/ var/named"; dump-file "/ var/named/data/cache_dump.db" Statistics-file "/ var/named/data/named_stats.txt"; memstatistics-file "/ var/named/data/named_mem_stats.txt"; recursing-file "/ var/named/data/named.recursing"; secroots-file "/ var/named/data/named.secroots"; allow-query {any;} ...: wq [root@localhost html] # vim / etc/named.rfc1912.zones / / Edit the DNS zone configuration file... / / omit part of the content... zone "kgc.com" IN {type master; file "kgc.com.zone"; allow-update {none;};} . / / omit some contents.: wq [root@localhost named] # cp-p named.localhost kgc.com.zone / / copy the DNS zone data file, and change the file name [root@localhost named] # vim kgc.com.zone / / Edit the DNS zone data configuration file $TTL 1D @ IN SOA @ rname.invalid. (0; serial 1D; refresh 1H; retry 1W; expire 3H) Minimum NS @ A 127.0.0.1www IN A 192.168.144.133 / set resolution address: wq [root@localhost named] # systemctl start named / / start the DNS service to open a win10 client and a win 7 client, install the web service in the win 7 client, establish a pirated website, and test access to the website in the client

[root@localhost html] # cd.. / conf/ enter the nginx configuration file directory [root@localhost conf] # vim nginx.conf / / Edit the configuration file. / / omit part of the content. # redirect server error pages to the static page / 50x.html # error_page 500502 503 504 / 50x.hml; location = / 50x.html {root html } location ~ *\. (jpg | gif | swf) ${/ / add hotlink protection entry valid_referers none blocked * .kgc.com kgc.com; if ($invalid_referer) {rewrite ^ / http://www.kgc.com/error.png; under the server module }}. / / omit some content.: wq [root@localhost conf] # systemctl restart nginx.service tests whether hotlink protection is enabled in the win 10 client.

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