改进 nginx 服务器块文件

改进 nginx 服务器块文件

如何改进以下server block文件

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;
    }
}

以避免nginx: [warn] conflicting server name "" on 0.0.0.0:80, ignored在运行命令时显示警告sudo nginx -t

谢谢。

答案1

您应该始终server_name在每个server块中定义。 在您的例子中,http 块中缺少它server

相关内容