Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx doesn't listen on port 80 twice?

Edit: Read this, first:

Apart from the accepted answer, these errors occur when nginx is started without systemd. Kill nginx: ps -ax | grep nginx → find the nginx master pidkill ###; run nginx with systemd: systemctl start nginx.

If systemctl is not used to start nginx, systemctl stop nginx doesn't seem to work (at least on my server); so, systemctl restart nginx gives this error when it tries to start nginx a second time.

About my setup:

I'm on Debian 10, Buster, with a live server. I have tested both domain names, and they broadcast by themselves using these config files, but they do not broadcast when both config files are active.

What I'm trying to accomplish:

How do I set up two websites on a single ip address using nginx?

Error message(s):

nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/example.com:22 line 2.

nginx: [emerg] a duplicate default server for 0.0.0.0:443 in /etc/nginx/sites-enabled/example.com:22 line 2. default_server is used on port 443.

nginx: [emerg] duplicate listen options for [::]:443 in /etc/nginx/sites-enabled/example.com:23 line 3. default_server tried as http2 in one file and port 443 is used.

nginx: [emerg] duplicate listen options for [::]:80 in /etc/nginx/sites-enabled/example.com:23 line 3. default_server tried as http2 in one file.

nginx: [emerg] invalid parameter "example.com" in /etc/nginx/sites-enabled/example.com:23 line 3: default_server tried as example.com

Config files (comments stripped):

There are two of these files (code seen below). My config files are exactly as-seen in the code block below with only one difference: in both files, example.com is a real, unique domain name that I own.

My config files live in /etc/nginx/sites-available, and they are symlinked to /etc/nginx/sites-enabled.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/example.com; #example.com is different in both files.
    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com; #example.com is different in both files.

    location / {
        try_files $uri $uri/ =404;
    }
}

Primary dissonance:

Another stack question says to rewrite these lines or comment one of them out. The rewrites either do not resolve the error or result in a situation where files are not publicly served (example.com goes nowhere).

Summary:

This question seems to be about line 2, line 3, or line 2 and 3.

Essentially, all of the leading how-to's either expressly tell the reader to use the code above -or- they don't mention these lines.

like image 526
Wolfpack'08 Avatar asked Jan 25 '23 07:01

Wolfpack'08


1 Answers

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.

like image 77
Ivan Shatsky Avatar answered Feb 07 '23 13:02

Ivan Shatsky