nginx on Windows


While I normally use Apache as a local web server I thought it would be fun to try out somethig new last week: nginx! 

This article explains how to get nginx up and running on Windows 7.

References

 

About

Nginx is a high-performance http server that has grown in popularity for its predictable usage of system resources. Whether you are running a cheap VPS or need to wring out an extra 1,000 concurrent users on your popular website, nginx deserves a look.

 

Installation

On Windows the installation is fairly straightforward:

  • Download the latest Stable build from nginx.org
  • Extract the Zip file
  • Open a Command prompt and cd to the nginx-x.x.x folder
And thats it. No 'install' to speak of; just the executables, documentation and configuration files needed to run the webserver. nginx weighs in at around 3MB, which is pretty light. As a stand-alone webserver it does not include a Windows Service (services.msc doesn't know about nginx)
 

Configure the Port and Root directory

On my dev box I already have a service running which binds to port 80, so when I ran nginx for the first time I was greeted with this message:

c:\Tools\WebServers\nginx-1.2.3>nginx.exe
nginx: [emerg] bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

 

To resolve this problem I had to open the configuration file and make a change to the conf/nginx.conf file. To somebody used to the Apache configuration file it seems like a listing of arcane directives. I just did a search for 80 to find where the port is set and found this:

server {

listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root html;

index index.html index.htm;

}

 

 

To get this to work in my environment I changed the line listen 80; to listen 81;. I also had to change the Web-root location to a path that included spaces in it. I ended up with a server configuration fragment that looks like this (changes bolded):

server {

listen 81;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

#root html; # original conf
root "C:/Development/Web Applications";

index index.html index.htm;

}

 

Note: To specify a path for the root location, use a forward slash for the path delimiter and wrap the path in double quotes. Don't forget the semicolon that goes at the end of the line.

 

Controlling the nginx process

Starting and Stopping nginx is fairly straight forward. The Installation page lists the basic commands:

Start:

start nginx.exe

Stop:

nginx.exe -s stop

 

If there are errors or issues while serving web pages, see logs\error.log or the Windows Application Event Log