为什么我的 Nginx 反向代理 node.js+express 服务器重定向到 0.0.0.0?

发布于 2024-11-16 05:14:20 字数 1075 浏览 0 评论 0原文

我有一台服务器,配置为通过 Ngnix 前端在多个域上托管多个 node.js+express 应用程序。一切都很好,除了从快速路由进行重定向调用时:

res.redirect('/admin');

然后客户端浏览器被重定向到 http://0.0 .0.0:8090

看来这一定是来自express的重定向标头的问题,但为了以防万一它是相关的,这里是相关域的 nginx.conf 文件:

server {
    listen 0.0.0.0:80;
    server_name  *.example.com;

    access_log  /var/log/nginx_example_access.log;
    error_log  /var/log/nginx_example_error.log debug;

    # proxy to node
    location / {
        proxy_pass         http://0.0.0.0:8090/;
        proxy_redirect     off;

        proxy_set_header   Host             $proxy_host;
        proxy_max_temp_file_size 0;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}

I have a server configured to host multiple node.js+express apps on multiple domains through an Ngnix frontend. Everything works great, except for when calls to redirect are made from an express route:

res.redirect('/admin');

Then the client browser is redirected to http://0.0.0.0:8090

It seems like it must be an issue with the redirection headers coming out of express, but just in case it's relevant, here's the nginx.conf file for the domain in question:

server {
    listen 0.0.0.0:80;
    server_name  *.example.com;

    access_log  /var/log/nginx_example_access.log;
    error_log  /var/log/nginx_example_error.log debug;

    # proxy to node
    location / {
        proxy_pass         http://0.0.0.0:8090/;
        proxy_redirect     off;

        proxy_set_header   Host             $proxy_host;
        proxy_max_temp_file_size 0;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2024-11-23 05:14:20

解决了。我的 nginx conf 文件中有一个问题,导致 node/express 接收到错误的请求标头。当相对路径传递到 res.redirect 时,它会从传入的 req 对象中提取主机并将其设置在响应标头中。

        proxy_set_header   Host             $proxy_host;

应该是

        proxy_set_header   Host             $host;

$proxy_host上游主机地址 0.0.0.0:port

$host 是传入请求 - header Host example.com


UPDATE

正如 Louis Chatriot 在评论中指出的那样,较新版本的 Nginx 已将 $host 替换为 $http_host,在之前的版本返回 example.com:port 但现在返回 example.com

Solved. I had a problem in my nginx conf file that was causing node/express to receive the wrong request-header. When a relative path is passed into res.redirect, it pulls the Host from the incoming req object and sets it in the response-header.

        proxy_set_header   Host             $proxy_host;

should have been

        proxy_set_header   Host             $host;

$proxy_host is the upstream host address 0.0.0.0:port

$host is the incoming request-header Host example.com


UPDATE

As Louis Chatriot points out in the comments, newer versions of Nginx have replaced $host with $http_host, which in previous versions returns example.com:port but now returns example.com.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文