运行 nginx 作为 Windows 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40846356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:43:53  来源:igfitidea点击:

run nginx as windows service

windowsnginxservice

提问by John Smith

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.

我正在尝试将 nginx(反向代理)作为 Windows 服务运行,以便即使在用户未连接的情况下也可以代理请求。

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

我搜索了很多,发现应该从 .exe 文件(例如 nginx)创建服务的winsw

i found many tutorials online saying to create an xml file as following

我在网上发现很多教程说创建一个xml文件如下

<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).

(我在 c: o 下名为 nginx 的文件夹中有 nginx.exe 路径正确)。

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?

有谁知道我该如何解决这个问题或以不同的方式将 nginx 作为窗口服务运行?

采纳答案by John Smith

I found NSSM(the Non-Sucking Service Manager): a program that does exactly what i want and it's much easier to set up.

我找到了NSSM(Non-Sucking Service Manager):一个程序完全符合我的要求,而且设置起来要容易得多。

回答by Dave

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

刚刚在这里偶然发现并设法使用这个免费的开源替代方案进行工作:https: //nssm.cc/

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

它基本上只是一个帮助您创建服务的 GUI。我使用的步骤:

  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:80and you should get the nginx logon. Turn off the service, disable browser cache and refresh, screen should now fail to load.
  1. 下载 Nginx ( http://nginx.org/en/download.html) 并 uzip 到 C:\foobar\nginx
  2. 下载 nssm ( https://nssm.cc/)
  3. 从命令行运行“nssm install nginx”
  4. 在 NSSM gui 中执行以下操作:
  5. 在应用程序选项卡上:将路径设置为 C:\foobar\nginx\nginx.exe,将启动目录设置为 C:\foobar\nginx
  6. 在 I/O 选项卡上,在慢速输入上键入“启动 nginx”。可选择在输出和错误槽中设置 C:\foobar\nginx\logs\service.out.log 和 C:\foobar\nginx\logs\service.err.log。
  7. 点击“安装服务”。转到服务,启动“nginx”。点击http://localhost:80,你应该得到 nginx 登录。关闭服务,禁用浏览器缓存和刷新,屏幕现在应该无法加载。

You should be good to go from then on.

从那时起你应该很高兴。

回答by Sam G

I found other solution other than NSSM. That is Windows Service Wrapper and the following are the instructions:

我找到了NSSM以外的其他解决方案。那是 Windows 服务包装器,以下是说明:

  1. Download the latest version of Windows Service Wrapper via github.

    • Current version as of this writing is v2.1.2(Since v2.x executables for .NET2.0 and .NET4.0 are available - others only on demand.)
  2. Rename winsw-xxxx.exe to something like nginxservice.exe.

    • This is the name that will show up for the process that owns your Nginx process.
  3. Place an XML file next to the exe with the same base name, e.g. nginxservice.xml. The contents should be like below (verify your nginx location).
  1. 通过github下载最新版本的 Windows Service Wrapper 。

    • 在撰写本文时,当前版本为 v2.1.2(因为 .NET2.0 和 .NET4.0 的 v2.x 可执行文件可用 - 其他仅按需提供。)
  2. 将 winsw-xxxx.exe 重命名为类似 nginxservice.exe 的名称。

    • 这是将为拥有您的 Nginx 进程的进程显示的名称。
  3. 在 exe 旁边放置一个具有相同基本名称的 XML 文件,例如 nginxservice.xml。内容应如下所示(验证您的 nginx 位置)。

<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</startargument> <startargument>c:\nginx</startargument> <stopexecutable>c:\nginx\nginx.exe</stopexecutable> <stopargument>-p</stopargument> <stopargument>c:\nginx</stopargument> <stopargument>-s</stopargument> <stopargument>stop</stopargument> </service>

<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</startargument> <startargument>c:\nginx</startargument> <stopexecutable>c:\nginx\nginx.exe</stopexecutable> <stopargument>-p</stopargument> <stopargument>c:\nginx</stopargument> <stopargument>-s</stopargument> <stopargument>stop</stopargument> </service>

  • You can find up to date details about the configuration on the config GitHub pageand a generic example showing all possible options here.

    1. Run the command nginxservice.exe install.
      • You will now have a Nginx service in your Services! (It is set to start automatically on boot; if you want to start your server, you must manually start the service (net start Nginx).)
  • 您可以在配置 GitHub 页面上找到有关配置的最新详细信息,并在此处找到显示所有可能选项的通用示例。

    1. 运行命令 nginxservice.exe install。
      • 您现在将在您的服务中拥有一个 Nginx 服务!(设置为开机自动启动;如果要启动服务器,必须手动启动服务(net start Nginx)。)

The Above answer was taken from a post.

以上答案摘自一个帖子

回答by Soumendra

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 softwarethen you can implement any of these two methods.

正如其他答案中所说,NSSM 是将 Nginx 作为服务运行的最佳工具。
如果您不想使用任何外部 3rd 方软件,那么您可以实现这两种方法中的任何一种。

  • Windows Task Scheduler
  • Windows startup shortcut
  • Windows 任务计划程序
  • Windows 启动快捷方式

Windows Task Scheduler

Windows 任务计划程序

  • As mentioned in this answerprepare 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 answerto 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.
  • 本答案所述,准备一个 start.bat 文件。
  • 将此文件放在 nginx.exe 所在的位置。
  • 打开 Windows 任务计划程序并按照此答案中的说明设置任务以无限期地运行它。
  • 不要忘记使用系统帐户以最高权限运行此任务,可以在此处找到更多详细信息。
  • 使任务每天在某个时间启动,通过bat文件检查服务是否已经在运行,避免创建多个nginx.exe实例。
  • 如果由于某种原因 Nginx 关闭,它会在 5 分钟内启动。

Windows Startup shortcut

Windows 启动快捷方式

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

  • Follow this answerto 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.
  • 创建一个nginx.exe的快捷方式,放到Windows的启动文件夹中。

  • 按照此答案查找您的启动位置。

  • 每当您登录系统时,Nginx 都会自动运行。
  • 这个是最简单的。但是,它取决于用户配置文件,即如果您在服务器上运行 Nginx,它只会为您的用户帐户运行,当您注销时它会停止。
  • 这是开发环境的理想选择。

回答by Immanuel Lechner

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

NSSM 非常好,但还有另一种选择: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://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-service?view=powershell-6

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

回答by tmjee

You'll need this for winsw

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)

您将需要<executable>假设您正在使用 nginx.conf 因此不需要任何启动参数以及 a<stopexecutable><stopargument>s (以模拟nginx -s stop