myol
myol

Reputation: 9838

Proper deployment of multiple sites using Nginx

I am an amateur with servers and I am setting up Nginx on my local ubuntu machine. I have multiple sites up and working locally as practice for transfer to a live server. I am unsure if this is the best setup for the sites in question. They are wordpress sites.

Each sites folder is in var/www

/var/www/site1
/var/www/site2

I sym linked the default file in /etc/nginx/sites-available/default to /etc/nginx/sites-enabled/default. Keeping it as is. Noting the listen parts especially;

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

Using this file as a basis, I did the same for both sites, creating separate file for each in sites available and sym linking them to sites-enabled. Here is site1s setup;

server {
        listen 80;
        listen [::]:80;

        root /var/www/site1;
        index index.php index.html index.htm;

        server_name local.site1.com; #for testing, edited in hosts

        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

My question is simply; is this an efficient way of setting up the Nginx server? This thread lists my setup as an option, but nothing on its efficiency. I left default in sites-enabled as the 'default_server' with my actual sites simply listening to port 80; I'm not really clued up on ports..

I feel like asking about having a 'dedicated' database server (block) for each site at this point, but maybe that is an entirely different kettle of fish..

Upvotes: 0

Views: 277

Answers (1)

Aleksey Deryagin
Aleksey Deryagin

Reputation: 2675

You config is OK, just use includes, and you can change global parameters for all sites from 1 file.

site1s.conf:

server {
   root /var/www/site1;
   server_name local.site1.com; #for testing, edited in hosts

   include /etc/nginx/conf.d/global.cnf;
}

site2s.conf ... siteNs.conf cloned from site1s.conf (you change just 2 lines with hostsnames )

/etc/nginx/conf.d/global.cnf (or any name you like, but better not .conf extension for avoid auto-loading by nginx - i dont know all your configs):

listen 80;
listen [::]:80;

index index.php index.html index.htm;

location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
        root /usr/share/nginx/html;
}

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}

Upvotes: 3

Related Questions