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

How to understand nginx rewrite and nginx-ingress rewrite

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to understand nginx rewrite and nginx-ingress rewrite. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

There is a proxy_pass instruction in the configuration of nginx

Location / name/ {proxy_pass http://127.0.0.1/remote/;}

In a configuration like this, uri / remote is added after the proxy_pass, which will replace the part of the normal request uri that matches the location with / remote. I have a similar scenario: accessing the aa.xxx.net/gateway/a/b proxy to the backend is similar to accessing the svc-gateway:8080/a/b. How to configure it? Cut off the gateway. The first thing that comes to mind is to use rewrite annotations in ingress to solve the problem.

Rewrite of nginx

Refer to the official documentation link

The ngx_http_rewrite_module module uses the PCRE regular expression to change the request URI, returning a redirection or conditionally selecting the configuration.

The break, if, return, rewrite, and set instructions are executed in the following order:

Server blocks are executed sequentially

Execute the module instructions within the matched location block

Loop redirection up to 10 times

Break instruction

Stops processing the current ngx_http_rewrite_module instruction set. If break is included in the if instruction in the location, other instructions in the location continue to execute.

Rewrite instruction Syntax: rewrite regex replacement [flag]; Default:-Context: server, location, if

If the specified regular match request uri,uri will be replaced by replacement. The rewrite is executed in the order in which it appears in the configuration file. The following is an example:

Location / {rewrite ^ / a / b; # return 200 okokok; rewrite ^ / b / c last; rewrite ^ / d / b;} location = / b {return 401;} location = / c {return 402;}

Like the configuration above, accessing localhost:port/d will return 401.

If rewrite does not specify flag, it will continue to execute

You can use the flags option to terminate subsequent instructions for processing. If the replacement starts with http://, https://, or $scheme, the request will not be executed later and the redirect will be returned directly to the client

Flag option 1. Last stops processing the current ngx_http_rewrite_module instruction set and rematches the new location 2, break option and break instruction with the replaced uri. When no longer matches the new location 3, redirect,replacement does not start with 'http://', etc., temporary redirection 3024, permanent, and permanent redirection 301 are returned.

The complete redirect url is generated in the format of the requested scheme and server_name_in_redirect and port_in_redirect instructions

Rewrite comments in nginx-ingress

Official link

Example:

... Nginx.ingress.kubernetes.io/rewrite-target: / $1...spec: rules:-host: aa.xxx.net http: paths:-backend: serviceName: svc-gateway servicePort: 8080 path: / gateway/ (. *)-backend: serviceName: svc-ui servicePort: 80 path: / (. *) tls:-hosts:-aa.xxx.net secretName: xxx.net

In version 0.22.0 or later, any substrings in the request URI that need to be passed to the rewrite path must be explicitly defined in the capture group. The first group is represented by $1 and can be used as a parameter to rewrite-target.

After using rewrite-target annotations, all path are forced to use rules that ignore case, similar to those in the nginx configuration:

Location ~ * "^ / foo/.*" {...}

The spec.rules.host field does not support norm matching and regularization like nginx, so you must write a complete domain name.

In order to achieve more accurate path matching, ingress-nginx first sorts the paths in descending order by length, including regular characters, before writing the paths as location blocks to the nginx template. Like nginx, the location that matches first in regular mode is executed directly without further matching.

Extended Reading: ingress-path-matching

Understanding Nginx Server and Location Block Selection Algorithms

The second way:

... Kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/server-snippet: | if ($uri ~ * "/ gateway/.*") {rewrite ^ / gateway/ (. *) / $1 break } nginx.ingress.kubernetes.io/use-regex: "true"... spec: rules:-host: aa.xxx.net http: paths:-backend: svc-gateway servicePort: 8080 path: / gateway (/ | $) (.*)-backend: serviceName: svc-gateway servicePort: 8080 path: / (admin | bg | auth | monitor) -backend: serviceName: svc-ui servicePort: 80 path: / tls:-hosts:-aa.xxx.net secretName: xxx.net

The nginx.ingress.kubernetes.io/server-snippet annotation is used to customize the configuration in the server block, and you can write the configuration of nginx to achieve more requirements. Each server block can only be used once. In this way, requirements similar to proxy_pass are realized.

On how to understand nginx rewrite and nginx-ingress rewrite to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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