1.需要借助"Windows Service Wrapper"小工具,項目地址: https://github.com/kohsuke/winsw (供學習)
2.下載地址: http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/1.18/winsw-1.18-bin.exe
3.下載該工具后,將其放在 Nginx安裝目錄下,並重命名為nginx-service.exe,創建配置文件nginx- service.xml(名字要和工具名一樣)(注意:存放nginx的目錄中不能存在中文,否則會導致發布失敗)
4.創建nginx-service.exe.config(為支持NET 4.0 runtime,默認只支持NET 2.0 runtime)

5. nginx-service.xml 內容如下:(記得修改路徑)
<service> <id>nginx</id> <name>Nginx Service</name> <description>High Performance Nginx Service</description> <logpath>D:\Web\nginx\logs</logpath> <log mode="roll-by-size"> <sizeThreshold>10240</sizeThreshold> <keepFiles>8</keepFiles> </log> <executable>D:\Web\nginx\nginx.exe</executable> <startarguments>-pD:\Web\nginx</startarguments> <stopexecutable>D:\Web\nginx\nginx.exe</stopexecutable> <stoparguments>-pD:\Web\nginx -s stop</stoparguments> </service>
6. nginx-service.exe.config 內容如下:
<configuration> <startup> <supportedRuntime version="v2.0.50727" /> <supportedRuntime version="v4.0" /> </startup> <runtime> <generatePublisherEvidence enabled="false"/> </runtime> </configuration>
7.重點:以管理員的身份運行cmd,跳轉到nginx所在的目錄下,安裝nginx的服務,執行命令:
nginx-service.exe install
卸載:
nginx-service.exe uninstall 或者sc delete 服務名


這時,nginx的windows服務便已經安裝好了,運行-->services.msc 打開服務列表

8.啟動服務,關閉服務的命令
啟動:net start nginx

關閉:net stop nginx

注意: 安裝成服務后,nginx -s reload 不可用,會報錯
9.重啟計算機,再打開服務列表查看,這時候服務已經是自啟動的了
參考:https://www.dazhuanlan.com/2019/12/16/5df6fbb4975a8/
