插件窝 干货文章 Linux下如何配置Nginx反向代理

Linux下如何配置Nginx反向代理

Nginx proxy server 配置 444    来源:    2025-04-23

Linux下配置Nginx反向代理指南

Nginx反向代理是一种常见的服务器配置方式,可以将客户端请求转发到后端服务器,同时隐藏后端服务器的真实信息。以下是详细的配置步骤:

1. 安装Nginx

在大多数Linux发行版中,可以使用包管理器安装:

# Ubuntu/Debian
sudo apt update
sudo apt install nginx

# CentOS/RHEL
sudo yum install epel-release
sudo yum install nginx

# 启动Nginx并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx

2. 基本反向代理配置

编辑Nginx配置文件(通常在/etc/nginx/nginx.conf/etc/nginx/conf.d/default.conf):

server {
    listen 80;
    server_name yourdomain.com;  # 替换为你的域名或IP

    location / {
        proxy_pass http://backend_server_ip:backend_port;  # 后端服务器地址
        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;
    }
}

3. 常用配置选项

3.1 负载均衡

upstream backend_servers {
    server backend1.example.com:8080 weight=3;
    server backend2.example.com:8080;
    server backend3.example.com:8080;

    # 可选负载均衡方法
    # least_conn;  # 最少连接
    # ip_hash;     # 基于IP的会话保持
}

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://backend_servers;
        # 其他proxy设置...
    }
}

3.2 WebSocket支持

location /ws/ {
    proxy_pass http://backend_server;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;  # 长连接超时时间
}

3.3 SSL/TLS终止

server {
    listen 443 ssl;
    server_name yourdomain.com;

    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    # SSL优化配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256...';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    location / {
        proxy_pass http://backend_server;
        # 其他proxy设置...
    }
}

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

4. 高级配置

4.1 缓存配置

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m use_temp_path=off;

server {
    # ...
    location / {
        proxy_cache my_cache;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_background_update on;
        proxy_cache_lock on;

        proxy_pass http://backend_server;
    }
}

4.2 健康检查

upstream backend {
    server backend1.example.com:8080;
    server backend2.example.com:8080;

    # 被动健康检查
    server backend3.example.com:8080 max_fails=3 fail_timeout=30s;

    # 主动健康检查(需要Nginx Plus或开源版+第三方模块)
    # health_check interval=5s fails=3 passes=2 uri=/health;
}

5. 测试与重载配置

配置完成后,测试Nginx配置是否正确:

sudo nginx -t

如果测试通过,重新加载Nginx配置:

sudo systemctl reload nginx
# 或
sudo service nginx reload

6. 日志查看

查看Nginx访问日志和错误日志:

# 访问日志
tail -f /var/log/nginx/access.log

# 错误日志
tail -f /var/log/nginx/error.log

常见问题解决

  1. 502 Bad Gateway错误

    • 检查后端服务器是否运行
    • 检查防火墙是否允许Nginx访问后端端口
    • 检查proxy_pass地址是否正确
  2. 413 Request Entity Too Large

    • 在Nginx配置中添加client_max_body_size 100M;(根据需要调整大小)
  3. 504 Gateway Timeout

    • 增加代理超时时间:proxy_connect_timeout 60s; proxy_read_timeout 60s;

通过以上配置,你可以建立一个功能完善的Nginx反向代理服务器,根据实际需求调整各项参数。