How can i share common configuration between two servers. My app support both http and https(for few pages) and i am currently using fastcgi_param to save sensitive information like DB name and password. How can i share the location and fastcgi_param for both server(80, 443).
server { listen 80; server_name example.com; } server { listen 443 ssl; server_name example.com; root /home/forge/example.com/public; # FORGE SSL (DO NOT REMOVE!) ssl on; ssl_certificate /etc/nginx/ssl/example.com/304/server.crt; ssl_certificate_key /etc/nginx/ssl/example.com/304/server.key; index index.html index.htm index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log off; error_log /var/log/nginx/example.com-error.log error; error_page 404 /index.php; location ~ \.php$ { fastcgi_param ENV "production"; fastcgi_param DB_HOST "127.0.0.1"; fastcgi_param DB_PASSWORD "123456"; fastcgi_param DB_USERNAME "user"; fastcgi_param DB_NAME "example"; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } location ~ /\.ht { deny all; } }
conf i want to share:
index index.html index.htm index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log off; error_log /var/log/nginx/example.com-error.log error; error_page 404 /index.php; location ~ \.php$ { fastcgi_param ENV "production"; fastcgi_param DB_HOST "127.0.0.1"; fastcgi_param DB_PASSWORD "123456"; fastcgi_param DB_USERNAME "user"; fastcgi_param DB_NAME "example"; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } location ~ /\.ht { deny all; }
In addition to Andrey's answer which should help you immensely.
NGINX also supports an include statement.
You could for example create a common directory (/etc/nginx/common/) and then create
/etc/nginx/common/locations.conf
. Your locations.conf file would then contain something like,Then in one of your site configuration files you just use
include common/locations.conf;
to include the locations file. For example,Starting from 0.7.14, you can combine HTTP and HTTPS server blocks into single one - much easier to maintain:
Take a look on http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server for details.
Personally I use Ansible to provision and setup the servers via a data file the describes the endstate you want. See https://github.com/geerlingguy/ansible-role-nginx
requirements.yml
hosts
playbook.yml PSEUDO CODE
And you can use Jinja2 templates to copy & configure fragments
Run it all with
ansible-galaxy -i hosts playbook.yml