我已经开始在我的 Windows 11 机器上试验 nginx。 我想要的是以下内容:
`服务器{ 听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 请求
听起来 Nginx 可能正在本地计算机以外的其他地方运行。
由于您使用 Nginx 作为代理(而不是重定向),Nginx 本身需要访问
proxy_pass
主机。如果 Nginx 没有在本地运行,则它无法访问 localhost
。
这样想:
Request: Browser => Nginx => http://localhost:3000
Response: Browser <= Nginx <= http://localhost:3000
希望有帮助。