The following code is an Nginx default conf file. The conf file itself includes another server
block which holds a site's configuration.
While this combination works and the sites appears fine on web, I have a feeling that my code doesn't follow the DRY standard.
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;
}
# notes {
# 443 ssl http2;
# [::]:443 ssl http2;
# }
}
server {
listen 80;
listen [::]:80;
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;
}
}
Given that my code seems to be a bit repetitive, I want to ask: Can it be made a bit less repetitive?
-
\$\begingroup\$ This works? Because your second server block is listening on port 80 and also using an SSL certificate. Either you mean to be listening on port 443 or you want to remove the lines about the SSL configuration (I think) \$\endgroup\$Conor Mancone– Conor Mancone2017年10月23日 14:50:35 +00:00Commented Oct 23, 2017 at 14:50
-
\$\begingroup\$ These are just comfortable comment because later I would have to use this syntax. \$\endgroup\$user125391– user1253912017年10月23日 15:03:44 +00:00Commented Oct 23, 2017 at 15:03
-
\$\begingroup\$ Was there anything else you hoped my answer might suggest? \$\endgroup\$Sᴀᴍ Onᴇᴌᴀ– Sᴀᴍ Onᴇᴌᴀ ♦2019年12月06日 14:18:51 +00:00Commented Dec 6, 2019 at 14:18
-
\$\begingroup\$ @SᴀᴍOnᴇᴌᴀ I didn't recall I putted two similar confs in one code block (maybe by mistake). I think there weren't other parts and the purpose at the time was to use just one big file for everything, something I later found out impossible with Nginx. \$\endgroup\$user125391– user1253912019年12月06日 17:44:45 +00:00Commented Dec 6, 2019 at 17:44
1 Answer 1
I compared the two server configurations and the only thing that appears to match between the two is the location ~ \.php$
directive, as well as the index
directive, though in the latter configuration that is under the directive location /
Perhaps the common configurations could be put into the file included via include
(i.e. fastcgi-php.conf
).