Nginx is a web server. It’s an alternative to the big beasts of Apache/IIS and is similar to Lighttpd, built for speed and good handling of heavy loads. It comes from the world of Linux but it does run on Windows as well (built natively). The only problem is it does support Windows Services.
Although Nginx has a great reputation for being fast and providing great performance, it does not offer such on Windows. If you check the following link, you’ll see that Windows support is considered Beta and doesn’t provide the same level of performance due to it’s implementation.
Maybe you didn’t even realise that WordPress is a Nginx user with it serving a lot of their static content and load balancing requests out to its other servers. If you want to find out more about Nginx take a look at the links at the bottom of this post.
Nginx Vs. Windows Services
Nginx is easy to install… you unzip it. Now I won’t go into configuration settings etc… because there’s a fantastic wiki full of information for the server (see links below). The default configuration will be suitable for now as it’ll serve on port 80 the html folder contained within the nginx directory.
To get it up and running you simply execute nginx.exe. The problem comes to when you need to stop it. You’ll need to execute the command:
nginx.exe -s stop
While this is reasonably simple, it would be nicer if it worked as a service, like apache or IIS. This would allow us to set nginx to automatic and start when the computer boots and give us easy ways to start, stop or restart the service, set recovery options, dependant services etc…
Why not just use instsrv/servany, FireDaemon or others?
There is already a how-to for using nginx with FireDaemon but it suffers from one very important issue. When nginx is started, it creates a secondary process. So you’ll have two nginx.exe’s running. There’s probably a very sensible reason for this, but it’s better to ask why in the nginx forum
The way instsrv/srvany (Microsoft’s way of making something a service) or FireDaemon work, is to start the program and when you want to stop it, it’ll close the process. But neither is capable of closing the extra nginx.exe. So each time you stop/start/restart you’re creating an extra nginx.exe process each time. Not good!
Setting up as a service (a better way)
Thanks to a small project called “Windows Service Wrapper” we have a way of properly starting and stopping nginx. First you’ll need to download the program from http://download.java.net/maven/2/com/sun/winsw/winsw/ you just need the latest exe (which was “winsw-1.8-bin.exe” at the time of writing).
- Retrieve latest “exe” of “Windows Service Wrapper”.
- Place this in your nginx folder and rename it to myapp.exe.
We then need tell WinSw what we want it to do. This will be via an XML config file where we’ll state that nginx needs a shutdown command.
- Create a file called myapp.xml containing the following:
<service> <id>nginx</id> <name>nginx</name> <description>nginx</description> <executable>c:\nginx\nginx.exe</executable> <logpath>c:\nginx\</logpath> <logmode>roll</logmode> <depend></depend> <startargument>-p c:\nginx</startargument> <stopargument>-p c:\nginx -s stop</stopargument> </service>
Obviously you should change the file slightly, depending on your folder locations. For those more technician people out there, you can also set a service that nginx depends on.
Finally we need to install the service, to do this simply execute the following command and you’ll then see the “Nginx” service in you’ve services list.
- Execute the command “c:\nginx\myapp.exe install”.
From my experience so far this has worked perfectly. You get Windows Service support and no orphaned “nginx.exe”‘s hanging around from service restarts. Best of both worlds.
It’d be great if nginx could do this itself, but the author is focusing on other more important developments at the moment. I’m sure there are people out there with enough programming knowledge to contribute the required code, so if you are such a person, please try to help.
Here is a small list of what Nginx can be useful for, due to it’s high performance nature.
- Serve static content directly and proxy dynamic content from a backend server (Apache/IIS).
- Load balancing proxy to share the load between servers.
- SSL Accelerator, take the load of SSL off the application server.
- Sit in front of application servers such as Jrun/Jetty (ColdFusion / Rail).
- Or just become your full web server as it’ll run PHP and other languages.
- Nginx Wiki – http://wiki.nginx.org/Main
- Nginx Forum – http://forum.nginx.org/
- Windows Service Wrapper – http://projectkenai.com/projects/winsw