Nginx https configuration issu

if you want to disable IP access, the http access redirection https, function has been implemented, but there are the following problems

  1. nginx-t report waring
  2. do I need to add default_server after lissten 80?
nginx: [warn] conflicting server name "_" on 0.0.0.0:80, ignored
server {
    listen  80;
    server_name _;
    return 500;
}

server {
    listen  80;
    server_name www.domain.com;
    rewrite ^(.*)$  https://$host$1 permanent;
}

server {
    listen       443 ssl;
    server_name  www.domain.com;
    -sharp ssl 
    ssl on;
    ssl_certificate 1_.club_bundle.crt;
    ssl_certificate_key 2_.club.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    

    -sharp Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
        root /www/blog;
        index index.html;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
Jan.08,2022

report warning because two identical server (both listening on port 80) are defined, and server_name is defined as an underscore in English, which means that all ip or domain names are allowed to access matching, which coincides with www.domain.com.
if you want to disable IP access, you can change the first Server to the following form

server {
        listen 80;
        server_name IP_ADDRESS;
        return 500;
}

  1. The server with server_name as _ is used to process requests with empty Host field in the request header, which is usually placed at the end of all rules and finally matched. It is generally recommended to directly return 444; (Nginx non-standard return code, which means to discard the request directly).
  2. default_server the general usage scenario is to explicitly specify a default rule to fit the request when multiple Host rules do not match. If you do not add default_server , then the first rule will be matched by default, so sometimes when you want to directly discard the request whose Host does not match any rules, you can directly add it to the server_name _ server block.
Menu