Home > Web Development > Nginx Windows Service

Nginx Windows Service

Introduction

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.

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 it’s 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 howto 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 (Microsofts 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://maven.dyndns.org/2/com/sun/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.

  • Exectue the command “c:\nginx\myapp.exe install”.

That’s it!

Conclusion

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 and help.

Ideas

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 infront of application servers such as Jrun/Jetty (ColdFusion / Rail).
  • Or just become your full web server as it’ll run PHP and other languages.

Links

  1. October 19, 2009 at 9:17 pm | #1

    Oh dude, I just started using Nginx a few weeks back, on Windows. Excellent tutorial. I’ll be blogging about this as well.

    • October 20, 2009 at 7:56 am | #2

      Glad I could help out, it’s something I’ve struggled with for a while and stopped myself from using Nginx until I could find a way to get it working as a service properly.

      In my setup I’ve got Nginx and Apache on the same machine, Nginx hogs the external addresses on port 80 and serves all the static content. Nginx then proxies dynamic requests to Apache, which sits on numerous 127.* addresses on port 80 so I don’t have to worry about broken links. It’s working really well my local development environment so far, just waiting for Nginx to call 0.8 stable and I’ll think about using it somewhere in production :) (hmmm maybe there’s something worthy of another blog post in there)

  2. October 20, 2009 at 4:39 pm | #3

    Cool. I do port forwarding from the firewall on port 80->8080, then have NGINX listening to 8080, which then does a proxy to the web server on 80. This way, if I ever have to remove NGINX, its seamless since its all port 80 on the web server. But yeah, makes for a lot of flexibility. There are some features that POUND has, like checking for malformed http requests, and some others, that I’m hoping NGINX can add.

  3. October 21, 2009 at 5:55 pm | #4

    Fantastic little tutorial – nice one. Just getting used to nginx but proving to be brilliant so far.

  4. October 30, 2009 at 7:26 pm | #5

    Great tutorial, thanks.

    An alternative to using a batch file is to reference the nginx.exe directly in the XML, but also include the -p option to specify the path to the nginx directory, e.g.:

    c:\nginx\nginx.exe
    -p c:\nginx
    -p c:\nginx -s stop

  5. October 30, 2009 at 7:27 pm | #6

    My XML got stripped — the last three lines of the previous comment are executable, startargument, and stopargument, respectively.

    • November 2, 2009 at 1:17 pm | #7

      @Dan,

      Well spotted. I didn’t notice there was a path parameter, that was the whole reason I had to add a batch file. I’ve updated the post (since I get quite a few hits for this now). Thanks for letting me know ;)

  1. October 16, 2009 at 3:15 pm | #1