Home  >  Article  >  Operation and Maintenance  >  How to optimize nginx small and medium vps

How to optimize nginx small and medium vps

WBOY
WBOYforward
2023-05-22 12:32:221158browse

Small vps is limited by system resources. When the number of visits is too large and exceeds the limit that the system can bear, some requests will be 502. When system resources are sufficient, optimize nginx, php-fpm, and the system itself to achieve two purposes:

1. Properly allocate system resources to maximize the use of limited resources. Good steel is used in the blade.

2, try to reduce disk i/o

1, main system resources

[root@xxxxxx nginx]# free -m  
       total    used    free   shared  buffers   cached  
mem:      994    815    179     0     43    118  
-/+ buffers/cache:    453    540  
swap:      0     0     0  
 
[root@xxxxxx nginx]# cat /proc/cpuinfo  
processor    : 0  
vendor_id    : genuineintel  
cpu family   : 6  
model      : 62  
model name   : intel(r) xeon(r) cpu e5-2650 v2 @ 2.60ghz  
stepping    : 4  
cpu mhz     : 2594.024  
cache size   : 20480 kb  
physical id   : 0  
siblings    : 1  
core id     : 0  
cpu cores    : 1  
apicid     : 0  
initial apicid : 0  
fpu       : yes  
fpu_exception  : yes  
cpuid level   : 13  
wp       : yes  
flags      : fpu vme de pse tsc msr pae mce cx8 apic 。。。。省略。。。。  
bogomips    : 5188.04  
clflush size  : 64  
cache_alignment : 64  
address sizes  : 46 bits physical, 48 bits virtual  
power management:

2, php-fpm optimization

pm = dynamic           //进程数,动态分配
pm.max_children = 24       //最大进程数
pm.start_servers = 8       //刚启动时的进程数
pm.min_spare_servers = 8     //服务器空闲时的最小进程数
pm.max_spare_servers = 24     //服务器空闲时的最大进程数

php_flag[display_errors] = off  //运行一段时间后,将错误提示信息关闭掉

One process of php-fpm occupies between 20m-30m. Top, look at the memory percentage occupied by php-fpm, and you can estimate it. Max_children, max_spare_servers are not bigger, the better.

Three, nginx optimization

1, install the latest stable version

# vim /etc/yum.repos.d/nginx.repo  //加上以下内容

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

# yum install nginx   //更新nginx

2, optimize the configuration of nginx

worker_processes auto;   //设置auto,nginx进程动态分配

# access_log  //注释掉,减少i/o
# log_format  //注释掉,减少i/o

gzip on;     //开启gzip
gzip_min_length 1k;
gzip_buffers   4 16k;
gzip_http_version 1.1;
gzip_comp_level 5;    //1-9,越大压缩越好,消耗资源越大
gzip_types    text/plain application/x-javascript text/css application/xml;
gzip_vary on;

worker_processes for small For vps, setting it to 1 or 2 is also acceptable. That's enough.

For small vps, the above optimized configurations of nginx and php-fpm are beneficial.

Four, linux startup process optimization

Copy code The code is as follows:

# chkconfig --list |grep on

View Processes that are started at startup and shut down unnecessary processes. If you encounter something you don’t know, it’s best to check it first before deciding whether to close it.

The above is the detailed content of How to optimize nginx small and medium vps. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete