nginx reverse proxy to backend running on localhost

yanhan picture yanhan · May 21, 2013 · Viewed 39.9k times · Source

EDIT: It turns out that the my setup below actually works. Previously, I was getting redirections to port 36000 but it was due to some configuration settings on my backend application that was causing it.

I am not entirely sure, but I believe I might be wanting to set up a reverse proxy using nginx.

I have an application running on a server at port 36000. By default, port 36000 is not publicly accessible and my intention is for nginx to listen to a public url, direct any request to the url to an application running on port 36000. During this entire process, the user should not know that his/her request is being sent to an application running on my server's port 36000.

To put it in more concrete terms, assume that my url is http://domain.somehost.com/

Upon visiting http://domain.somehost.com/ , nginx should pick up the request and redirect it to an application already running on the server on port 36000, the application does some processing, and passes the response back. Port 36000 is not publicly accessible and should not appear as part of any url.

I've tried a setup that looks like:

server {
    listen 80;
    server_name domain.somehost.com
    location / {
        proxy_pass http://127.0.0.1:36000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

and including that inside my main nginx.conf

However, it requires me to make port 36000 publicly accessible, and I'm trying to avoid that. The port 36000 also shows up as part of the forwarded url in the web browser.

Is there any way that I can do the same thing, but without making port 36000 accessible?

Thank you.

Answer

Intermernet picture Intermernet · May 21, 2013

EDIT: The config below is from a working nginx config, with the hostname and port changed.

You need to may be able to set the server listening on port 36000 as an upstream server (see http://nginx.org/en/docs/http/ngx_http_upstream_module.html).

server {
        listen   80;
        server_name domain.somehost.com;

        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:36000/;
                proxy_redirect http://localhost:36000/ https://$server_name/;
        }
}