2

I have a few websites on a Ubuntu 16.04-Nginx server environment and the Nginx conf file of each includes this set of location blocks:

location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
    expires 365d;
}
location / {
    index index.php index.html index.htm fastcgi_index;
    try_files $uri $uri =404 $uri/ /index.php?$args;
}
location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php*-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

This set is the same in all confs and this results in redundancy. I tried to put it inside a server block in the global /etc/nginx/nginx.conf and restarted the server, but the server crashed.

How can one upstream this set from /etc/nginx/nginx.conf, into the server block of each site conf?

1 Answer 1

5

I am assuming that your question is how you can fix the redundancy issues, please correct me if I wrong. And if you are intent to move the location block to nginx.conf, i'm afraid that is not possible. However, If you have same configuration for several virtual server, you can use 'include' directive in every virtual server. Here is the example :

server{
    ...
    include /etc/nginx/common.d/location_common.conf;
    ...
}

And copy your configuration to the path. I hope this will help you. Thanks

2
  • Yes, you are right. I hoped to put these directly in my (rather short nginx.conf) but sadly you just confirm what I thought --- that it isn't possible... I might indeed just use an include. Thanks Ilham. Commented Jan 14, 2018 at 14:19
  • That's not a good solution if you have huge number of vhosts. Here 18 lines common conf included in 15k vhosts makes nginx eat like 3-4GB of ram. relad takes much longer. Not a great solution. Some people put second nginx in front of first. Second has single server {} catching everything, then do location stuff inside and proxy all other traffic to first nginx which has all typical vhosts etc. Also not a great solution.
    – arekm
    Commented Mar 15, 2019 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .