Nginx反向代理给另一台网站服务器加速

服务器配置:1核512M

服务器系统:Ubuntu-24.04.1-x64

假设:

  • 服务器A(入口/加速)
    • 香港500Mbps
    • IP:500M服务器IP
    • 系统:Ubuntu 22.04/24.04
  • 服务器B(源站)
    • 日本30Mbps
    • IP:日本服务器IP
    • 已经运行自己的网站

最终架构:

大陆用户
    |
    ↓
500M服务器 (Nginx反向代理+缓存)
    |
    ↓
日本服务器 (真实网站)

一、准备工作

1. 修改DNS

假设网站域名:

www.example.com

现在:

www.example.com → 解析→B服务器IP

改成:

www.example.com → 解析→A服务器IP

等待DNS生效。

可以测试:

ping www.example.com

确认解析到了500M服务器。


二、A服务器安装Nginx

SSH登录A服务器:

ssh root@A服务器IP

更新:

apt update && apt upgrade -y

安装:

apt install nginx -y

启动

systemctl start nginx
systemctl enable nginx

查看:

systemctl status nginx

浏览器访问:

http://A服务器IP

看到 nginx 页面说明成功。


三、配置反向代理

删除默认配置:

rm /etc/nginx/sites-enabled/default

创建网站配置:

vim /etc/nginx/sites-available/example.com

写入:

server {

    listen 80;
    server_name example.com www.example.com;


    location / {

        proxy_pass http://B服务器IP;


        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_connect_timeout 30s;
        proxy_read_timeout 60s;


    }

}

修改演示:

server {

    listen 80; # 如果网站你的网站端口用的是443,就把80端口改成443
    server_name example.com www.example.com; # 改成自己的一级域名和二级域名


    location / {

        proxy_pass http://B服务器IP; # 改成自己的B服务器IP,如果你已经使用https了就改成(https://B服务器IP);

        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_connect_timeout 30s;
        proxy_read_timeout 60s;


    }

}

启用:

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

检查:

nginx -t

正常输出:

syntax is ok
test is successful

重启nginx:

systemctl restart nginx

四、在A服务器申请SSL证书

1.安装Certbot

A服务器执行:

apt install certbot python3-certbot-nginx -y

2.申请证书

执行:

certbot --nginx -d hmknows.com -d www.heimaolk.site

过程中提示输入:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel):输入你的邮箱地址
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.8-July-06-2026.pdf. You must agree in
order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: 输入y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: 建议选择n

成功后一般会看到:

Congratulations! You have successfully enabled HTTPS

然后执行:

nginx -t

正常显示:

syntax is ok
test is successful

再重启:

systemctl restart nginx

最后查看网站是否能正常访问

 

五、开启 HTTP/2

先备份Nginx配置(必须)

cp /etc/nginx/sites-enabled/example.com /etc/nginx/sites-enabled/example.com.bak

恢复备份:

cp /etc/nginx/sites-enabled/example.com.bak /etc/nginx/sites-enabled/example.com

1. 编辑配置

vim /etc/nginx/sites-enabled/example.com

找到:

listen 443 ssl;

改成:

listen 443 ssl http2;

2. 测试

nginx -t

成功:

syntax is ok
test is successful

重启:

systemctl reload nginx

3. 检查

执行:

curl -I --http2 https://www.heimaolk.site

看到:

HTTP/2 200

成功

 

© 版权声明
THE END
喜欢就支持一下吧
点赞435 分享