hackpravj
By:
hackpravj

How to forward traffic from another port to port 80

August 2, 2014 12.1k views

I'm using nginx to serve my sinatra application with unicorn.
now as port 80 is already being used, I have setuped this app to listen on port 81.
I can access it via IP:81 but to make a DNS entry, I need to forward http traffic from 81 to 80.

How to do this or clear me if, I'm making a wrong approach.

2 Answers

You can configure nginx to reverse proxy connections to port 81. If you're going this route, it would make sense to allow access to your sinatra app through nginx only. Edit your app's config and set it to listen on "127.0.0.1:8888" or any unused port above 1024 (any program that needs to listen on ports < 1024 must run as root which is not recommended for a website) and add the following nginx server block:

server {
    listen 80;
    server_name yourapp.com; # or server_name subdomain.yourapp.com;

    location / {
        proxy_pass http://localhost:8888;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        # Enables WS support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
    }
}

Hi Kamal Nasser,
actually this results in

 * Restarting NGINX nginx                                                                                                                       nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

as my apache is using 80 already.

Have another answer? Share your knowledge.