是否可以仅将某些对真实网站的请求重定向到本地主机上的网络服务器?

问题描述 投票:0回答:1

我已经开始在我的 Windows 11 机器上试验 nginx。 我想要的是以下内容:

  • 给出的是一些真实的网站。
  • 所有以 realwebsite.com/magic 开头的请求都应该转到 localhost:3000
  • 但是以 realwebsite.com 开头的所有其他请求都应该转到真实网站

我尝试过的:

  • 首先,我在 localhost:3000 上设置了一个服务器并确认它正在工作。
  • 编辑了 C:\Windows\System32\drivers tc 中的主机文件并添加了 127.0.0.1 realwebsite.com
  • 编辑 nginx.conf 添加:

`服务器{ 听80; 服务器名称 realwebsite.com;

    # send magic requests to localhost server
location /magic{
    proxy_pass http://localhost:3000;
    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;
}

    # send all other requests to the real website       
location / {
    proxy_pass https://www.realwebsite.com;
    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;
}       

}`

预期行为:

当我访问 realwebsite.com/magic 时,浏览器应该显示一些 test.html 文件

实际行为:

无法访问站点,并且在 localhost:3000 上运行的服务器在输入 realwebsite.com/magic 时不会记录任何 get 请求 但是当我直接访问 localhost:3000 时,服务器确实记录了 get 请求

node.js nginx
1个回答
0
投票

听起来 Nginx 可能正在本地计算机以外的其他地方运行。

由于您使用 Nginx 作为代理(而不是重定向),Nginx 本身需要访问

proxy_pass
主机。如果 Nginx 没有在本地运行,则它无法访问
localhost

这样想:

Request: Browser => Nginx => http://localhost:3000
Response: Browser <= Nginx <= http://localhost:3000

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.