Nginx Not Listening to Port 80

nginx starting but isn't listening on port 80

In the process of writing the question and reproducing the steps I figured out what the problem was.

I had created and edited my sites-available server in /etc/nginx/sites-available/mysite.tld

Then to enable the site, I was in the directory /etc/nginx/ when in my tired state created the link into the sites-enabled folder by running ln -s sites-available/mysite.tld sites-enabled/mysite.tld.

It wasn't until this morning that I realized I had created a bad link when I couldn't vim into the sites-enabled linked version.

Doing an rm on the bad link and then using absolute paths for the whole thing ln -s /etc/nginx/sites-available/mysite.tld /etc/nginx/sites-enabled/mysite.tld then a subsequent restart of nginx now shows it listening on port 80 as expected.

tl-dr; sometimes it's best to just go to bed and look at it in the morning

nginx doesn't listen on port 80 twice?

When you specify default_server flag on a listen directive, nginx will use that server block to serve any request where HTTP Host header does not match the server_name in any other server blocks (or the request missing Host header at all). You can use default_server flag on any particular IP:port combination given as parameter of listen directive only once. But you can use this flag several times on different IP:port combinations.

Some examples, assuming your server have several network interfaces:

server {
listen 1.2.3.4:80 default_server; # this will be default server block for any request coming to 1.2.3.4 IP address on port 80
...
}
server {
listen 5.6.7.8:80 default_server; # this will be default server block for any request coming to 5.6.7.8 IP address on port 80
...
}
server {
listen 80 default_server; # this will be default server block for any request coming to any other IP address (except 1.2.3.4 and 5.6.7.8) on port 80
...
}

Working example:

Typically, when I need to serve several sites on the same server, I use the following configuration (of course this is simplified, nowadays we typically use HTTPS with http://example.com to https://example.com redirection):

Site 1 config file

server {
listen 80;
server_name example1.com www.example1.com;
...
}

Site 2 config file

server {
listen 80;
server_name example2.com www.example2.com;
...
}

Default server config file

server {
listen 80 default_server;
server_name _;
return 444;
}

Because of default_server flag on listen directive third server block is used when the request's Host header doesn't contain one of my sites names (or the request does not have Host header at all). I don't expect visitors that doesn't know what site they are visiting (these are typically port scanners, vulnerability searchers etc.), so I use special nginx 444 code (close connection without any response) for them.

Nginx is listening on port 80 or 443 but not responding

It turns out that the DNS configuration went mad and pointed my domain to somewhere else. I fixed the DNS so the site will come back soon.

thanks



Related Topics



Leave a reply



Submit