Nginx – Configure nginx to forward external port to same internal port

nginxport-forwardingreverse-proxy

I'm running TileMill, which is listening to port 20008 and 20009, from localhost. I want 20009 to be only accessed through nginx on port 80 (with simple auth). I want 20008 to be accessible "directly" from the outside.

server {
   listen 80;
   server_name localhost;
   location / {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:20009;
        auth_basic "Restricted";
        auth_basic_user_file htpasswd;
    }
}
server {
   listen 20008;
   server_name localhost;
   location / {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:20008;
    }
}

The obvious problem is that nginx can't listen on port 20008 – it's already being used by TileMill.

Restarting nginx: nginx: [emerg] bind() to 0.0.0.0:20008 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:20008 failed (98: Address already in use)

Is there a way where nginx can listen externally without conflicting with the other service listening internally? (Changing localhost to the server's external IP didn't work.)

Best Answer

Change your listen statement to include your external IP, eg:

listen 1.2.3.4:20008;