Tips on Configuring Nginx for Virtual Hosting
By Angsuman Chakraborty, Gaea News NetworkThursday, July 10, 2008
Nginx tutorials and examples doesn’t tell you how you can configure nginx with virtual hosting when different sites are in different pre-defined directories. Often you are moving over your site from a different web server like Apache HTTPD to Nginx. So you have a pre-defined directory structure which may not be easy to change. Configuring nginx is somewhat tricky in this scenario because nginx isn’t very flexible in accepting directives everywhere. After much experimentation we came up with this simple way:
server { # Standard nginx directives like listen, index etc.
server_name host1.com host2.com host3.com
# Set default document root in a variable set $myroot /var/www/html;
if ($host ~* host1\.com$) { # The myroot value is just an example, use your own set $myroot /var/www/html/forum/host1; } if ($host ~* host2\.com$) { # The myroot value is just an example, use your own set $myroot /var/www/html/site; }
root $myroot;
# More directives follow
}
You may notice that in virtual hosting server_name is set to the first server name in your server_name list. This can lead to directory resolution problems in nginx (read the linked article for an elegant solution) in addition to specifying a wrong value for $_SERVER[SERVER_NAME] for php etc. To fix the second problem you can specify the following fastcgi_param:
fastcgi_param SERVER_NAME $host;
Note: nginx [engine x] is a high performance HTTP server and mail proxy server written by Igor Sysoev.
Tags: nginx, Virtual Hosting
Nginx Hacking Tips