什么是反向代理?

正向代理:客户端→代理→目标网站(如翻墙)
反向代理:用户→代理→后端服务器(如Nginx转发请求到后端)

Nginx最常见的用途:

  • 负载均衡(多台服务器分流)
  • 反向代理(隐藏后端服务器)
  • SSL证书(HTTPS)
  • 静态文件服务

安装Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
# Ubuntu/Debian
apt update
apt install -y nginx

# CentOS/RHEL
yum install -y nginx

# 启动
systemctl start nginx
systemctl enable nginx

# 验证
nginx -v

浏览器访问 http://服务器IP 看到 Nginx 欢迎页。


配置文件位置

1
2
3
4
/etc/nginx/nginx.conf                    # 主配置文件
/etc/nginx/conf.d/*.conf # 额外配置
/etc/nginx/sites-available/*.conf # 站点配置
/etc/nginx/sites-enabled/*.conf # 启用的站点(软链接)

基础反向代理

配置示例

编辑 /etc/nginx/conf.d/proxy.conf

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

location / {
proxy_pass http://127.0.0.1:8080; # 转发到本地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;
}
}

重启Nginx:

1
2
nginx -t                     # 测试配置
systemctl reload nginx # 重载配置(不中断服务)

访问 http://example.com 自动转发到 http://127.0.0.1:8080


负载均衡

轮询策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
upstream backend {
server 192.168.1.10:80;
server 192.168.1.11:80;
server 192.168.1.12:80;
}

server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend; # 转发到upstream
}
}

请求会轮流发给3台服务器。

权重分配

1
2
3
4
5
upstream backend {
server 192.168.1.10:80 weight=3; # 权重3,接收50%请求
server 192.168.1.11:80 weight=2; # 权重2,接收33%请求
server 192.168.1.12:80 weight=1; # 权重1,接收17%请求
}

IP哈希(同一IP访问同一台服务器)

1
2
3
4
5
upstream backend {
ip_hash; # 开启IP哈希
server 192.168.1.10:80;
server 192.168.1.11:80;
}

适用于需要保持会话的场景。

健康检查

1
2
3
4
5
upstream backend {
server 192.168.1.10:80 max_fails=3 fail_timeout=30s;
server 192.168.1.11:80 max_fails=3 fail_timeout=30s;
server 192.168.1.12:80 backup; # 备用服务器(前面都挂了才用)
}
  • max_fails=3:失败3次标记为不可用
  • fail_timeout=30s:30秒后重试

HTTPS配置(SSL证书)

使用自签名证书(测试用)

1
2
3
4
# 生成证书
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/nginx/ssl/key.pem \
-out /etc/nginx/ssl/cert.pem

配置:

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

ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;

location / {
proxy_pass http://127.0.0.1:8080;
}
}

# HTTP自动跳转HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}

使用Let’s Encrypt免费证书

1
2
3
4
5
6
7
8
# 安装certbot
apt install -y certbot python3-certbot-nginx

# 自动配置HTTPS
certbot --nginx -d example.com

# 自动续期
certbot renew --dry-run

静态文件服务

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

root /var/www/static;
index index.html;

# 开启gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript;

# 缓存设置
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
}

反向代理常见配置

websocket支持

1
2
3
4
5
6
location /chat {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

上传文件大小限制

1
client_max_body_size 100M;                  # 最大上传100MB

超时设置

1
2
3
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;

缓存后端响应

1
2
3
4
5
6
7
8
9
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m;

server {
location / {
proxy_pass http://backend;
proxy_cache my_cache;
proxy_cache_valid 200 5m; # 缓存200响应5分钟
}
}

实战场景

场景1:部署前端+后端

1
2
3
4
5
6
7
8
9
10
11
12
13
# 前端静态文件
server {
listen 80;
server_name example.com;

root /var/www/frontend;
index index.html;

# 后端API代理
location /api/ {
proxy_pass http://127.0.0.1:3000;
}
}

场景2:多个域名转发不同服务

1
2
3
4
5
6
7
8
9
10
11
12
13
# blog.example.com
server {
listen 80;
server_name blog.example.com;
proxy_pass http://127.0.0.1:3000;
}

# api.example.com
server {
listen 80;
server_name api.example.com;
proxy_pass http://127.0.0.1:5000;
}

场景3:防止后端IP暴露

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

# 隐藏Nginx和后端服务器信息
server_tokens off;

location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

性能优化

worker进程数

编辑 /etc/nginx/nginx.conf

1
2
worker_processes auto;                    # 自动匹配CPU核心数
worker_connections 1024; # 每个worker最大连接数

开启HTTP/2

1
listen 443 ssl http2;

启用缓存

1
2
3
4
# 打开文件缓存
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;

查看日志

1
2
3
4
5
6
7
8
# 访问日志
tail -f /var/log/nginx/access.log

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

# 特定站点日志
tail -f /var/log/nginx/example.com.access.log

常见问题

Q: 配置修改后不生效?
A: 检查语法:nginx -t,然后重载:systemctl reload nginx

Q: 502 Bad Gateway?
A: 后端服务没启动,或端口不正确。

Q: 如何屏蔽特定IP?
A:

1
2
3
deny 192.168.1.100;
deny all; # 允许白名单
allow 192.168.1.0/24;

Q: 如何限流?
A:

1
2
3
4
5
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;

location / {
limit_req zone=one burst=20;
}

总结

  • 反向代理:proxy_pass 转发请求到后端
  • 负载均衡:upstream 定义多台服务器,weight 调整权重
  • HTTPS:ssl_certificate 配置证书,Let’s Encrypt免费申请
  • 静态文件:root 指定目录,gzip 开启压缩,expires 设置缓存
  • nginx -t 测试配置,systemctl reload nginx 重载
  • 日志:/var/log/nginx/access.logerror.log
  • 性能优化:worker_processes auto,开启HTTP/2,文件缓存

下一篇:Apache+PHP动态网站搭建。