Win10 uses nginx, registers to the service settings to start and run in the background to solve the problem of Access is denied.

Install nginx

Download

nginx official website download page: https://nginx.org/en/download.html
Simply select the latest window version or find the version you need and download it
image.png

Installation and use

After the download is completed, there will be a compressed package file, which can be directly extracted to the directory you need. It requires no installation and can be used immediately after decompression.
image.png
For simple tests, you can directly double-click the nginx.exe file to run in the foreground mode. Or use start nginx in cmd console to run in background

These two running modes are based on the current account. If the account exits, the process will also be closed.
Therefore, if it is used on the server, it needs to be registered in the service so that it can survive and start even after the server restarts abnormally.

Here are some commonly used methods to register services:

Configure nginx to start automatically at boot

nginx registered to the service

WinSW-x64
Download

Download address: https://github.com/winsw/winsw/releases
It is recommended to download from the github repository to avoid downloading versions containing malicious scripts elsewhere.
Find the latest stable version, and then select the file for your corresponding operating system to download.
image.png

Configuration

Its usage principle is that winsw.exe itself is registered as a windows service and can be set to self-start.
When it starts, it executes the set commands in accordance with the configuration in the xml file with the same name as the exe to achieve the effect of self-starting.

1. We copy the downloaded WinSW-x64 to the same directory as nginx.exe and rename it: nginx-service
It is not necessary to change the name. The name can be arbitrary. The purpose of changing the name is to facilitate later maintenance and search and use in the service.
2. Then create a file in the same directory as nginx.exe: nginx-service.xml
Note that the file name needs to be consistent with the name set in the first step, otherwise the instruction to register the service also needs to specify the path of the xml file
The contents of the xml file are as follows:

<service>
  <id>apm-nginx</id>
  <name>apm-nginx</name>
  <description>apm-nginx-self-starting service</description>
  <logpath>D:\Program Files\
ginx-1.25.2\server-logs\</logpath>
  <logmode>roll</logmode>
  <depend></depend>
  <executable>D:\Program Files\
ginx-1.25.2\
ginx.exe</executable>
  <stopexecutable>D:\Program Files\
ginx-1.25.2\
ginx.exe -s stop</stopexecutable>
</service>

Detailed introduction to file nodes: https://github.com/winsw/winsw/blob/v3/docs/xml-config-file.md
Introduction to commonly used nodes:

  • id:Specifies the ID that Windows uses internally to identify the service. This must be unique among all services installed on the system
  • name: The short display name of the service, also needs to be unique. not too long
  • description: description information
  • logpath: error log generated when WinSW drives nginx
  • logmode: log mode
  • executable: executable file path, the path of nginx that needs to be driven
  • stopexecutable: command to stop nginx
Use

Run the cmd window with administrator rights, and then enter the directory where nginx-service is located
Or enter cmd directly on the path, then run the following command, and then follow the prompts to authorize execution.
image.pngimage.png
Run command:

nginx-service.exe install

Then we can go to the task manager to view the service startup status and set the startup configuration of the service
image.png
The configuration of the service will not be described in detail here.
Then we come to the details and we can find that nginx is started together with the nginx-service we just configured
After stopping the apm-nginx service, the following three processes are stopped together.
image.png

WinSw common commands

nginx-service.exe is the renamed WinSW name

nginx-service.exe installl installation service
nginx-service.exe start starts the service
nginx-service.exe stop stop service
nginx-service.exe restart restart the service
nginx-service.exe uninstall delete service
nginx-service.exe status View status
NSSM
Download

Official website download link: https://nssm.cc/download
Download the stable version directly
image.png
Directory structure after downloading and decompressing:
image.png

Installation

Go to the directory corresponding to the system
Enter the following command to invoke the nssm panel

nssm install

image.png
Select the exe file path and set the service name and click install.
image.png

Use

Use the same as WinSW, the same as ordinary management services
image.png

nssm common commands
nssm install servername //Create servername service
nssm start servername //Start the service
nssm stop servername //pause service
nssm restart servername //Restart the service
nssm remove servername //Delete the created servername service

Add to system boot plan

Find Task Scheduler in the search bar and expand Microsoft>Windows
image.png
Add basic tasks
image.png
Set to run at startup, the operation is to start the program
image.png
Select the nginx.exe executable file
It should be noted that the start value must be filled in. The value filled in is the file path where nginx is located, such as: D:\Program Files\
ginx-1.25.2
You can see this setting in the previous nssm configuration
image.png
After the creation is completed, you can directly click Run to start, or restart the computer to start.
image.png
In order to enable nginx to run without a user, you need to make some settings
If the task is created by creating a task, it can be set at the time of creation.
image.png

Some questions

The configuration file cannot be reloaded (Access is denied)

nginx: [error] OpenEvent(“Global\
gx_reload_24248”) failed (5: Access is denied)

Problem analysis:
  • Because the Windows account runs an isolation mechanism, we cannot directly close programs started by other users, but we can kill the process through pid.
  • Because nginx reloads the configuration service without stopping, it is controlled by a live process. When reloading, nginx will create a new process and then redirect all subsequent traffic to the new process. When all the traffic in the process is processed, Old processes will be closed. This allows nginx to reload the configuration without restarting the service.
  • This problem is caused by the above error when shutting down the old nginx process.
Solution
How to host windows services with nssm and windsw

This method allows you to use the system’s administrator account as the service startup account.
image.png
The administrator account is not enabled by default and the password is unknown.
You can use other tutorials to enable administrator and modify the default account. No details here

Some can also use the PSEXEC program to perform controlled restarts. It is generally not recommended to use this tool on the server.

C:\Soft\PSTools\PSEXEC -s D:\Program Files\
ginx-1.25.2\
ginx.exe -p c:\
ginx -s reload
How to use Task Scheduler

Starting using this method will have the same problem, but we can use the task execution account to avoid this error.
We create a command file under the same level as nginx.exe for
reload.bat:

nginx -s reload

Then create two tasks
image.png
Creating tasks remains basically unchanged
apm-nginx:
image.png
Then create an apm-nginx-reload for reloading the configuration file
image.png
Just set the trigger to start once. We don’t need it to start at boot. We just need to execute it manually. Or set some event changes to start, but this is complicated
image.png
Operation: We just use it to execute the bat file we just created.
image.png
Then it’s OK. When we need to reload the configuration file, we can manually run the apm-nginx-reload scheduled task.
The main thing is that the execution account of the two tasks needs to be the same. Generally, the system or network account can be used.