Nginx multiple domain directives

I'm using Nginx for a PHP project. Here is what I do in /etc/nginx/sites-available/default:

server { server_name; include /etc/nginx/main.conf; // listen, php directives, etc. location ~ (.*)\.php(/|$) { fastcgi_pass php:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; } } server { server_name; include /etc/nginx/main.conf; location ~ (.*)\.php(/|$) { fastcgi_pass php:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; // Specific to this domain auth_basic "Authentication"; auth_basic_user_file /etc/nginx/.htpasswd; } }


listen 80; client_max_body_size 5M; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; root /var/www/web;

I have a lot of repetition in this code : The two location block could be merge in one for the two domains. I think I could use a if statement to add my specific code for domain_b but it's cleary not a recommanded way according to the documentation

Do you have idea how I can do to respect the DRY concept ?


-------------Problems Reply------------

For me I create a folder in my conf and call it includes for example and then you can include this where ever you want, for example

# /etc/nginx/includes/php.conf
location ~ (.*)\.php(/|$) {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;

Then in your configurations do something like this

server {
include /etc/nginx/main.conf; // listen, php directives, etc.
include /etc/nginx/includes/php.conf;

server {
include /etc/nginx/main.conf;
include /etc/nginx/includes/php.conf;
auth_basic "Authentication";
auth_basic_user_file /etc/nginx/.htpasswd;

Category:php Views:10 Time:2016-08-08
Tags: php nginx

Related post

  • nginx multiple domain virtual host configuration 2010-05-22

    I'm setting up nginx with multiple domain or wildcard support for convenience sake, rather than setting up 50+ different sites-available/* files. Hopefully this is enough to show you what I'm trying to do. Some are static sites, some are dynamic with

  • Seamlessly direct multiple domains to a single set of PHP files? 2010-01-30

    I need to direct multiple domains to a single set of (PHP) files. So I point a domain at my server, which then goes to a single index.php file. This index.php file then detects the domain accessing it and returns the appropriate content. I do not wan

  • How do I maintain PHP sessions across multiple domains on the same server? 2008-10-28

    I am looking for a way to maintain PHP sessions across multiple domains on the same server. I am going to be integrating my sites with a Simple Machines Forum so I will need to use MySQL based sessions. Thanks! --------------Solutions------------- De

  • Multiple domains, same web application on IIS 6 2008-11-07

    On IIS 6, is it possible to have multiple domain names pointing to the same web application, and conditionally serve CSS from within the web application based on the domain name? I need to host hundreds of different "skins" on the same web applicatio

  • Securing multiple domains on a single app 2009-10-28

    I'm investigating a feature to allow users to point their own domain names at our service to provide a more complete white-lable offering. So, I end up with multiple domains hitting the same IP. How do I provide SSL security for this? Do I need a cer

  • Apache mod_rewrite and multiple domains 2010-04-05

    I'm trying to use mod_rewrite to map multiple domains to different servlets on one host. Example: -> 192.168.1.n/dom1 -> 192.168.1.n/dom2 ... I'm using the mod_rewrite and mod_proxy and VirtualHost directive but it see

  • Multiple Domains on a VPS with Apache 2010-08-16

    I am planning on getting a VPS soon and have two sites I want to host. I have my local vhosts setup for my development environment using host file to port it correctly. My question is hopefully simple: When setting up with two separate domains that p

  • One rails application for multiple domain names 2010-09-01

    I have one rails application needed to be deployed by passenger module nginx. This application needs to be served for hundred domain names. I don't have enough memory to launch hundred rails instances. I'm not sure the proper way to launch rails in f

  • PHP authentication with multiple domains and subdomains 2010-11-03

    I have one main domain:, subdomains:, and other domains, Now it's done like these: ini_set("session.cookie_domain", ""); $domain = ''; login.php $user = $db->query("SELECT id

  • Python web framework for dealing with multiple domains? 2011-01-15

    I'm working on a project where I need to switch out the configuration options (templates,database used) based on the domain requested. What is the best framework to do this with? I did something similar previously using Django, but it was a real hack

  • Multiple domains for site subsections 2011-01-19

    Here's the deal. Basically I've got multiple domains, and I would like one of the domains to point to the regular base of the site, but the other domains to point to a subsection of the site without modifying the url in the address bar. The idea is t

  • Recommended way to setup Django Fast CGI configuration with multiple domains 2011-01-20

    I'm creating a Django project that will be used by multiple domains, and the functionality will be slightly different depending on the domain. I'm looking for advice on the proper way to set this up. The sites framework seems like it would be a good

  • grails controllers render errors multiple domain classes 2011-05-19

    I have this controller which performs me a multiple domain databinding class, and it is working as i want. But i would like some help about the error messages. Is it possible to display error messages from multiple domain classes? If so, how should b

  • htaccess and multiple domains 2011-12-14

    Scenario: I'm directing multiple domains at the same IP/machine, and delivering content that is dependent on the requesting/origin domain. I'm not using name based v-hosts because I need to keep the code-base consolidated, among other miscellaneous r

  • Multiple Domains to vary content on shared host PHP files 2011-12-21

    I have done some research on this problem without and solution yet. Setup as follows: Multiple domains say, and Both domains are mapped a the same hosting folder on a LAMP server. I want the index.php file in that fol

  • What is the proper way to configure Magento + Apache while running multiple stores on multiple domains? 2012-02-14

    This question has been asked amply all over the internet and on SO -- The available answers are obscured by specific details about everyone's particular installation. Hopefully this question will help everyone, including me, understand this better. M

  • X-Frame-Options Allow-From multiple domains 2012-04-18

    I have an 4.0 IIS7.5 site which I need secured using the x-frame headers option I also need to enable my site pages to be iframed from my same domain as well as from my facebook app. Currently I have my site configured with a site headed of:

  • Multiple domains for one site: alias or redirect? 2008-09-26

    I'm setting up a number sites right now and many of them have multiple domains. The question is: do I alias the domain (with ServerAlias) or do I Redirect the request? Obviously ServerAlias is better/easier from a readability or scripting perspective

  • Integrated Security for multiple domains? 2008-10-15

    Is there a way to connect to a db with Integrated Security from multiple domains? --------------Solutions------------- yes, you have to set up the active directory domains to handle a restricted trust. Then sql server can use the credentials from bot

  • IIS Multiple Domain Redirect 2008-11-08

    I currently have 2 domain names that I want to setup different websites for. I am currently looking at using some free hosting that works well for my current needs but doesn't give me any way to point "" to the actual site. Instead I have

  • Hosting Multiple Domains on Same Server Port with Apache2 2009-03-10

    How do I configure Apache2 via webmin or command-line (I'm using RHEL5 Linux) so that I can have multiple domains on the same server on the same port but in different subdirectories? For instance, trying to get and to be

  • Multiple-domains on WAMP localhost 2009-10-04

    If you're reading my question, thanks, I appreciate your help I'm using Wamp on Windows Vista. I've created 2 project folders: www/project1/ www/project2/ My virtual hosts file looks like this: NameVirtualHost *:80 <VirtualHost *:80> DocumentRo

  • ASP.NET caching for web application with multiple domain names 2009-11-12

    Is it possible to have a central cache for an ASP.NET web application that is accessed using multiple domain names? The web application is using a single website and application pool, with multiple domains (host headers) pointing to it. A bit of back

  • ASP.NET - authentication against multiple domains 2009-12-18

    My website can be access from many domain (a my own blog engine). So, how i can cross authentication? I'm using: IIS7, C#, membership, form authentication. --------------Solutions------------- If you are using Active Directory (assuming that

Copyright (C), All Rights Reserved.

processed in 0.099 (s). 11 q(s)