Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Nginx reverse-proxy node.js+express server redirecting to 0.0.0.0?

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;
    }
}
like image 697
Daniel Mendel Avatar asked Jun 19 '11 21:06

Daniel Mendel


People also ask

What is reverse proxy redirect?

A reverse proxy resides in front of one or more Web servers and shields the origin server from client requests. Often, a reverse proxy cache is a front-end for all client requests to a server. An administrator assigns a reverse proxy cache to a specific origin server.

Can Nginx reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.

What is reverse proxy in node JS?

Reverse proxy is a proxy server which retrieve resources on behalf of client from one or more servers. Client end need not to know about all those servers. They request to proxy server on specific URL with over HTTP and proxy server finds out where to look ( in Servers ) to serve that request.

How do you redirect http to https in nginx explain with an example?

Redirect HTTP to HTTPS version for Specified domain in NginxServer_name domain-name.com www.domain-name.com – it specifies the domain names. So, replace it with your website domain name that you want to redirect. Return 301 https://domain-name.com$request_uri – it moves the traffic to the HTTPS version of the site.


1 Answers

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.

like image 188
Daniel Mendel Avatar answered Sep 29 '22 12:09

Daniel Mendel