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 and nginx Modules (Theory)

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

Share

Shulou(Shulou.com)06/02 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

The implementation of Nginx Jump requirement in Rewrite practical scenario

Use rewrite for matching jumps

Jump after matching global variables with if

Use location matching and then jump rewrite to match domain names or parameter strings in server {}, if {}, location {} segments

Use if global variable matching

Use the regular expression metacharacters commonly used by proxy_pass reverse agents to describe ^ match the start position of the input string $match the end position of the input string * match the previous character zero or more times + match the previous character one or more times? Matches the preceding character zero or once. Match any single character except\ n, using a pattern such as "[.\ n]" Can match any character, including "\ n",\ d match pure number [0-9] {n} repeat {n,} repeat n or more times [c] match a single character c [a-z] match any [a-zA-Z] of the lower case letter of Amurz or any Rewrite command syntax of the uppercase letter of Amurz: rewrite [flag] Flag tags supported by rewrite for content after regular jump

The flag tag says:

The tag indicates that last is equivalent to the [L] tag of Apache, indicating that the completion of rewritebreak will be terminated upon completion of the matching of this rule. Redirect will no longer match any of the following rules. Redirect returns 302 temporary redirection, the browser address will display the jumped URL address, the crawler will not update the urlpermanent and return 301 permanent redirection, the browser address bar will display the jumped URL address, and the crawler will update the url.

Last and break comparison:

Lastbreak usage scenarios are generally written in server and if generally use URL matching in location to match url matching after non-terminating rewriting url matching location classification after termination rewriting: location = patt {} [precise matching] location patt {} [General matching] location ~ patt {} [regular matching]

Common expressions for regular matching:

The tag says ~ to perform a regular match, case sensitive ~ * to perform a regular match, not case sensitive! ~ perform a regular match, case-sensitive mismatch! ~ * performs a regular match, case-insensitive, case-insensitive, ^ ~ normal character matching; uses prefix matching. If the match is successful, the exact match of other location= normal characters is no longer matched. That is, exact match @ defines a named location, using expressions of the same type location priority in internal orientation, long characters will first match * sort by priority * * = type ^ ~ type expression regular expression (~ and ~ *) type regular string match type, usually match (/) by prefix match, 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]} # # priority location ~ / images/abc {[configuration H]} # # if compared with regular ~ / images/abc/1.html, regular priority is higher location / images/abc/1.html {[configuration I]} location priority rule

Nginx module

1the working principle of 1MagneNginx

Nginx consists of kernels and modules.

  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 modules: HTTP module, EVENT module and MAIL module

Basic modules: HTTP Access module, HTTP FastCGI module, HTTP Proxy module and HTTP Rewrite module

Third-party modules: HTTP Upstream Request Hash module, Notice module and HTTP Access Key module.

The modules of Nginx are functionally divided into the following three categories:

  Handlers (processor module): this kind of module processes requests directly, and performs operations such as outputting content and modifying headers information. 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 single worker process mode, in addition to the main process, there is also a worker process, which is single-threaded

  in 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.

Operation principle of 3.Nginx+FastCGI

  Nginx does not support direct calls or parsing of external programs, and 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.

Thank you for reading!

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