ngnix virtual hosts

Setting up virtual host in nginx

# check ubuntu version
# try
lsb_release -a
# if that doesn't work, then try
cat /etc/*release

Determining the Ubuntu version helps in assuming the default locations of nginx conf file.


# create *.conf files locally
# create 2 server blocks. Only one block carries the default_server directive
server
 {
   listen 80 default_server;
   listen [::]:80 default_server ipv6only=on;
   root /var/www/example.com/public_html;
   index index.html index.htm;
   server_name example.com www.example.com;
   location / {
      try_files $uri $uri/ =404;
   }
}
# notice: The 2nd server block is non-default and it doesn't carry ipv6only directive
server {
 listen 80;
 listen [::]:80;
 root /var/www/foobar.com/public_html;
 index index.html index.htm;
 server_name foobar.com www.foobar.com;
 location / {
   try_files $uri $uri/ =404;
 }
}

# the default server responds if a domain matching fails, or if you directly hit the IP of the server

# copy them to the 'sites-available' remote dir
scp -r /Users/<username>/<path-to-dir>/foozbar.com.nginx.conf root@123.456.789.101112:/etc/nginx/sites-available/foozbar.com.nginx.conf

# remove default conf from 'sites-enabled' dir
rm /etc/nginx/sites-enabled/default

# create soft-link of the 'conf' from the '-available' to the '-enabled' dir
ln -s /etc/nginx/sites-available/foozbar.com.nginx.conf /etc/nginx/sites-enabled/


# note: the file extension is not mandatory. It has just been used as a convention

# restart nginx
# a) the signal way
nginx -s reload

# b) the service way
sudo service nginx restart

More:

That’s all folks

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s