Liver goods! The practical summary of nginx high performance optimization configuration

Migrant worker brother 2020-11-10 17:24:02
liver goods practical summary nginx


Nginx yes Apache A good substitute for the service . It is characterized by less memory , Strong concurrency , in fact nginx Concurrent The ability is better in the same type of web server , Therefore, famous domestic factories such as : TaoBao , JD.COM , Baidu , Sina , NetEase , Tencent and others are using Nginx Website .

In our daily work and study , How can we optimize our own Nginx The server ? How should we deal with the following problems ?

One 、 How to customize the 404 Error page

1) Before optimization , The client uses a browser to access non-existent pages , Will prompt 404 File not found

[root@client ~]# firefox http://192.168.4.5/xxxxx        // Visit a page that doesn't exist

2) modify Nginx The configuration file , Custom error page

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
    charset utf-8;               // Modify this option only if Chinese is required
error_page   404  /404.html;    // Custom error page
.. ..
[root@proxy ~]# vim /usr/local/nginx/html/404.html      // Generate error page
Oops,No NO no page …
[root@proxy ~]# nginx -s reload
# Please make sure first that nginx Is the boot state , Otherwise, running the command will report an error , The error information is as follows :
#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

3) After optimization , The client uses a browser to access non-existent pages , Will prompt oneself to define 40x.html page

[root@client ~]# firefox http://192.168.4.5/xxxxx    // Visit a page that doesn't exist

common http The status code can be shown in the reference table

Two 、 How to view server status information ( Very important function )

1) Compile and install with --with-http_stub_status_module Open the status page module

[root@proxy ~]# tar  -zxvf   nginx-1.12.2.tar.gz
[root@proxy ~]# cd  nginx-1.12.2
[root@proxy nginx-1.12.2]# ./configure   
> --with-http_ssl_module                        // Turn on SSL encryption
> --with-stream                                // Turn on TCP/UDP Agent module
> --with-http_stub_status_module                // Turn on status Status page
[root@proxy nginx-1.12.2]# make && make install    // Compile and install

2) Enable Nginx service And check the listening port status

ss The command can view the information about the ports started in the system , Common options for this command are as follows :

  • -a Show all port information
  • -n Display port number in digital format
  • -t Show TCP Port of connection
  • -u Show UDP Port of connection
  • -l Display the port information the service is listening on , Such as httpd After starting , It's going to be monitored all the time 80 port
  • -p Display the service name of the listening port ( That is the name of the program )

Be careful : stay RHEL7 You can use ss Command substitution netstat command , Function as , Options as .

[root@proxy ~]# /usr/local/nginx/sbin/nginx
[root@proxy ~]# netstat  -anptu  |  grep nginx
tcp        0        0 0.0.0.0:80        0.0.0.0:*        LISTEN        10441/nginx
[root@proxy ~]# ss  -anptu  |  grep nginx

3) modify Nginx The configuration file , Define the status page

[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
… …
location /status {
                stub_status on;
                 #allow IP Address ;
                 #deny IP Address ;
        }
… …
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

4) After optimization , View status page information

[root@proxy ~]# curl  http://192.168.4.5/status
Active connections: 1 
server accepts handled requests
 10 10 3 
Reading: 0 Writing: 1 Waiting: 0
  • Active connections: Number of currently active connections .
  • Accepts: The total number of connections that have been accepted by clients .
  • Handled: The total number of client connections that have been processed .( Generally speaking, it is related to accepts Agreement , Unless the server limits the number of connections ).
  • Requests: The number of requests sent by the client .
  • Reading: The current server is reading the number of client request headers .
  • Writing: The number of response messages currently being written by the server .
  • Waiting: How many clients are waiting for a response from the server .

3、 ... and 、 Optimize Nginx Concurrency

1) Use before optimization ab High concurrency testing

[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/
Benchmarking 192.168.4.5 (be patient)
socket: Too many open files (24)                // Prompt too many open files

2) modify Nginx The configuration file , Increase the amount of concurrency

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
worker_processes  2;                    // And CPU The number of cores is consistent
events {
worker_connections 65535;        // Every worker Maximum number of concurrent connections
}
.. ..
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

3) Optimize Linux kernel Parameters ( Maximum number of files )

[root@proxy ~]# ulimit -a                        // View all attribute values
[root@proxy ~]# ulimit -Hn 100000                // Set hard limits ( Provisional rules )
[root@proxy ~]# ulimit -Sn 100000                // Set soft limits ( Provisional rules )
[root@proxy ~]# vim /etc/security/limits.conf
    .. ..
*               soft    nofile            100000
*               hard    nofile            100000
# The configuration file is divided into 4 Column , They are as follows :
# User or group      Hard or soft      Items that need to be restricted     The value of the limit

4) Test the server concurrency after optimization ( Because the client did not adjust the kernel parameters , So in proxy test )

[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/

Four 、 Optimize Nginx Packet header caching

1) Before optimization , Use scripts to test whether long header requests get responses

[root@proxy ~]# cat lnmp_soft/buffer.sh
#!/bin/bash
URL=http://192.168.4.5/index.html?
for i in {1..5000}
do
    URL=${URL}v$i=$i
done
curl $URL                                // after 5000 After the second cycle , Generate a long URL Address bar
[root@proxy ~]# ./buffer.sh
.. ..
<center><h1>414 Request-URI Too Large</h1></center>        // The head information is too large

2) modify Nginx To configure file , Increase packet header cache size

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
client_header_buffer_size    1k;        // The default request packet header information cache     
large_client_header_buffers  4 4k;        // The number and capacity of large request packet header information cache
.. ..
}
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

3) After optimization , Use scripts to test whether long header requests get responses

[root@proxy ~]# cat buffer.sh
#!/bin/bash
URL=http://192.168.4.5/index.html?
for i in {1..5000}
do
    URL=${URL}v$i=$i
done
curl $URL
[root@proxy ~]# ./buffer.sh

5、 ... and 、 The browser caches static data locally

1) Use Firefox Browser view cache

With Firefox Browser as an example , stay Firefox Enter... In the address field about:cache Will be displayed Firefox Browser cache information , As shown in the figure , Click on List Cache Entries You can view the details .2) Empty firefox Local cache data , As shown in the figure .3) Change Nginx To configure file , Define the cache time for static pages

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
location ~* .(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires        30d;            // Define the client cache time as 30 God
}
}
[root@proxy ~]# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
# Please make sure first that nginx Is the boot state , Otherwise, running the command will report an error , The error information is as follows :
#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

4) After optimization , Use Firefox Browser access to images , Look at the cache again

[root@client ~]# firefox http://192.168.4.5/day.jpg

stay firefox Enter... In the address field about:cache, View local cache data , Check if there are pictures and the expiration time is correct .

source :https://blog.csdn.net/mage_li...

image

版权声明
本文为[Migrant worker brother]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple