fayalikt
fayalikt

Reputation: 151

Reduce duplication in Nginx site-conf files

The following code repeats itself in all my Nginx site-conf files (sites-available/domain.tld.conf). I could have 50 websites and all 50 site-conf files will have the same duplication:

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 ~ /\.ht {
    deny all;
}

location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
    expires 365d;
}

Is there a way to reduce duplication (without unsing include)?

Maybe, to put these in the global nginx.conf file?

Update for Richard Smith:

Putting this server block in the end of nginx.conf makes the server restart to fail:

server {
    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 ~ /\.ht {
        deny all;
    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
        expires 365d;
    }
}

Upvotes: 2

Views: 1623

Answers (1)

Dangelov
Dangelov

Reputation: 700

Put all your repeating configuration in a separate file and include it. I prefer to have it in a known location, and to split it based on the topic of the file, for example:

include /etc/nginx/cache_static_files.conf ;
include /etc/nginx/proxy_to_php73_fpm.conf ;

NB: use full/absolute paths for the included configuration files.

Upvotes: 1

Related Questions