[英]run nginx as windows service
我正在尝试将 nginx(反向代理)作为 Windows 服务运行,以便即使在用户未连接的情况下也可以代理请求。
我四处搜索,发现应该从 .exe 文件(例如 nginx)创建服务的winsw 。
我在网上发现很多教程说创建一个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>
(我在 c: o 下名为 nginx 的文件夹中有 nginx.exe 路径正确)。
现在的问题是该服务已创建但我似乎无法启动它,每次我尝试启动它时都会弹出一个窗口说
Error 1053: The service didn't respond to the start or control request in a timely fashion
有谁知道我该如何解决这个问题或以不同的方式将 nginx 作为窗口服务运行?
刚刚在这里偶然发现并设法使用这个免费的开源替代方案进行工作: https : //nssm.cc/
它基本上只是一个帮助您创建服务的 GUI。 我使用的步骤:
从那时起你应该很高兴。
NSSM 非常好,但还有另一种选择:PowerShell Cmdlet New-Service
这里只是一个简单的例子:
$params = @{
Name = "MyService"
BinaryPathName = "path/to/exe"
DisplayName = "My Service"
StartupType = "Automatic"
Description = "Description of my service"
}
New-Service @params
我找到了NSSM以外的其他解决方案。 那是 Windows 服务包装器,以下是说明:
通过github下载最新版本的 Windows Service Wrapper。
将 winsw-xxxx.exe 重命名为类似 nginxservice.exe 的名称。
<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>
您可以在配置 GitHub 页面上找到有关配置的最新详细信息,以及在此处显示所有可能选项的通用示例。
以上答案摘自一个帖子。
我找到了NSSM (Non-Sucking Service Manager):一个程序完全符合我的要求,而且设置起来要容易得多。
正如其他答案中所说,NSSM 是将 Nginx 作为服务运行的最佳工具。
如果您不想使用任何外部 3rd 方软件,那么您可以实现这两种方法中的任何一种。
Windows 任务计划程序
Windows 启动快捷方式
创建一个nginx.exe的快捷方式,放到Windows的启动文件夹中。
按照此答案查找您的启动位置。
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>
您将需要一个<executable>
假设您正在使用 nginx.conf 因此不需要任何启动参数以及<stopexecutable>
和<stopargument>
s(以模拟nginx -s stop
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.