Improving nginx server block file

How do I improve the following server block file

server {
    listen 80;
    return 301 https://$host$request_uri;
}
 
server {
    listen 443 ssl;

    ssl_certificate /etc/letsencrypt/live/demo.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/demo.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    server_name demo.com www.demo.com;

    root /var/www/demo.com/public;
         
    access_log /var/log/nginx/demo-com.access.log;
    error_log /var/log/nginx/demo-com.error.log;
         
    index index.php index.html index.htm index.nginx-debian.html;
         
    location / {
         try_files $uri $uri/ /index.php?$query_string;
    }
 
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
         
    location ~ /\.ht {
        deny all;
    }
}

so as to prevent the warning nginx: [warn] conflicting server name "" on 0.0.0.0:80, ignored from showing when running the command sudo nginx -t.

Thank you.


Solution 1:

You should always define server_name in every server block. In your case, it is missing from http server block.