


Detailed explanation of nginx response and request processing methods
This article details the method of nginx server responding to and processing http and other requests, and explains the configuration method of nginx virtual host. Friends in need can refer to it.
First, nginx name-based virtual host
Nginx first selects which virtual host to handle the request.
Start with a simple configuration (where all 3 virtual hosts are listening on port *:80):
Copy code Code example:
server {
listen 80;
server_name jbxue.org www.jbxue.org;
...
}
server {
listen 80;
server_name jbxue.net www.jbxue.net;
...
}
server {
listen 80;
server_name jbxue.com www.jbxue.com;
…
}
In this configuration, nginx only checks the "Host" header of the request to determine which virtual host the request should be handled by. If the Host header does not match any virtual host, or the request does not contain a Host header at all, nginx will distribute the request to the default virtual host defined on this port. In the above configuration, the first virtual host listed is nginx's default virtual host - this is nginx's default behavior. Moreover, you can explicitly set a host as the default virtual host, that is, set the "default_server" parameter in the "listen" command:
Copy code Code example:
server {
listen 80 default_server;
server_name jbxue.net www.jbxue.net;
…
}
The "default_server" parameter is available starting from version 0.8.21. In previous versions, the "default" parameter should be used instead.
Please note that "default_server" is an attribute of the listening port, not the host name. More on this later.
How to prevent the processing of requests with undefined host names
If the "Host" header is missing from the request, you can define the following host and discard these requests:
Copy codeCode example:
server {
Listen 80; er server_name "" "";
Return 444;
}
Virtual host based on mixed domain name and IP
Copy codeCode example:
server {
listen 192.168.1.1:80;
server_name jbxue.org www.jbxue.org;
...
}
listen 192.168.1.1:80;
server_name jbxue. net www.jbxue.net;
...
}
listen 192.168.1.2:80;
server_name jbxue.com www.jbxue.com;
...
}
Copy codeCode example:
server { listen 192.168.1.1:80;
server_name jbxue. org www.jbxue.org;
...
}
server {
listen 192.168.1.1:80 default_server;
server_name jbxue.net www.jbxue.net;
...
}
server {
listen 192.168.1.2 :80 default_server;
server_name jbxue.com www.jbxue.com;
...
}
Second, a simple PHP site configuration
In a typical, simple PHP site, how nginx selects location to process a request:
Copy codeCode example:
server {
Listen 80;
server_name jbxue.org www.jbxue.org;
root /data/www;
location / {
index index.html index.php;
}
location ~* .(gif|jpg|p ng)$ {
expires 30d;
}
location ~ .php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
First, nginx uses prefix matching to find out The most accurate location. In this step, nginx will ignore the order in which locations appear in the configuration file.
In the above configuration, the only prefix matching location is "/", and because it can match any request, it is used as the last choice.
Then, nginx continues to match the locations of the regular expressions in the order in the configuration, and stops searching after matching the first regular expression.
The matched location will be used. If there is no location matching the regular expression, the location with the most accurate prefix match just found is used.
Please note that all location matching tests only use the URI part of the request, not the parameter part. This is because there are many ways to write parameters, such as:
/index.php?user=john&page=1
/index.php?page=1&user=john
In addition, anyone can add them at will in the request string. String:
/index.php?page=1&something+else&user=john
Let’s see how the request is processed using the above configuration:
The request "/logo.gif" first matches the location "/", and then matches The regular expression ".(gif|jpg|png)$". Therefore, it will be processed by the latter. According to the "root /data/www" command, nginx maps the request to the file /data/www/logo.gif" and sends this file to the client.
The request "/index.php" first also matches the location "/ ", and then matches the regular expression ".(php)$". Therefore, it will be processed by the latter and then sent to the FastCGI server listening at localhost:9000. The fastcgi_param directive sets the value of the FastCGI parameter SCRIPT_FILENAME to " /data/www/index.php", and then the FastCGI server executes this file. The variable $document_root is equal to the value set by the root command, and the value of the variable $fastcgi_script_name is the requested uri, "/index.php".
Request "/about .html" can only match the location "/", so it will use this location for processing. According to the "root /data/www" instruction, nginx maps the request to the file "/data/www/about.html", and Send this file to the client.
The processing of the request "/" is more complicated. It can only match the location "/", so it will be processed using this location.
Then, the index directive uses its parameters and " root /data/www" command to detect whether the corresponding file exists.
If the file /data/www/index.html does not exist and /data/www/index.php exists, this command will be executed once Internal redirection to "/index.php", then nginx will re-find the location matching "/index.php", as if the request was sent from the client
As seen before, this redirection. The request is finally handed over to the FastCGI server for processing
The above has introduced a detailed explanation of nginx's method of responding to and processing requests, including relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

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.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.