Home > Article > Backend Development > Basic introduction to nginx (based on official documentation)
nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests. nginx employs event-based model and OS-dependent mechanisms to efficiently distribute requests among worker processes. The number of worker processes is defined in the configuration file and may be fixed for a given configuration or automatically adjusted to the number of available CPU cores (see worker_processes).
The way nginx and its modules work is determined in the configuration file. By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local /etc/nginx.
The following content is related to nginx starting, shutting down, and reloading configuration files.
To start nginx, run the executable file. Once nginx is started, it can be controlled by invoking the executable with the -s
parameter. Use the following syntax:
nginx -s <em>signal</em>
Where signal may be one of the following:
stop
— fast shutdownquit
— graceful shutdownreload
— reloading the configuration filereopen
— reopening the log filesFor example, to stop nginx processes with waiting for the worker processes to finish serving current requests, the following command can be executed:
nginx -s quit
This command should be executed under the same user that started nginx.
Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted. To reload configuration, execute:
nginx -s reload
Once the master process receives the signal to reload configuration, it checks the syntax validity of the new configuration file and tries to apply the configuration provided in it. If this is a success, the master process starts new worker processes and sends messages to old worker processes, requesting them to shut down. Otherwise, the master process rolls back the changes and continues to work with the old configuration. Old worker processes, receiving a command to shut down, stop accepting new connections and continue to service current requests until all such requests are serviced. After that, the old worker processes exit.
Once nginx is started, you can control nginx through the nginx -s [signal] command. Where [signal] The following commands can be used:
stop
— Quick stop quit
— Smooth shutdown reload
— Reload configuration file reopen
— Log files
nginx -s quit
When the configuration file is changed, the new configuration will only take effect when nginx is restarted or receives a command to reload the configuration file. In order to change the configuration file to take effect, you can use
nginx -s reload
Once the master process receives the signal to reload the configuration file, it will first check whether the new configuration file has syntax errors. If there are no errors, the master process will adopt the new configuration and start The new worker process also notifies the old worker process to stop working. Otherwise, if there is an error in the configuration file, the master process will still use the old configuration, and the old worker processes will continue to work. Once the master process notifies the worker process to stop working, the worker process will first stop receiving connections, then process all current requests, and then exit to end execution.
The above introduces the basic introduction of nginx (based on official documents), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.