Monday, April 9, 2012

Amazon EC2 nginx virtual hosts configuration


Firts of all you should change DNS configuration. Do it in the beginning of process because some providers has some time to update configuration on theirs servers.
We'll create 2 vhosts:
test.example.com
test2.example.com
Go to your's DNS config panel and add 2 CNAME records with ec2 instance address ( same for both)
ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com
login to aws.
Install nginx on amazon ec-2:
yum install nginx
see version (for future troubleshooting, if it ocuur)
nginx -v
Create 2 vhosts folder tructures.
cd /var/
mkdir www
cd www/
Folder for each vhost
mkdir test.example.com
mkdir test2.example.com
cd test.example.com/
mkdir logs
mkdir web
mcedit web/index.html
Create simple html page:
<html>
<body>test.example.com</body>
</html>
cd ..
cd test2.example.com/
mkdir logs
mkdir web
mcedit web/index.html
Content for vhost 2
<html>
<body>test2.example.com</body>
</html>
Change web root owner
chown -R nginx:nginx /var/www

Change nginx setting to allow 2 vhost's with www. alias for each of them
cd /etc/nginx/conf.d/
mcedit virtual.conf
Add this lines to virtual.conf:

index index.htm index.html index.php;


#
# test.example.com

#
server {
  listen 80;
  server_name www.example.com;
  rewrite ^ $scheme://test.example.com$request_uri? permanent;
}
server {
  listen 80;
  server_name test.example.com;
  access_log  /var/www/test.example.com/logs/access.log;
  error_log   /var/www/test.example.com/logs/error.log;
  root /var/www/test.example.com/web;
}

#
# test2.example.com
#

server {
  listen 80;
  server_name www.test2.example.com;
  rewrite ^ $scheme://test2.example.com$request_uri? permanent;
}
server {
  listen 80;
  server_name test2.example.com;
  access_log  /var/www/test2.example.com/logs/access.log;
  error_log   /var/www/test2.example.com/logs/error.log;
  root /var/www/test2.example.com/web;
}
Restart nginx:
service nginx restart
Open in browser http://test.example.com and test2.example.com . Each page has different content.