nginx web server runs on my ubuutu server. I have multiple websites under /var/www. Let's keep it as simple (myipaddres/web1 & myipaddres/web2)

/var/www/web1
/var/www/web2

At the moment my nginx confg (single file) looks like this (root points /var/www)

 server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name localhost;
    root /var/www;
    index index.php index.html index.htm;
}

But I want to have unique nginx configuration for each subdomain (myipaddres/web1 & myipaddres/web2) like

 /etc/nginx/sites-available/web1 & /etc/nginx/sites-available/web2

I was trying something like this, but nginx fails to reload.

 /etc/nginx/sites-available/web1

looks like this

 server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name localhost;
    root /var/www/web1;
    index index.php index.html index.htm;
}
 /etc/nginx/sites-available/web2

looks like this

 server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name localhost;
    root /var/www/web2;
    index index.php index.html index.htm;
}
share|improve this question
up vote 0 down vote accepted

Each server must have unique pair server_name and listen $port.

So you have two variants:

  1. Change server_name to localhost2 for some and add 127.0.0.1 localhost to your /etc/hosts . (Bad variant)
  2. Use location

server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name localhost; index index.php index.html index.htm; location /web1 { root /var/www; } location /web2 { root /var/www; } }

or if you want has different file for each app:

server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name localhost; index index.php index.html index.htm; include locations/app*.conf; }

and two file: /etc/nginx/locations/app1.conf and /etc/nginx/locations/app2.conf with text

location /web1 { root /var/www; }

and

location /web2 { root /var/www; }

share|improve this answer
    
location method works well. When just my ip address (myipaddress) is given in the browser without subdirectory location (myipaddress/web1), it loads the 404 page, is there any way to prevent this? (I don't want to load anything when just myipaddress given). thanks – kuruvi Jun 22 '14 at 17:52
    
location / { root /var/www; } after locations web1 and web2 – mr_tron Jun 23 '14 at 6:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.