什么是Nginx反向代理,为什么需要它?
Nginx是目前世界上使用最广泛的Web服务器和反向代理服务器之一。反向代理的核心作用是:Nginx监听服务器的80(HTTP)和443(HTTPS)端口,根据请求的域名(Host头)将流量转发到不同的后端服务。这样,一台香港云服务器或香港独立服务器就可以同时托管多个使用不同域名的网站,充分利用服务器资源,无需为每个网站购买独立服务器。
本文将从零开始,完整演示如何在Ubuntu服务器上配置Nginx反向代理,实现多个域名绑定到不同端口的后端服务,并自动申请和续期Let's Encrypt SSL证书,全程约30分钟可以完成。
第一步:安装Nginx
apt update && apt upgrade -y
apt install nginx -y
systemctl enable nginx && systemctl start nginx
nginx -v && systemctl status nginx
安装完成后,访问服务器IP,如果看到Nginx默认欢迎页面,说明安装成功。
第二步:理解Nginx配置文件结构
在Ubuntu上,Nginx配置文件组织如下:
/etc/nginx/
├── nginx.conf # 主配置文件
├── sites-available/ # 所有站点配置存放处
│ ├── default
│ ├── site1.conf
│ └── site2.conf
└── sites-enabled/ # 已启用的站点(软链接)
└── site1.conf -> ../sites-available/site1.conf
最佳实践是为每个网站创建独立配置文件,放在sites-available目录,然后用软链接启用。
第三步:创建Node.js应用的反向代理配置
假设有一个运行在3000端口的Node.js应用,域名为app1.yourdomain.com:
vim /etc/nginx/sites-available/app1.conf
写入以下内容:
server {
listen 80;
listen [::]:80;
server_name app1.yourdomain.com;
access_log /var/log/nginx/app1_access.log;
error_log /var/log/nginx/app1_error.log;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
}
}
第四步:创建WordPress网站配置
vim /etc/nginx/sites-available/wordpress.conf
写入以下内容:
server {
listen 80;
listen [::]:80;
server_name blog.yourdomain.com;
root /var/www/wordpress;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
expires 1y;
add_header Cache-Control "public, no-transform";
}
location ~ /\. {
deny all;
}
}
第五步:启用站点配置并验证
# 创建软链接启用站点
ln -s /etc/nginx/sites-available/app1.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/
# 检查配置语法(必须无错误)
nginx -t
# 重载Nginx(不中断现有连接)
systemctl reload nginx
第六步:申请Let's Encrypt SSL证书
申请前确保域名DNS已解析到服务器IP,且80端口可以正常访问:
apt install certbot python3-certbot-nginx -y
# 为各域名申请证书(Certbot自动修改Nginx配置)
certbot --nginx -d app1.yourdomain.com
certbot --nginx -d blog.yourdomain.com
# 一次申请主域名和www
certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot会自动在Nginx配置中添加SSL指令,并配置HTTP到HTTPS的自动跳转,证书申请过程全自动。
第七步:配置SSL证书自动续期
# 验证自动续期配置
certbot renew --dry-run
# 查看续期计时器
systemctl status certbot.timer
# 手动添加cron(保险起见)
echo "0 3 * * * certbot renew --quiet && systemctl reload nginx" >> /etc/crontab
第八步:Nginx全局性能优化
修改 /etc/nginx/nginx.conf 主配置文件加入优化参数:
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
use epoll;
multi_accept on;
}
http {
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript image/svg+xml;
client_max_body_size 100M;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
}
常见问题排查
502 Bad Gateway:后端服务未运行或端口错误。检查:systemctl status 你的服务 和 ss -tlnp | grep 3000。
证书申请失败:域名DNS未生效或80端口被防火墙拦截。检查:dig 域名 和 ufw status。
显示默认Nginx页面:软链接未创建或配置语法错误。检查:ls /etc/nginx/sites-enabled/ 和 nginx -t。
总结
通过Nginx反向代理,一台香港云服务器可以托管多个不同域名的网站,充分利用服务器资源。如果网站流量增长,需要更多资源,随时升级到香港独立服务器,所有Nginx配置完全兼容,迁移成本极低。域名可以在IDC.Net域名注册购买(20元起,含Whois隐私保护),管理界面简洁,DNS修改即时生效。
