Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nginx Rewrite module (contains an overview of Nginx module)-theoretical chapter

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Rewrite jump scenario URL looks more standardized. Reasonable enterprises will disguise dynamic URL addresses as static addresses to provide service URLs for new domain names, and then allow the old access to be redirected to the new domain names. Some business adjustment Rewrite jump implementation on the server

Rewrite practical scenario 1, the implementation of Nginx jump requirements using rewrite matching jump using if matching global variables after jump using location matching and then jump 2, rewrite in server {}, if {}, location {} paragraph 3, use if global variables for domain names or parameter strings to match regular expression metacharacters commonly used by proxy_pass reverse agents

Rewrite command syntax: rewrite [flag]; flag tags supported by rewrite after regular jump

The flag tag says:

Last and break comparison:

Location classification: location = patt {} [exact match] location patt {} [general match] location ~ patt {} [regular match]

Common expressions for regular matching:

Location priority * * for expressions of the same type, long characters will first match * arrange by priority * * = type ^ ~ type expression regular expression (~ and ~ *) type regular string matching type, prefix matching usually matches (/), if there is no other match Any request will be matched to compare the similarities between rewrite and location** can be redirected * * different points * * rewrite is to change the path to get resources within the same domain name location is to control access or direction proxy for a class of paths You can also proxy_pass to other machines * * rewrite will write the execution order in location * * execute rewrite instructions in server block execute location match execute rewrite instructions in selected location location priority example # # exact match /, hostname cannot be followed by any string location = / {[configuration A]} # # all addresses begin with /, this rule will match all requests However, regular and longest strings first match location / {[configuration B]} # # matches any address that begins with / documents/, and location / documents/ {[configuration C]} # # matches any address that begins with / documents/abc when the subsequent regular expression does not match, and when the subsequent regular expression does not match Location ~ / documents/abc {[configuration D]} # # addresses starting with / images/. After matching, stop matching location ^ ~ / images/ {[configuration E]} # # match all requests ending in gif, and images under / images/ will be processed by [configuration E] because ^ ~ has a higher priority location ~ *\. (gif | jpg | jpeg) ${[configuration F]} # # longest character matches to / images/abc Lowest priority location / images/abc {[configuration G]} # # if the priority begins with / images/abc, the next priority is location ~ / images/abc {[configuration H]} # # if compared with regular ~ / images/abc/1.html Regular higher priority location / images/abc/1.html {[configuration I]} location priority rules match a specific file: (location = full path) > (location ^ ~ full path) > (location ~ full path) > (location ~ full path) > (location full path) > (location /) access a file with directory matching: (location = directory) > (location ^ ~ directory /) > (location ~ directory) > (location ~ directory) > (location directory) > (location /) Nginx module

1the working principle of Nginx Nginx consists of kernel and module.   Nginx itself actually does very little work. When it receives a HTTP request, it simply maps the request to a location block by looking up the configuration file, and the instructions configured in this location will start different modules to complete the work, so the module can be regarded as the real labor of Nginx.   usually involves one handler module and multiple filter modules for instructions in a location (of course, multiple location can reuse the same module). The handler module is responsible for processing the request and completing the generation of the response content, while the filter module processes the response content. The modules developed by users according to their own needs belong to third-party modules. It is with the support of so many modules that the function of Nginx is so powerful. The module of Nginx is divided into core module, basic module and third-party module: core module: HTTP module, EVENT module and MAIL module; basic module: HTTP Access module, HTTP FastCGI module, HTTP Proxy module and HTTP Rewrite module; third-party module: HTTP Upstream Request Hash module, Notice module and HTTP Access Key module. Nginx modules are functionally divided into the following three categories:   Handlers (processor module): these modules directly process requests, output content and modify headers information and other operations. Generally speaking, there can only be one Handlers processor module; Filters (filter module): this kind of module mainly modifies the output of other processor modules, which is finally output by Nginx; Proxies (proxy class module): such modules are modules such as HTTP Upstream of Nginx, which mainly interact with some back-end services such as FastCGI to achieve service proxy and load balancing functions. 2. The process model of Nginx. In terms of working mode, Nginx can be divided into two modes: single-process and multi-process.   in the single worker process mode, in addition to the main process, there is a worker process, the worker process is single-threaded;   in the multi-worker process mode, each worker process contains multiple threads. Nginx defaults to single worker process mode. After   Nginx starts, there will be one master process and multiple worker processes. The   master process is mainly used to manage the worker process, which mainly includes receiving signals from the outside world, sending signals to each worker process, monitoring the running status of the worker process, and automatically restarting the new worker process when the worker process exits (under abnormal circumstances). The master process acts as the interactive interface between the whole process group and the user and monitors the process at the same time. It does not need to deal with network events and is not responsible for business execution. It will only manage worker processes to restart services, smooth upgrades, change log files, and take effect in real time. 3.Nginx+FastCGI operating principle Nginx does not support the direct call or parsing of external programs, all external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is socket under Linux (this socket can be either a file socket or an ip socket). In order to call a CGI program, wrapper also needs a wrapper of FastCGI (wrapper can be understood as a program used to start another program), and this wrapper is bound to a fixed socket, such as a port or a file socket. When Nginx sends the CGI request to the socket, through the FastCGI interface, wrapper receives the request, and then Fork (derives) a new thread, which calls the interpreter or external program processing script and reads the returned data; then wrapper passes the returned data through the FastCGI interface, along the fixed socket to Nginx; and finally the Nginx sends the returned data (html page or picture) to the client.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report