centos

怎样优化CentOS Nginx性能

小樊
37
2025-11-23 21:01:16
栏目: 智能运维

CentOS 上优化 Nginx 性能的可落地方案

一 系统层优化

二 Nginx 配置优化

三 示例配置片段

# /etc/nginx/nginx.conf 关键片段
worker_processes auto;
worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections 65535;
    multi_accept on;
    accept_mutex on;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    keepalive_timeout 60;
    keepalive_requests 1000;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    open_file_cache          max=102400 inactive=20s;
    open_file_cache_valid    30s;
    open_file_cache_min_uses 1;

    server_tokens off;

    # 静态资源缓存
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
        add_header Cache-Control "public";
    }

    # 示例:上游长连接(按需)
    # upstream backend {
    #     server 10.0.0.11:8080;
    #     server 10.0.0.12:8080;
    #     keepalive 32;
    # }
    #
    # server {
    #     location / {
    #         proxy_pass http://backend;
    #         proxy_http_version 1.1;
    #         proxy_set_header Connection "";
    #         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;
    #     }
    # }
}

上述片段覆盖了进程、连接、传输、压缩、缓存与上游长连接等关键优化点,可直接按需合并到现有配置中。

四 验证与上线

0
看了该问题的人还看了