28

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.

1 Answer 1

31

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/;
        }
}
Sign up to request clarification or add additional context in comments.

10 Comments

it didnt work. but there are some mistakes in your config above. I had to shift the upstream block out of the server block, and remove the 'http://' prefix for the '127.0.0.1:36000' portion. There is an inner server block and I had to remove the 'server{' and '}' portion as well. But yea it didnt work out... any ideas why?
Ahh, sorry I made a typo. the second server block shouldn't exist. editing now.
Also, try using the proxy_redirect statement. proxy_redirect http://localhost:36000/ http://$server_name/
Still doesnt work. Now my config looks like: upstream backend { server 127.0.0.1:36000; } server { listen 80; server_name domain.somehost.com; location / { proxy_pass backend; proxy_redirect 127.0.0.1:36000 http://$server_name/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
@bibstha Good catch, I've edited my answer. It works with $http_host, but technically it should use $host to strip the port number etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.