Nginx Virtual Hosts

Here we look at how to configure Virtual Hosts in Nginx.

First of all, you need to create 2 new folders to handle our next step:

mkdir /usr/local/etc/nginx/sites-available mkdir /usr/local/etc/nginx/sites-enabled

sites-available are all your virtual config files and sites-enabled hold a symbolic link from sites-available folder so you can remove and create new config if you want new sites added.

Next step is create your first virtualhost file:

nano /usr/local/etc/nginx/sites-available/{hostfile}

Change {hostfile} to your domain eg. test.local

My virtualhost file look likes this:

server { listen *:8080; server_name {test-domain}; #access_log /Users/{username}/{webfolder}/{hostfile}/log/access.log; #error_log /Users/{username}/{webfolder}/{hostfile}/log/error.log; root /Users/{username}/{webfolder}/{hostfile}; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri $uri/ /index.php?$args; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_intercept_errors on; include fastcgi_params; } }

change {username}, {webfolder} and {hostfile} width there names your ealier have changes the names on.

When you have save this folder you go to the sites-enabled and run the following command.

ln -s /usr/local/etc/nginx/sites-available/{hostfile} /usr/local/etc/nginx/sites-enabled/{hostfile}

Restart you NGINX webserver now:

sudo nginx -s reload

Look in your browser about its working well:

http://{hostfile}:8080

Remember to changes your computers hostfile if not you have done that already in sudo nano /etc/hosts put this line in your hostfile

127.0.0.1 {test-domain}