Running NGINX as a Windows Service
nginx is a great tool that works as a web server, and also as a web proxy that allows to make useful things like protect a site using TLS, you know, put it under HTTPS protocol. Other useful thing possible to acomplish is to protect a site with credentials, or generating access logs, or load balance traffic between several servers (in this aspect nginx is the product behind comercial solutions like BigIP F5).
To setup nginx on windows, is as simple as getting the zip file from the nginx download page https://nginx.org/en/download.html
After you download just unzip it to a folder like c:\nginx
You can start it manually just running from the command line the nginx.exe from c:\ngnix. You can do a fast test pointing your browser to http://localhost/ or http://127.0.0.1 or if you are feeling geeke today try http://2130706433/
There is only an issue for setting the nginx as a service, and it is that its executable does not support the WindowsNT service API, for this reason we need a wrapper to handle its interaction with Windows Services.
To run it as a service there are several solutions, older ones like the Windows Server 2003 Resource Kit using SrvAny, that it is to old to recommend in 2023, also the 2014 misfortune named NSSM (non-sucking service manager).
Luckily I stumbled across a simple, open source based solution built with .net, that allows to run any executable as a service
https://github.com/winsw/winsw
This wrapper allows to use any .exe as a Windows Service. It handles the configuration and installation of the service using XML.
Get Alfonso Baqueiro Bernal’s stories in your inbox
Join Medium for free to get updates from this writer.
To setup the winsw wrapper you need to go to https://github.com/winsw/winsw/releases and download a recent version of the WinSW-*.exe corresponding to your architecture, most of the times x64
https://github.com/winsw/winsw/releases/download/v2.12.0/WinSW-x64.exe
Once you download, copy the file to where you setup your nginx dir in this example c:\nginx and rename it to nginx-service.exe.
Next you need to create a nginx-service.xml for describe the service:
<service>
<id>nginx</id>
<executable>c:\nginx\nginx.exe</executable>
<name>nginx</name>
<description>nxing web server</description>
<workingdirectory>c:\nginx</workingdirectory>
<priority>Normal</priority>
<stoptimeout>15</stoptimeout>
<stopexecutable>c:\nginx\nginx.exe</stopexecutable>
<stoparguments>-s stop</stoparguments>
<startmode>Automatic</startmode>
</service>Finally, to install as a service, run a command line (the old ms-dos command line works better) with administrative privileges and run:
cd c:\nginx
nginx-service.exe installYou will get a confirmation for the service installation, and also if you open the windows services UI you will find the new service entry.
From that UI you can start or stop the service, also change parameters as the Startup Type, change to Manual, Automatic or Disable, also you can change the User the service runs as, in the LogOn tab.
Hoping that you find this article useful, you can connect with me at LinkedIn https://www.linkedin.com/in/abaqueiro/










