生产环境 nginx 推荐配置

本文提供了Nginx生产环境的推荐配置方案,主要包括性能优化和安全设置。配置中启用了自动工作进程、epoll事件模型、TCP优化参数,并调整了连接数、缓冲区大小等性能参数。安全方面包括隐藏版本号、HSTS预加载、SSL/TLS优化配置,以及XSS防护等安全头设置。同时提供了负载均衡配置示例和HTTPS重定向方案。该配置可根据实际需求调整,建议结合具体业务场景和服务器资源进行优化。

bash 复制代码
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

# 根据CPU核心数自动设置
worker_processes auto; 
# 增加文件描述符限制
worker_rlimit_nofile 65535;
# 工作进程优先级:-20为最高(范围-20~19),减少被系统调度打断的概率
worker_priority -10;
error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/conf/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.

events {
    # Linux下使用epoll
    use epoll;  
    # 单个工作进程最大连接数
    worker_connections 65535;
    # 允许一次接受多个新连接
    multi_accept on;  
    # 关闭负载均衡锁
    accept_mutex off;  
}

http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /usr/local/nginx/logs/access.log main;

    # 启用sendfile
    sendfile            on;
    # 启用TCP_NOPUSH
    tcp_nopush          on;
    # 启用TCP_NODELAY
    tcp_nodelay         on;
    
    keepalive_timeout   65;
    # 单个连接最大请求数
    keepalive_requests 1000;        
    types_hash_max_size 4096;
    client_max_body_size 100M;
    client_header_timeout 20s;
    client_body_timeout 20s;
    send_timeout 20s;
    
    # 缓冲区优化
    client_body_buffer_size 32k;
    client_header_buffer_size 4k;
    large_client_header_buffers 8 16k;
    output_buffers 2 32k;
    postpone_output 1460;
    
    # Gzip压缩优化
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_comp_level 6;
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/json
        application/javascript
        application/xml+rss
        application/atom+xml
        image/svg+xml;
    gzip_proxied any;
    gzip_buffers 16 8k;
    
    # 安全相关设置
    # 隐藏版本号
    server_tokens off;              
    # 重置超时连接
    reset_timedout_connection on;   
    
    include /usr/local/nginx/conf/mime.types;
    default_type application/octet-stream;

    # 定义一个名为 muses_gateway_backend 的后端服务器组
    upstream muses_gateway_backend {
        server 192.168.1.11:8080;
        server 192.168.1.12:8080;
        # 可选:配置负载均衡策略(默认是轮询)
        # weight: 设置权重,数值越大被分配的请求越多
        # server 192.168.1.11:8080 weight=3;
        # server 192.168.1.12:8080;

        # 可选:指定备用服务器,当主服务器都不可用时,流量会转向备用服务器
        # server 192.168.1.100:8080 backup;
    }

    # Settings for a TLS enabled server.
    #
    server {
        listen       80;
        server_name  xxx.xxx.com;

        # 启用 HSTS 预加载
        add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
    
        # 重定向到 HTTPS
        return 301 https://$server_name$request_uri;
    }

    # Settings for a TLS enabled server.
    #
    server {
        listen       443 ssl ;
        listen       [::]:443 ssl ;
        server_name  xxx.xxx.com;

        # ======================= 证书配置开始 =======================
        # 指定证书文件(中间证书可以拼接至该pem文件中),请将 /etc/ssl/cert/ssl.pem 替换为您实际使用的证书文件的绝对路径
        ssl_certificate /usr/local/nginx/cert/xxx.xxx.com.pem;
        # 指定私钥文档,请将 /etc/ssl/cert/ssl.key 替换为您实际使用的私钥文件的绝对路径
        ssl_certificate_key /usr/local/nginx/cert/xxx.xxx.com.key;
        # 配置 SSL 会话缓存,提高性能
        ssl_session_cache shared:SSL:1m;
        # 设置 SSL 会话超时时间
        ssl_session_timeout 5m;
        # 自定义设置使用的TLS协议的类型以及加密套件(以下为配置示例,请您自行评估是否需要配置)
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        # 指定允许的 TLS 协议版本,TLS协议版本越高,HTTPS通信的安全性越高,但是相较于低版本TLS协议,高版本TLS协议对浏览器的兼容性较差
        ssl_protocols TLSv1.2 TLSv1.3;
        # 优先使用服务端指定的加密套件
        ssl_prefer_server_ciphers on;
        # ======================= 证书配置结束 =======================
   
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        include /usr/local/nginx/conf/mime.types;

        # 接收并向下传递原始的源IP地址
        # 使用 proxy_protocol 或者通过 X-Forwarded-For 头部传递
        # 这里使用 X-Forwarded-For 方式(更常见)

        # 真实IP地址获取(如果前端有代理)
        # real_ip_header X-Forwarded-For;
        # 当设置为 on 时,Nginx 会从 X-Forwarded-For 头部的最后一个 IP 地址开始,向前查找第一个不在 set_real_ip_from 指定的可信网络范围内的 IP 地址,将其作为真实客户端 IP。
        # real_ip_recursive on;
        
        # 传递真实客户端IP地址给后端
        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;
        proxy_set_header Host $http_host;
        
        # 设置默认response的安全头(http和https站点通用)
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Frame-Options SAMEORIGIN;
        #add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-ancestors 'self'; object-src 'none';";
        # 下面两个header用于https站点
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
        add_header Referrer-Policy "no-referrer-when-downgrade";

        location = / {
            # 转到pcportal首页(适用于默认页不是/的场景)
            return 301 /home/;
        }

        location / {
            root   /usr/local/nginx/html/portal;
            index  index.html index.htm;
                                                                                                                                                                        
            try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;
        location = /40x.html {
        }
    
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

}

(END)

相关推荐
啵啵啵啵哲2 小时前
【输入法】Ubuntu 22.04 终极输入法方案:Fcitx5 + 雾凇拼音 (Flatpak版)
linux·运维·ubuntu
渝妳学C2 小时前
深度解析Linux中编译器gcc/g++
linux·运维
晴天¥3 小时前
Oracle中的概要文件
运维·数据库·oracle
杭州泽沃电子科技有限公司3 小时前
变压器安全,在线监测如何实现?
运维·在线监测·智能运维
0思必得03 小时前
[Web自动化] Requests模块基本使用
运维·前端·python·自动化·html·web自动化
mr_orange_klj4 小时前
关于负载均衡的负载均衡的AI问答(豆包)
运维·负载均衡
自己的九又四分之三站台4 小时前
docker安装pgvector、age和postgis
运维·docker·容器
忧郁蓝调264 小时前
Redis不停机数据迁移:基于 redis-shake 的跨实例 / 跨集群同步方案
运维·数据库·redis·阿里云·缓存·云原生·paas
吃螺丝粉4 小时前
zookeeper权限设置
linux·运维·服务器