Multiple Websites on Nginx & Sites-Available

multiple websites on nginx & sites-available

Just to add another approach, you can use a separate file for each virtual domain or site you're hosting.
You can use a copy of default as a starting point for each one and customize for each site.

Then create symlinks in sites-enabled. In this way you can take sites up and down just by adding or removing a symlink and issuing a service nginx reload.

You can get creative and use this method to redirect sites to a maintenance mode page while you are doing site maintenance.

So the structure looks like this:

/sites-available/ (you can use obvious file names like this)
|
|-> a.mysite.com
|-> b.mysite.com
|-> someOtherSite.com

/sites-enabled/ (these are just symlinks to the real files in /sites-available)
|
|-> a.mysite.com
|-> b.mysite.com

Notice that since there are only the first two entries are the only symlinked items in sites-enabled, the third entry, someOtherSite.com is therefore offline.

nginx configuration for multiple sites on same server

The above configuration should raise an error as you have defined two default_server, Also there is no point in defining two server blocks for the same domain,

This is how you can achieve it,

map $request_uri $rot {
"~dir1" /var/www/dir1/Folder/app/;
default /var/www/dir2/Folder/app/;
}
server {
listen 80 default_server;
server_name mydomain.net;
root $rot;
index index.html;

location / {
try_files $uri $uri/ index.html =404;
}
}

The other way can be,

server {
listen 80 default_server;
server_name mydomain.net;

location /dir2/ {
root /var/www/dir2/PharmacyAdminApp1/app;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /dir1/ {
root /var/www/dir1/PharmacyAdminApp1/app;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}

If it still throws any error, switch your error_log on, and check which files are being accessed, and correct from there.

multiple site on localhost served by nginx without domain name

Thanks @lotfio.

if server_name is the same cannot be other server blocks using the same server_name, I suppose.

to avoid setting on /etc/hosts we can be do:

on /etc/nginx/sites-available/default:

server {
#... normal default stuff conf
include /etc/nginx/sites-avilable/localhost_adminer.inc;
incluse /etc/nginx/sites-avilable/localhost_pippo.inc;
#...
#... normal default stuff conf
}

if you like to do a reverse proxy on apache2 for adminer like my first try to move from apache2 to nginx you have to configure apache2 to Listen on other port (I choose 8181):

in /etc/nginx/sites-avilable/localhost_adminer.inc

location /adminer/ {
index conf.php;
alias /etc/adminer/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8181/adminer/;
}

(I'm on ubuntu so adminer package is configured to start on /etc/adminer/)

and so on for pippo, pluto sites etc.

best regards,

Leonardo

NGINX Multiple Site Setup

Just consolidating the links in the comments above and adding a few more for reference :

https://www.nginx.com/resources/wiki/start/topics/examples/full/

https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/

https://nginx.org/en/docs/example.html

+

multiple websites on nginx & sites-available

Below two are in turn referenced in one of the answers in the above SO post :

http://nginx.org/en/docs/http/request_processing.html

http://nginx.org/en/docs/http/server_names.html

+

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

While its not quite the standard SO answer, until someone else with better understanding comes along, you can refer these.

Proper deployment of multiple sites using Nginx

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;
}


Related Topics



Leave a reply



Submit