Home > Backend Development > PHP Tutorial > Basic introduction to nginx (based on official documentation)

Basic introduction to nginx (based on official documentation)

WBOY
Release: 2016-08-08 09:20:12
Original
980 people have browsed it

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.

nginx consists of a master process and several worker processes. The master process is mainly used to read configuration files and manage worker processes. The worker process is responsible for processing requests. nginx is based on the event model and can efficiently utilize worker processes to handle requests according to the characteristics of the operating system. The number of worker processes is defined in the configuration file. It can also be defined through a specified configuration file, or its number can be automatically determined based on the number of CPU cores.
The working mode of nginx and related modules is specified through configuration files. By default, the configuration file is named nginx.conf and is located in the /usr/local/nginx/conf or /etc/nginx or /usr/local/etc/nginx path.

The following content is related to nginx starting, shutting down, and reloading configuration files.

Starting, Stopping, and Reloading Configuration

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>
Copy after login

Where signal may be one of the following:

  • stop — fast shutdown
  • quit — graceful shutdown
  • reload — reloading the configuration file
  • reopen — reopening the log files

For 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
Copy after login
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
Copy after login

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
For example, to wait for the worker process to process the current request before shutting down nginx, you can use

nginx -s quit
Copy after login

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
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template