Answer the question
In order to leave comments, you need to log in
How to achieve DRY in nginx configuration for many sites of the same type?
When switching from nginx+apache to nginx+php-fpm, there was a problem with nginx configs.
There are ~5 users on the server, each with several sites.
Half of the sites have a simple structure:
/home/user_name/sites/example.com/log/...
/home/user_name/sites/example.com/tmp/...
/home/user_name/sites/example.com/www/...
/home/user_name/sites/example.com/backend/log/...
/home/user_name/sites/example.com/backend/tmp/...
/home/user_name/sites/example.com/backend/www/...
/home/user_name/sites/example.com/frontend/log/...
/home/user_name/sites/example.com/frontend/tmp/...
/home/user_name/sites/example.com/frontend/www/...
/etc/nginx/nginx.conf
/etc/nginx/sites/default.conf
/etc/nginx/sites/andrey/site.ru.conf
/etc/nginx/sites/andrey/example.com.conf
/etc/nginx/sites/ivan/example.ru.conf
...
...
http {
...
include sites/default.conf;
include sites/andrey/*.conf;
include sites/ivan/*.conf;
}
set $user_name ivan;
set $site_name example.ru;
include sites/basic_server.conf; # конфиг сервера для первой схемы
# или
include sites/advanced_server.conf; # конфиг сервера для второй схемы с бекэндом
listen 80;
server_name $site_name www.$site_name;
access_log /home/$user_name/sites/$site_name/log/nginx_access.log main;
error_log /home/$user_name/sites/$site_name/log/nginx_error.log;
root /home/$user_name/sites/$site_name/www;
index index.php index.html;
if ($host = 'www.$site_name') {
rewrite ^/(.*)$ http://$site_name/$1 permanent;
}
location ~ \.php$ {
include conf.d/fastcgi.conf;
}
set $sites array(
array('user' => ivan, 'name' => 'example.com', 'type' => 'basic'),
array('user' => andrey, 'name' => 'example.ru', 'type' => 'advanced'),
);
Answer the question
In order to leave comments, you need to log in
You can not bother and generate with a template engine from SCM (such as chef/puppet/ansible/salt/cfengine/bcfg2)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question