前言

记录 Nginx 常用的配置,便于查找。

推荐使用 Nginx-Proxy-Manager,图形化界面操作更简单方便,同时支持免费 SSL 及自动续签。

Nginx 配置

nginx
nginx.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
user www-data;  # 运行 Nginx 的用户
worker_processes auto;  # 自动根据 CPU 核心数设置工作进程数
pid /run/nginx.pid;  # PID 文件位置

events {
    worker_connections 1024;  # 每个 worker 进程允许的最大连接数
}

http {
    include /etc/nginx/mime.types;  # MIME 类型配置
    default_type application/octet-stream;  # 默认 MIME 类型

    sendfile on;  # 开启高效文件传输
    tcp_nopush on;
    tcp_nodelay on;

    keepalive_timeout 65;  # 连接保持时间
    gzip on;  # 启用 gzip 压缩

    server {
        listen 80;  # 监听端口
        server_name example.com;  # 服务器名称或域名

        location / {
            root /var/www/html;  # 网站根目录
            index index.html index.htm;  # 默认首页文件
        }

        error_page 500 502 503 504 /50x.html;  # 错误页面
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    }
}

反向代理配置

nginx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:8080;  # 代理到后端服务
        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;
    }
}

基于上述基础配置,支持更多功能可以参考以下:

支持端口号

修改代理头 Host,使用 http_host 替代 host

nginx
1
proxy_set_header Host $http_host;

支持 “保持登录” 功能

在 Web 应用中,用户登录成功后,服务器通常会通过 Set-Cookie 响应头发送一个会话 Cookie 给客户端。这个 Cookie 在后续的请求中会被浏览器自动附加到请求头中,以标识用户身份

添加如下配置:

nginx
1
2
proxy_pass_request_headers on;
proxy_cookie_path / "/; HTTPOnly; Secure";

支持 WebSocket

添加如下配置:

nginx
1
2
proxy_set_header Upgrade $http_upgrade; 
proxy_set_header Connection "upgrade"; 

支持大文件上传

添加如下配置:

nginx
1
2
3
4
5
proxy_request_buffering off; # 直接将文件流式传输到后端服务器,而不是在 Nginx 中进行缓存
client_max_body_size 200G;   # 设置为允许 200GB 大小的上传文件
proxy_connect_timeout 600s;  # 设置连接超时时间为 600 秒
proxy_read_timeout 600s;     # 设置读取响应的超时时间为 600 秒
proxy_send_timeout 600s;     # 设置发送请求的超时时间为 600 

负载均衡配置

nginx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
http {
    upstream backend {
        server backend1.example.com;  # 后端服务器 1
        server backend2.example.com;  # 后端服务器 2
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;  # 负载均衡到 backend 组
        }
    }
}

SSL 配置

nginx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/nginx/ssl/example.com.crt;  # 证书文件
    ssl_certificate_key /etc/nginx/ssl/example.com.key;  # 私钥文件

    ssl_protocols TLSv1.2 TLSv1.3;  # 启用的 SSL 协议
    ssl_ciphers HIGH:!aNULL:!MD5;  # 加密算法
    ssl_prefer_server_ciphers on;

    location / {
        root /var/www/html;
        index index.html index.htm;
    }
}

# HTTP 到 HTTPS 的重定向
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;  # 强制重定向到 HTTPS
}

缓存配置

nginx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
http {
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
            proxy_cache my_cache;  # 使用缓存
            proxy_cache_valid 200 302 10m;  # 缓存 200 和 302 响应,缓存时间为 10 分钟
            proxy_cache_valid 404 1m;  # 缓存 404 响应,缓存时间为 1 分钟
        }
    }
}

静态资源配置

nginx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
server {
    listen 80;
    server_name example.com;

    location / {
        root /var/www/html;  # 网站根目录
        index index.html;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {  # 匹配静态文件
        expires 30d;  # 设置缓存过期时间为 30 天
    }
}

日志配置

nginx
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
server {
    listen 80;
    server_name example.com;

    access_log /var/log/nginx/access.log main;  # 访问日志
    error_log /var/log/nginx/error.log warn;  # 错误日志

    location / {
        root /var/www/html;
        index index.html;
    }
}