V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
vsean
V2EX  ›  问与答

求教关于 NGINX 反向代理的问题

  •  
  •   vsean · 316 天前 · 765 次点击
    这是一个创建于 316 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在的网站在 B VPS 上,但是由于 B VPS 到国内的访问速度很慢

    又增加了一台 A 服务器,A 服务器到国内的速度很快

    A B 在同一个机房,相互访问速度很快,访问延迟在 1ms 以内

    能否把域名解析到 A 服务器,通过 nginx 反向代理到 B 上,以加快访问速度

    无法直接把网站更改到 A VPS 上

    感谢解答

    7 条回复    2023-06-19 18:51:16 +08:00
    LLaMA2
        1
    LLaMA2  
       316 天前   ❤️ 1
    不需要,只需要吧 A 机器的 80 端口直接转发到 B 机器的 80 端口,然后域名解析给 A ,其他配置保持不动!
    Arumoh
        2
    Arumoh  
       316 天前   ❤️ 1
    可以,直接反向代理就行
    vsean
        3
    vsean  
    OP
       316 天前
    @ye4tar #1 443 直接转发也可以吗
    jifengg
        4
    jifengg  
       316 天前   ❤️ 1
    @vsean proxy_pass 后面可不仅仅只能跟 127.0.0.1 ,而是任何 ip 都可以的。
    所以,域名解析到 A ,A 上的 nginx proxy_pass 到 B 的端口即可( B 都可以不用 80 端口)
    vsean
        5
    vsean  
    OP
       316 天前
    http {
    server {
    listen 443 ssl;
    server_name your_domain.com;
    ssl_certificate /path/to/your/certificate.crt;
    ssl_certificate_key /path/to/your/private.key;

    location / {
    proxy_pass http://B_server_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;
    }
    }
    }

    GPT 给出了这样的配置,不知道是否正确,一会尝试一下,可能还要加上 80 端口,感谢各位的解答
    LLaMA2
        6
    LLaMA2  
       316 天前
    @vsean 所有端口都可以,
    如果网站需要追踪远程访问的 IP ,你需要把 nginx 放在 A 上,
    如果没有追踪 IP 的需求,则 nginx 任意在 A 或 B 都可以,
    测试一下就好啦,明确的答案: 你的要求是可以满足的,不用怕,配置起来就可以啦
    vsean
        7
    vsean  
    OP
       316 天前
    server {
    listen 80;
    listen 443 ssl http2;
    ssl_certificate /usr/local/nginx/conf/ssl/domain.com.crt;
    ssl_certificate_key /usr/local/nginx/conf/ssl/domain.com.key;
    server_name domain.com www.domain.com;

    location / {
    proxy_pass 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;
    }
    }

    @ye4tar #1
    @Arumoh #2
    @jifengg #4

    按照如上配置,设置好了,感谢各位的解答
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   884 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:32 · PVG 03:32 · LAX 12:32 · JFK 15:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.