#Nginx uses a multi-process model to provide external services, including a master process and multiple worker processes. The master process is responsible for managing Nginx itself and other worker processes.
All actual business processing logic is in the worker process. There is a function in the worker process that executes an infinite loop, continuously processing the requests received from the client, and processing them until the entire Nginx service is stopped. (Recommended learning: nginx use)
In the worker process, the ngx_worker_process_cycle() function is the processing function of this infinite loop.
In this function, the simple processing flow of a request is as follows:
The mechanism provided by the operating system (such as epoll, kqueue, etc.) generates related events.
Receive and process these events. If data is received, a higher-level request object is generated.
Process the header and body of the request.
Generate a response and send it back to the client.
Complete the request processing.
Reinitialize timers and other events.
Request processing process
In order to let everyone better understand the request processing process in Nginx, we take HTTP Request as an example to explain in detail.
From within Nginx, the processing of an HTTP Request involves the following stages.
Initialize HTTP Request (read data from the client and generate an HTTP Request object, which contains all the information of the request).
Process request headers.
Process the request body.
Call the handler associated with this request (URL or Location), if any.
Call each phase handler in sequence for processing.
Here, we need to understand the concept of phase handler. Phase literally means stage. So phase handlers are easy to understand, they are handlers that contain several processing stages.
In each stage, there are several handlers. When processing reaches a certain stage, the handlers of that stage are called in turn to process the HTTP Request.
Normally, a phase handler processes this request and produces some output. Usually a phase handler is associated with a location defined in a configuration file.
A phase handler usually performs the following tasks:
Get location configuration.
Produce an appropriate response.
Send response header.
Send response body.
When Nginx reads the header of an HTTP Request, Nginx first searches for the configuration of the virtual host associated with the request. If the configuration of this virtual host is found, then usually, this HTTP Request will go through the following stages of processing (phase handlers):
NGX_HTTP_POST_READ_PHASE: Read request content phase
NGX_HTTP_SERVER_REWRITE_PHASE: Server request address rewrite phase
NGX_HTTP_FIND_CONFIG_PHASE: Configuration lookup phase:
NGX_HTTP_REWRITE_PHASE: Location request address rewrite phase
NGX_HTTP_POST_REWRITE_PHASE: Request address rewrite submission Phase
NGX_HTTP_PREACESS_PHASE: Access permission check preparation phase
NGX_HTTP_ACCESS_PHASE: Access permission check phase
NGX_HTTP_POST_ACCESS_PHASE: Access permission check submission phase
NGX_HTTP_TRY_FILES_PHASE: Configuration item try_files Processing phase
NGX_HTTP_CONTENT_PHASE: Content generation phase
NGX_HTTP_LOG_PHASE: Log module processing phase
In the content generation phase, in order to generate a correct response to a request, Nginx must Leave it to an appropriate content handler to handle.
If the location corresponding to this request is explicitly specified as a content handler in the configuration file, then Nginx can directly find the corresponding handler by matching the location and hand the request to the content handler. to deal with. Such configuration directives include perl, flv, proxy_pass, mp4, etc.
If the location corresponding to a request does not directly have a configured content handler, then Nginx will try in sequence:
If a location has random_index on configured, then randomly select A file sent to the client.
If an index directive is configured in a location, then the file specified by the index directive is sent to the client.
If autoindex on is configured in a location, then the file list under the server path corresponding to the request address is sent to the client.
If gzip_static on is set on the location corresponding to this request, then check whether a corresponding .gz file exists, and if so, send this to the client (if the client supports gzip).
If the requested URI corresponds to a static file, the static module will send the content of the static file to the client.
After the content generation phase is completed, the generated output will be passed to the filter module for processing.
The filter module is also related to location. All fitter modules are organized into a chain. The output will pass through all filters in sequence until a return value from the filter module indicates that the processing has been completed.
Here are some common filter modules, such as:
server-side includes.
XSLT filtering.
Image scaling and the like.
gzip compression.
Among all filters, there are several filter modules that need attention. The instructions are as follows in the order of calls:
write: Write output to the client, actually writing to the socket corresponding to the connection.
postpone: This filter is responsible for subrequest, that is, subrequest.
copy: Copy some bufs (files or memory) that need to be copied and then hand them over to the remaining body filter for processing.
The above is the detailed content of How to handle requests using Nginx. For more information, please follow other related articles on the PHP Chinese website!

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment