47

I am trying to run nginx (reverse proxy) as a windows service so that it's possible to proxy a request even when a user is not connected.

I searched a lot around and found winsw that should create a service from an .exe file (such as nginx).

i found many tutorials online saying to create an xml file as 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>

(i have nginx.exe in a folder called nginx under c: o the paths are correct).

Now the problem is that the service is created but i can't seem to make it start, every time i try to start it a windows pops up saying

Error 1053: The service didn't respond to the start or control request in a timely fashion

Does anyone know how can i fix this or a different way to run nginx as a window service?

John Doe
  • 1,613
  • 1
  • 17
  • 35
  • 1
    This is a clear duplicate of this question : https://stackoverflow.com/questions/10061191/add-nginx-exe-as-windows-system-service-like-apache – JJP Sep 18 '17 at 08:47
  • 1
    Possible duplicate of [Add nginx.exe as Windows system service (like Apache)?](https://stackoverflow.com/questions/10061191/add-nginx-exe-as-windows-system-service-like-apache) – Davide Pizzolato Jun 06 '18 at 09:07
  • @jjp that question is about how to run it, my question is about a specific error. in my question I literally used the code of that question solution but it obviously doesn't work. I don't really get how this can be considered a duplicate aside from the title but ok – John Doe Jun 07 '18 at 10:20
  • @Davide Pizzolato same as above since I can tag only 1 user – John Doe Jun 07 '18 at 10:21

5 Answers5

87

Just stumbled here and managed to get things working with this free open source alternative: https://nssm.cc/

It basically is just a GUI to help you create a service. Steps I used:

  1. Download NGinx (http://nginx.org/en/download.html) and uzip to C:\foobar\nginx
  2. Download nssm (https://nssm.cc/)
  3. Run "nssm install nginx" from the command line
  4. In NSSM gui do the following:
  5. On the application tab: set path to C:\foobar\nginx\nginx.exe, set startup directory to C:\foorbar\nginx
  6. On the I/O tab type "start nginx" on the Input slow. Optionally set C:\foobar\nginx\logs\service.out.log and C:\foobar\nginx\logs\service.err.log in the output and error slots.
  7. Click "install service". Go to services, start "nginx". Hit http://localhost:80 and you should get the nginx logon. Turn off the service, disable browser cache and refresh, screen should now fail to load.

You should be good to go from then on.

Dave
  • 1,036
  • 9
  • 10
  • 3
    Note: If you are using Windows 10 creators update you need to install a [more recent build of NSSM](http://nssm.cc/download). I ran into an issue where NSSM worked fine and then after Windows updated I couldn't run `nssm start nginx`. I thought the issue was with my nginx config, but it turned out to be NSSM not working correctly. – seangwright Jun 07 '17 at 16:57
  • "Input slow" - That seems to be some kind of typo? (Is it supposed to be "Input (stdin)"). What does that do? (The others seems to take filenames) – Gert van den Berg Sep 17 '18 at 13:13
  • NSSM can also be installed using Chocolatey through `choco install nssm`. The version installed is much more recent (version from 16th January 2018 at the time of writing) than the one available for download in the NSSM webpage. – joanlofe Mar 05 '19 at 14:59
  • By the way, the latest version in Chocolatey doesn't work as expected. I made it work using version 2.24. – joanlofe Mar 05 '19 at 15:41
  • 2
    These days it appears that `choco install nginx` will also install NSSM and configure it for you. – Charles Wood Mar 14 '19 at 17:06
  • 1
    2019-12-24. This guide stil actual for window 10 nginx 1.16.1 and nssm 2.24 and save my time! – Sergio Belevskij Dec 24 '19 at 13:49
  • I just received a random notification from this question 3 years later, switching my accepted answer to this one since it's basically what I did but it's more complete and the comments are more useful – John Doe Oct 28 '20 at 15:42
  • fun fact: nssm's arguments section cannot parse $env:NGINX_HOME – NanoNova Dec 14 '20 at 04:58
  • Using Windows 10 PowerShell, I needed to run `PS C:\code\nssm-2.24\win64> .\nssm install nginx` to get nssm to open. – Ryan Jun 12 '21 at 00:18
  • What is meant with 'type "start nginx" on the Input slow.' That makes no sense to me? What is an input slow? – Schoof Jul 28 '23 at 06:57
36

NSSM is very nice, but there is another alternative: The PowerShell Cmdlet New-Service

Here is just a simple example:

$params = @{
    Name = "MyService"
    BinaryPathName = "path/to/exe"
    DisplayName = "My Service"
    StartupType = "Automatic"
    Description = "Description of my service"
}
New-Service @params

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-service?view=powershell-6

  • 1
    not sure why this isn't higher voted. Clearly the better answer. – Barry Oct 10 '20 at 21:24
  • 1
    I think this is the best answer too. I will try on my server. – paulz Feb 15 '21 at 00:28
  • 14
    I don't think this works. As far as i know you can't run any exe as a Microsoft windows service. When i try this with nginx i get "Windows could not start teh nginx service on Local Computer. Error 1053: The Service did not respond to the start or control request in a timely fashion" – Mark Jul 16 '21 at 19:20
  • 2
    I got the same problem as @Mark - This doesn't work good with nginx – CuriousDeveloper Aug 10 '21 at 19:08
  • This is will not be work because windows service required special exe – Actimele Dec 22 '21 at 23:41
  • New-Service is just a way to configure windows service, equivalent of using `sc.exe`. But Windows Services require some specific entrypoints (for Start/Stop/Pause/Restart/etc) - that's why there are "Windows Service Wrappers" like NSSM, which can convert a regular application into a Windows Service. – drizin Jun 08 '23 at 13:49
  • Using this code targetting nginx.exe will not work. – ATX Jul 13 '23 at 21:00
4

As told in other answers NSSM is the best tool to run Nginx as a service.
If you do not want to use any external 3rd party software then you can implement any of these two methods.

  • Windows Task Scheduler
  • Windows startup shortcut

Windows Task Scheduler

  • As mentioned in this answer prepare one start.bat file.
  • Put this file where nginx.exe is present.
  • Open windows task scheduler and set up the task as described in this answer to run it indefinitely.
  • Do not forget to run this task as the highest privilege with the system account, more details can be found here.
  • Make the task to start daily at a certain time, through the bat file it will check whether the service is already running to avoid creating multiple nginx.exe instances.
  • If due to some reason Nginx shuts down, within 5 minutes it will start.

Windows Startup shortcut

  • Create one shortcut of nginx.exe and put it in the startup folder of Windows.

  • Follow this answer to find your startup location.

  • Nginx will run automatically whenever you log in to the system.
  • This one is the easiest. However, it is dependent on user profile i.e. if you are running Nginx on a server, it will run only for your user account, when you log off it stops.
  • This is ideal for dev environment.
Soumendra
  • 1,518
  • 3
  • 27
  • 54
1

You'll need this for winsw

   <service>
        <id>nginx</id>
        <name>nginx</name>
        <description>nginx</description>
        <executable>c:\...\nginx.exe</executable>
        <logpath>...</logpath>
        <logmode>roll</logmode>
        <stopexecutable>c:\nginx\nginx-1.14.0\nginx.exe</stopexecutable>
        <stopargument>-s</stopargument>
        <stopargument>stop</stopargument>
    </service>

You will need an <executable> assuming you are using the nginx.conf hence don't need any starting up arguments and also a <stopexecutable> and <stopargument>s (to emlate nginx -s stop)

tmjee
  • 179
  • 1
  • 3
0

I found your issue. You have stopargument instead of stoparguments. use the following:

    <executable>"%BASE%\nginx.exe"</executable>
    <!--arguments></arguments-->
    <startarguments></startarguments>

    <stopexecutable>"%BASE%\nginx.exe"</stopexecutable>
    <stoparguments>-s stop</stoparguments>

This is running on my server

Marco
  • 1