我感觉好像在我的Nginx默认文件中有重复的代码

这是我的sites-available/default Nginx文件。 正如你可以看到它包含我的默认设置,我的网站的设置。

 server { server_name _; return 301 https://$host$request_uri; root /var/www/html; index index.php index.html index.htm; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass 127.0.0.1:9000; } location / { try_files $uri $uri/ =404; } location ~ /\.ht { deny all; } } server { listen 80; listen [::]:80; # 443 ssl http2; # [::]:443 ssl http2; server_name contfix.co.il www.contfix.co.il; ssl_certificate /etc/nginx/conf.d/ssl/contfix.co.il.crt; ssl_certificate_key /etc/nginx/conf.d/ssl/contfix.co.il.key; ssl_dhparam /etc/nginx/conf.d/ssl/dhparam.pem; root /var/www/html/contfix.co.il; location / { index index.php index.html index.htm; try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass 127.0.0.1:9000; } location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff)$ { expires 365d; } location ~* \.(pdf)$ { expires 30d; } } 

我觉得好像我在那里有一些代码重复,使我的站点特定的服务器块太长,没有很好的理由,因此不忠于DRY标准。

我试图删除所有在我看来是顶部的默认服务器块的重复,但每次只是打破了服务器。

我真的有重复吗? 有什么我可以移动到顶级的默认服务器块?

我的目的是使站点特定的服务器块尽可能短。

你错误地解释了默认_服务器块的用途。 它不包含所有其他服务器块的default设置,当没有其他块匹配客户端发出的服务器请求时(或换句话说,当没有webapp或网站可用时),将使用该块。 您仍然需要独立configuration每个服务器模块。

你可以使用include,但是这样可以简化单个变更,因为你只需要对所有的服务器块进行一次更改,这会使整个configuration更加混乱。