在LNMP(Linux, Nginx, MySQL, PHP)环境中优化Nginx配置是提升网站性能和稳定性的重要步骤。以下是一些常见的优化方法:
grep processor /proc/cpuinfo | wc -l
worker_processes auto; # 或者设置为CPU核心数
worker_cpu_affinity 0001 0010 0100 1000; # 适用于4核CPU
worker_rlimit_nofile 65535;
/etc/security/limits.conf
中设置:* soft nofile 65535
* hard nofile 65535
epoll
事件模型,并设置合适的worker_connections
。events {
use epoll;
worker_connections 65535;
multi_accept on;
}
sendfile
、tcp_nopush
等指令提高传输效率。http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
# 其他优化指令
}
server {
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
}
http {
client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;
}
http {
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 4 4k;
}
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html index.htm;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
通过以上优化配置,可以显著提升Nginx的性能和稳定性,从而提高整个LNMP环境的效率。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:LNMP中Nginx怎么配置