Understanding network configuration for Vapor+nginx

xian asked:

I have my Vapor App MyApp running on localhost:8080 on my Ubuntu server and installed nginx to connect to it via <server-ip>:80.
It works, but I don’t understand the following part of it:

To get it to work I followed a small tutorial on how to host a Vapor app. A part of an nginx config file had to be set up like:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

I now should’ve been able to reach MyApp using <server-ip>:80, but this didn’t work. As MyApp starts with the prompt:

Server starting on http://localhost:8080

I changed the server block in the nginx config to:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    location / {
        proxy_pass http://localhost:8080; // Changed to localhost
    }
}

This worked and I am now able to reach MyApp!

netstat shows the following entries:

[...]:~# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address     Foreign Address  State     PID/Program name
tcp        0      0 0.0.0.0:80        0.0.0.0:*        LISTEN    1106/nginx: master
tcp        0      0 127.0.0.53:53     0.0.0.0:*        LISTEN    601/systemd-resolve
[...] // my ssh connection
tcp6       0      0 ::1:8080          :::*             LISTEN    8145/<MyApp>
tcp6       0      0 :::80             :::*             LISTEN    1106/nginx: master
  • So it seems my Vapor app runs on tcp6. I’m new to server development and thought of localhost as an equivalent to 127.0.0.1. Why did my changes make it work?
  • With my change to localhost, might I run into any other problems?

My answer:


The name localhost is equivalent to the IPv6 address ::1 in addition to the IPv4 address 127.0.0.1.

For the last … almost 20 years or so, IPv6 has been the preferred protocol over IPv4, so when an IPv6 address is available for a name it will be tried first.

This means that you should expect localhost connections to be made to ::1 on virtually every modern device, unless someone has gone to great lengths to disable IPv6. And on some operating systems such as Windows, IPv6 cannot be disabled anyway, so localhost is always expected to connect to ::1.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.