-
安装Nginx作为反向代理
- 使用包管理器安装Nginx:
sudo apt update && sudo apt install nginx
- 启动并确保Nginx运行:
sudo systemctl start nginx sudo systemctl enable nginx
- 使用包管理器安装Nginx:
-
配置Nginx
- 打开Nginx配置文件(通常在
/etc/nginx/sites-available/default或/etc/nginx/sites-enabled):sudo nano /etc/nginx/sites-available/default
- 修改
server块,确保监听80和443端口:server { listen 80; listen [::]:80; server_name your_domain.com; root /var/www/html; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { proxy_pass http://localhost:300; 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; } } server { listen 443; listen [::]:443; server_name your_domain.com; root /var/www/html; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { proxy_pass http://localhost:300; 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; } } - 保存并退出编辑器。
- 打开Nginx配置文件(通常在
-
启用Nginx配置文件
- 确保创建了必要的链接:
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
- 确保创建了必要的链接:
-
设置防火墙规则
- 使用firewalld(如果是RHEL/CentOS)或iptables( Debian/Ubuntu)配置防火墙:
- firewalld:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --permanent --add-port 80/tcp sudo firewall-cmd --permanent --add-port 443/tcp sudo firewall-cmd --reload
- iptables:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT sudo service iptables save sudo service iptables restart
- firewalld:
- 使用firewalld(如果是RHEL/CentOS)或iptables( Debian/Ubuntu)配置防火墙:
-
配置加速器服务器
- 确保加速器服务(如Node.js或Python应用)已经安装并在
localhost:300上运行。 - 在Nginx配置中,将
proxy_pass指向加速器服务的内部地址。
- 确保加速器服务(如Node.js或Python应用)已经安装并在
-
申请SSL证书
- 使用Let's Encrypt获取免费证书:
sudo apt install certbot sudo certbot --register --nginx
- 根据提示生成证书文件,通常位于
/etc/letsencrypt/live/your_domain.com/。
- 使用Let's Encrypt获取免费证书:
-
重启Nginx以应用新配置
sudo systemctl restart nginx
-
测试连接
- 使用浏览器访问
https://your_domain.com,确保网页加载正常。
- 使用浏览器访问
-
监控和维护
- 定期检查Nginx日志和加速器服务状态。
- 使用
sudo journalctl -u nginx查看Nginx日志。
通过以上步骤,您可以成功配置加速器服务器的连接服务,确保外部请求被正确反向代理并加速。




