nginx - various strange rewrite requirements

This document documents some of the rewrite requirements I encountered in a production environment. Some conventional rewrite requirements may be ready in minutes, but some complex requirements take up to 4 hours to study. In order to avoid this phenomenon of repeated battles and defeats, it is necessary to record documents and shorten the time.

General requirements rewrite

General rewrite requirements are for SEO. Some URLs will jump with a status code of 302, which will affect Google's ranking. You need to change the url jump to 301 jump (permanent redirection). Therefore, you need to do 301rewrite through nginx.
Example:

rewrite /cn/service/onsite/detail https://support.oppo.com/cn/onsite-repair/ permanent;

No regular matching is required

After URL Rewrite with parameters - keep parameters

#Example:
#https://example.com/cn/show/?q=xxx -->
#https://example.com/cn/getinfo/?q=xxx

if ( $arg_q ) { rewrite /cn/show/ https://example.com/cn/getinfo/ permanent;}

remarks:

         Judge whether the parameter q exists/ cn/getinfo / does not end with?, So the parameters will be attached.

URL Rewrite with parameters - remove parameters

#Example:
#https://example.com/cn/show/?aid=1234 -->
#https://example.com/cn/getinfo/

if ( $request_uri = "/cn/show/?aid=1234" ) {rewrite (.*) https://example.com/cn/getinfo/? permanent;}

remarks:

         The uri obtained from the parameter $uri of nginx does not have parameters, which can be obtained through $request_uri gets a uri with parameters. So as to make if judgment.? Remove the forwarded parameters.

Cases where regular matching is required

After URL Rewrite with parameter - the key of the parameter becomes & value unchanged

#Example:
#https://example.com/cn/show/?q=xxxx --> 
#https://example.com/cn/getinfo/?kw=xxxx
#PS: the value xxxx is a variable. After 301rewrite, the key is changed, but the value remains unchanged.

if ( $arg_q ){ rewrite /cn/show/ https://example.com/cn/getinfo/?kw=$arg_q? permanent;}

url with parameter - value requiring regular matching parameter

#Example:
#https://example.com/cn/show?city=xxxx  (XXXX is 1-4 digits) - >
#https://example.com/cn/getinfo/
#ps: if the value of the parameter needs to meet the conditions, 301 forwards it to / cn/getinfo / without retaining the parameters.

if ( $arg_city ~ "[0-9]{1,4}" ) {rewrite /cn/show  https://example.com/cn/getinfo/? permanent;}

remarks:
         As mentioned earlier, you can use $request_ Uri get the URI with parameters to make if judgment. However, in the actual configuration process, it is found that if the values of the parameters are to be regularly matched, the values of the parameters in the URI cannot be normally matched by means of if ($request_uri ~ "/ cn/service/map?city = regular expression"). So $arg is used in the above example_ Parametername is a built-in parameter of nginx to regularize the value of matching parameters.

  url rewrite with multiple parameters

#Example:
#https://example.com/cn/shwo?id=123&name=456&age=20 -->
#https://example.com/cn/getinfo/
#Remove parameters after ps:301

set $cnshwo "";
if ( $arg_id = 123 ) {
  set $cnshwo "1";
}
if ( $arg_name = %456 ) {
  set $cnshwo "${cnshwo}1";
}
if ( $arg_age = 20 ) {
  set $cnshwo "${cnshwo}1";
}
if ( $help657 = 111 ) {
  rewrite (.*)$ https://example.com/cn/getinfo/? permanent;
}

remarks:

        nginx cannot write multiple if judgments. Therefore, to judge that multiple situations are satisfied at the same time, an intermediate variable can be used as the judgment basis.

Regular judgment is not the beginning or end of a field

#Rule: do not start with / cn /. *; html is included in the middle; Not with* ending.


if ( $request_uri ~* ^(?!/cn/.*).*.html.*(?<!\?).*$ ) { rewrite /(.*).htm$ https://other-example.com/$1/ permanent; }

remarks:

        Regular expression, ^ (?! str) does not start with str; (? <! str) $does not end with str.

Reference documents

https://segmentfault.com/a/1190000019266662https://segmentfault.com/a/1190000019266662

map optimizes a large number of rewrite scenarios

Scenario: if we have thousands of redirection rules to configure, and we don't use regular rules to reduce entries. In this case, in order to improve efficiency, map can be used.

First, we need to prepare the map file. One redirection per line, separated by spaces and terminated by colons.

File path: / var/www/vhosts/acme.com/redirects/redirect.map

The format is as follows:

/source-url-1 https://acme.com/dest-url-1:
/source-url-2 https://acme.com/dest-url-2:

Step 2: configure under http

map_hash_max_size 4096;
map_hash_bucket_size 256;
map $request_uri $new_uri {
  include /var/www/vhosts/acme.com/redirects/redirect.map
}

Step 3: under the corresponding server or server location, we use the path obtained from the map file to rewrite

if ($new_uri != "") {
	rewrite ^(.*)$ $new_uri permanent;
}

Reference documents

Nginx Redirects With Map File Reliable Penguin - BlogClient provided a CSV file with several thousand redirects for site hosted on Plesk server. For maximum efficiency we implemented the redirects using ahttps://blogs.reliablepenguin.com/2021/05/19/nginx-redirects-with-map-file

Tags: Nginx

Posted on Fri, 03 Dec 2021 06:46:11 -0500 by dotty