nginx doesn't listen on port 80 twice?

后端 未结 4 1423
离开以前
离开以前 2020-12-11 09:34

Edit: Read this, first:

Apart from the accepted answer, these errors occur when nginx is started without systemd. Kill nginx: ps -ax | grep nginx

相关标签:
4条回答
  • 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.

    0 讨论(0)
  • 2020-12-11 10:06

    You can't run two applications on the same address with the same port.

    What you need to do is just change port of your application one with 80 and other application with some other port say 3000.

    If you want your application to run only with 80 port than just remove the default file from

    /etc/nginx/sites-available/default

    and optionally also from

    /etc/nginx/sites-enabled/default

    You can run

    sudo rm -rf /etc/nginx/sites-available/default

    sudo rm -rf /etc/nginx/sites-enabled/default

    Now you will be able to run one of your application on port 80

    0 讨论(0)
  • 2020-12-11 10:13

    As the error message suggests, you can't have two default listeners on the same IP address and port. Remove default_server from the listen declarations in one or both of your server blocks.

        listen 80;
        listen [::]:80 ipv6only=on;
    
    0 讨论(0)
  • 2020-12-11 10:14

    I came to this issue when I config three host name on one IP.

    I use a free ssl cercificate which can only sign on one hostname. So I apply three ssl certificates.

    server {
        listen      443 backlog=50000;
        server_name example1.com;
    
    server {
        listen       443;
        server_name  example2.com;
    
    server {
        listen       443  backlog=50000;
        server_name  example3.com;
    

    When I use nginx -t to test the config file, it shows

    nginx: [emerg] duplicate listen options for 0.0.0.0:443 in /etc/nginx/conf.d/example1_https.conf:2

    nginx: configuration file /etc/nginx/nginx.conf test failed

    I fixed it by remove the backlog=50000 from the example3.com

    0 讨论(0)
提交回复
热议问题