什么是反向代理?
正向代理:客户端→代理→目标网站(如翻墙)
反向代理:用户→代理→后端服务器(如Nginx转发请求到后端)
Nginx最常见的用途:
- 负载均衡(多台服务器分流)
- 反向代理(隐藏后端服务器)
- SSL证书(HTTPS)
- 静态文件服务
安装Nginx
1 2 3 4 5 6 7 8 9 10 11 12 13
| apt update apt install -y nginx
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; 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; } }
|
请求会轮流发给3台服务器。
权重分配
1 2 3 4 5
| upstream backend { server 192.168.1.10:80 weight=3; server 192.168.1.11:80 weight=2; server 192.168.1.12:80 weight=1; }
|
IP哈希(同一IP访问同一台服务器)
1 2 3 4 5
| upstream backend { ip_hash; 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; } }
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
| apt install -y certbot python3-certbot-nginx
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 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;
|
超时设置
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; } }
|
实战场景
场景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;
location /api/ { proxy_pass http://127.0.0.1:3000; } }
|
场景2:多个域名转发不同服务
1 2 3 4 5 6 7 8 9 10 11 12 13
| server { listen 80; server_name blog.example.com; proxy_pass http://127.0.0.1:3000; }
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;
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; worker_connections 1024;
|
开启HTTP/2
启用缓存
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.log 和 error.log
- 性能优化:
worker_processes auto,开启HTTP/2,文件缓存
下一篇:Apache+PHP动态网站搭建。