Centos 6.5 – NGINX how to setup virtual hosts.

These instructions show how to create virtual directories in NGINX. The virtual site we will use here as an example is “mysite.lol”. In summary the following steps will be executed to achieve virtual hosts in nginx.
– Create the directory for our virtual site under /srv/www. Each site will have it’s own “public_html” directory; for example, “/srv/www/mysite.lol/public_html”. We will also create the directories for the log files.
– Create the directories for available and enabled virtual hosts. Also create a soft link to link the “available” site to our enabled directory.
– Edit the “/etc/nginx/nginx.conf” config file.
– Define the virtual host under the /etc/nginx/sites-available.
– Create content for our mysite.lol (a simple index.html) file.

Step 1) Install nginx by first installing the EPEL repo.

To install the EPEL repo follow the instructions listed at http://www.rackspace.com/knowledge_center/article/installing-rhel-epel-repo-on-centos-5x-or-6x

Once you have installed the EPEL repo then you can proceed with installing nginx by simply running
yum install nginx

step 2) Setup nginx for virtual hosts.

mkdir -p /srv/www/mysite.lol/public_html
mkdir /srv/www/mysite.lol/logs
chown -R nginx:nginx /srv/www/mysite.lol/logs
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

step 3) Edit the “/etc/nginx/nginx.conf” file

Under the “http” section enter the following line in order to make nginx aware of possible virtual hosts.

# Load virtual host configuration files.
include /etc/nginx/sites-enabled/*;

step 4) Define the virtual host under the /etc/nginx/sites-available. This needs to be done in order to setup the specific parameters for our virtual host that will host the “mysite.lol” site.

 vi /etc/nginx/sites-available/mysite.lol
### mysite.lol

server {

# disable access log and define error_log to critical.
access_log off;
error_log /srv/www/mysite.lol/logs/mysite_error_log_critical crit;

# listen at 80 port, on any ip address.
listen 80;

# set the server name of your website
server_name mysite.lol www.mysite.lol;

# define the root directory of your site and index files.
 location / {
 root /srv/www/mysite.lol/public_html;
 index index.php index.html index.htm;

 }

# define the root of static files and set cache to the max 
 location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|wml|swf)$ {
 root /srv/www/mysite.lol;
 expires max;
 add_header Pragma public;
 add_header Cache-Control "public, must-revalidate, proxy-revalidate";
 }

}

Step 5) Create a test index.html file under “sites-available”

cd /etc/nginx/sites-available/
vi index.html

step 6) Create a soft link to enable your virtual host.

cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/mysite.lol
Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.