kenen
kenen

Reputation: 397

Nginx proxy https to http on non standard port?

I have nginx configured to proxy https traffic to an http server running on the same machine.

Everything works fine when I configure nginx to listen on / proxy from https port 443. But I really want to listen on a non standard port. When I configure a non standard port, nginx receives the request and sends it to my http server, as it should, but the server is responding with an HTTP redirect back to the browser that tells it to redirect to 'https://server.com/someurl". I mean the redirect url looks good except for it's missing the correct port. Am I missing an HTTP header that I need to be setting in the proxy?

Specifically I'm running an http instance of Tracks; http://getontracks.org. If it matters.

My (working on standard port) nginx server configuration:

location /{
    proxy_pass http://localhost:50000;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
    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 https;
    proxy_redirect off;
}

Upvotes: 12

Views: 17393

Answers (2)

user1480400
user1480400

Reputation:

The issue stems from the line -

proxy_set_header Host $host;

Your web server(WEBrick) in turn is including this when issuing the redirect response.

You can change it to include the non-standard port -

proxy_set_header Host $host:$server_port;

which should resolve this.

Upvotes: 29

user507077
user507077

Reputation:

This is more an issue of the application running on the upstream host (Tracks), not of the web server configuration (unless your upstream web server configuration contains rewrite rules, of course). Different applications handle these situations differently: some require an explicit configuration parameter telling them the actual URL the application can be reached at, some use certain HTTP headers, some use other HTTP headers.

As Tracks is a Rails application you might end up getting more knowledgeable answers if you add a corresponding tag to your question.

Upvotes: 1

Related Questions