How to redirect a directory path in nginx

Author : xuzhiping   2022-11-01 13:44:53 Browse: 1622
Category : JavaScript

Abstract: How do I redirect a directory path in nginx? In order to solve this problem, this article introduces the corresponding analysis ...

How do I redirect a directory path in nginx? In order to solve this problem, this article introduces the corresponding analysis and answer in detail. I hope it can help more partners who want to solve this problem to find a more simple and easy way.

nginx

1.nginx modify root mapping

It is the easiest way to modify root mapping to realize nginx directory access redirection, which is recommended.

Location / image {
Root / folderName
}

2. Access redirection through nginx rewrite internal redirection

Example of nginx configuration code:

Location / image {
Rewrite ^ / image/ (. *) $/ folderName/image/$1 last
}

3. Implementation of alias alias mapping for nginx settings

Example configuration:

Location / image {
Alias / folderName/image; # write the absolute path here
}

4. Realized by permanent 301absolute jump of nginx

Example configuration:

Location / image {
Rewrite ^ / image/ (. *) $http://dashidan.com/folderName/image/$1;
}

5. Realize page jump by judging uri

Example configuration:

If ($request_uri ~ * ^ (/ image)) {
Rewrite ^ / image/ (. *) $/ folderName/image/$1 last
}

Nginx location matching rules

Location matching command

  • ~# The wavy line indicates that a regular match is performed, which is case sensitive.
  • ~*# means to execute a regular match, not case sensitive.
  • ^~# ^~indicates normal character matching. If this option matches, only this option will be matched, and no other options will be matched. It is generally used to match directories.
  • =# Exact matching of ordinary characters.
  • @# "@" defines a named location, which is used for internal orientation, such as error_ page, try_ files.

and the following four points:

  • =Prefix directives strictly match this query. If found, stop searching.
  • All the remaining regular strings, the longest match. If this match uses the ^~prefix, the search stops.
  • Regular expression, the order defined in the configuration file.
  • If the third rule produces a match, the result is used. Otherwise, it is used as if from rule 2.

Priority of location match (regardless of the order of location in the configuration file)

  • Exact matching will be the first to be processed. If an exact match is found, nginx stops searching for other matches.

  • Normal character matching, regular expression rules and long block rules will be matched first with the query That is, if the match also needs to see if there is a regular expression match and a longer match.

  • ^ ~ only this rule is matched, and nginx stops searching for other matches, otherwise nginx will continue to process other location instructions.

  • Finally, match the instructions with "~" and "~ *". If the corresponding match is found, nginx stops searching for other matches. When there is no regular expression or no regular expression is matched, then the most matching verbatim matching instruction will be used.

Location = / {
# Only match "/".
[configuration A]
}
Location / {
# Match any request, because all requests start with "/"
# But longer character matching or regular expression matching takes precedence
[configuration B]
}
Location ^~ / images/ {
# Match any request that starts with / images/ and stop matching other location
[configuration C]
}
Location ~*\. (gif|jpg|jpeg) ${
# Match requests that end with gif, jpg, or jpeg.
# But all requests for the / images/ directory will be processed by [Configuration C].
[configuration D]
}

This is the answer to the question about how to redirect the directory path in nginx. I hope the above content can be of some help to you, if you still have a lot of doubts to be solved. You can follow the billionaire Cloud Industry Information Channel to learn more about it.

Label :
    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).