Quantcast
Read WebProNews
With Friends!

NGINX Virtual Host Configuration: Migrating from Apache

Switching ain't easy.

Get the WebProNews Newsletter:

I have a total success kid moment every time I switch one of our Apache HTTP servers to NGINX. Without any significant tweaking to NGINX, we take a highly configured and pageview hardened Apache server and increase its capacity ten fold with a switch to NGINX. It comes by no surprise to me, then, that NGINX is continuing its steady ascension in the web server market share. W3Techs reports that NGINX broke 10% in early 2012, and now is halfway to 11% market share. What’s more convincing than that, however, is it it’s apparent dominance in the high traffic websites.

NGINX Market Share March 2012 courtesy W3Techs

Have you made the switch to NGINX? Let us know in the comments.

Many http servers utilize virtual hosting. Through virtual hosting, you can host various web properties from separate domains on one single http server utilizing, if you so desire, only one single IP address. In Apache, a virtual host looks something like this:

<VirtualHost *:80>
  ServerAdmin mmarr@ientry.com
  ServerName webpronews.com
  ServerAlias  www.webpronews.com
  DirectoryIndex index.php
  DocumentRoot /path/to/document/root/html

  <Directory /path/to/document/root/html>
    AllowOverride All
  </Directory>

  ErrorLog /var/log/httpd/webpronews_error_log
  CustomLog /var/log/httpd/webpronews_referer_log
</VirtualHost>

This VirtualHost directive tells Apache that any request coming in for the domain webpronews.com or www.webpronews.com gets passed along to the DocumentRoot of our website, in this case “/path/to/document/root/html”. Thus, www.webpronews.com/some-file.html gets sent over to /path/to/document/root/html/some-file.html and serves that file, if available. This vhost directive also tells us to allow .htaccess files to override the Apache settings on a per directory basis. Finally, we specify our log locations for all the errors and requests for this virutal host. Although not perfect for all implementations, this is a very standard and sufficient Apache virtual host configuration.

All the same above can be accomplished in NGINX, except for, unfortunately, the .htaccess support. NGINX doesn’t support .htaccess files, so any url rewrites in NGINX will have to be included in this same nginx virtual host directive. Here’s the same configuration for NGINX:

server {
   listen 80;
   server_name: webpronews.com www.webpronews.com;

   location / {
      root /path/to/document/root/html;
      index index.php;
   }

   access_log /var/log/nginx/webpronews_access_log main;
   error_log /var/log/nginx/webpronews_error_log error;
}

Personally, I find this NGINX configuration example considerably cleaner. However, since there is no PHP module for NGINX, you’ll have to add somewhere in the server block to pass all your PHP files through a proxy. NGINX provides a few examples of how to do this in the default nginx.conf file. After adding the PHP handling and any rewrites, the NGINX virtual host configuration can get pretty messy.

I’ve been in the process of migrating literally thousands of Apache virtual host configurations to NGINX, and it ain’t easy. I won’t complain if NGINX makes a tool to efficiently migrate an Apache virtual host configuration file plus any .htaccess files in the root directory of the virtual host to a single NGINX virtual host directive. Until then, I’ll simply have to continue manually migrating these configurations over in order to enjoy the benefits of NGINX.

Top Rated White Papers and Resources
There are 2 Comments. Add Yours.
  1. Like (0) Dislike (0)
    niraj

    can u give me the more than two host adding file example for virtual hosting in Nginx?

    Most of the time I was getting error ..

    Please mentioned with file name.

    Reply
  2. Like (0) Dislike (0)
    Brian

    Really a bit tiring to move the hosted domain one by one.
    I am thinking to switch to nginx too.
    :)

    Reply

What do you think? Respond.

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>