How to redirect nginx directory path

How to redirect nginx directory path

If you want the path following the domain name to point to other directories on the local disk instead of the default web directory, you need to set up nginx directory access redirection. Application scenario: dashidan.com/image automatically jumps to dashidan.com/folderName/image. There are four ways to implement nginx directory path redirection. Modify the root mapping, redirect internally through Nginx rewrite, set up alias mapping, and implement it through nginx's permanent 301 absolute redirect.

1 nginx modifies root mapping

Modifying the root mapping to redirect nginx directory access is the simplest way and is recommended.

location /image {
 root /folderName;
}

2 Access redirection through nginx rewrite internal jump

Nginx configuration code example:

location /image {
 rewrite ^/image/(.*)$ /folderName/image/$1 last;
}

3 nginx alias mapping implementation

Configuration example:

location /image {
 alias /folderName/image; #Write the absolute path here}

4. Implemented through nginx's permanent 301 absolute redirect

Configuration example:

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

5. Redirect page by judging URI

Configuration example:

if ( $request_uri ~* ^(/image)){
 rewrite ^/image/(.*)$ /folderName/image/$1 last;
}

nginx location matching rules

location matching command

~ #The wavy line indicates a regular match, case-sensitive
~* # indicates to perform a regular match, not case sensitive
^~ #^~ indicates ordinary 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.
= # Perform an exact match of common characters
@ #"@" defines a named location, used for internal targeting, such as error_page, try_files

  1. = prefix matches this query exactly. If found, stop searching.
  2. Of all the remaining regular strings, the longest match. If this match is prefixed with ^~, the search stops.
  3. Regular expressions, in the order defined in the configuration file.
  4. If rule 3 produces a match, the result is used. Otherwise, the same rules as from rule 2 are used.

The priority of location matching (regardless of the order of locations in the configuration file)

= Exact matches are processed first. If an exact match is found, nginx stops searching for further matches.

Ordinary character matching, regular expression rules and long block rules will be given priority over query matching, which means that if the item matches, it is necessary to see if there is a regular expression match and a longer match.

^~ only matches this rule and nginx stops searching for other matches. Otherwise, nginx continues to process other location instructions.

Finally, the instructions with "~" and "~*" are matched. If a corresponding match is found, nginx stops searching for other matches. When there is no regular expression or no regular expression is matched, the verbatim match instruction with the highest degree of match will be used.

location = / {
 # Only matches "/".
 [ configuration A ] 
}
location / {
 # Matches any request, because all requests start with "/" # But longer character matches or regular expression matches will take precedence [ configuration B ] 
}
location ^~ /images/ {
 # Match any request starting with /images/ and stop matching other locations
 [ configuration C ] 
}
location ~* \.(gif|jpg|jpeg)$ {
 # Matches requests ending with gif, jpg, or jpeg. 
 # But all requests to the /images/ directory will be handled by [Configuration C]. 
 [ configuration D ] 
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of nginx configuration URL redirection-reverse proxy
  • Detailed explanation of redirection function in Nginx
  • How to set up 301 redirection in ngin configuration and 301 redirection in nginx subdirectory
  • Configure nginx to redirect to the system maintenance page

<<:  MySQL 5.7.10 winx64 installation and configuration method graphic tutorial (win10)

>>:  WeChat applet selects the image control

Recommend

Web front-end development CSS related team collaboration

The front-end development department is growing, ...

React's context and props explained

Table of contents 1. context 1. Usage scenarios 2...

How to use ElementUI pagination component Pagination in Vue

The use of ElementUI paging component Pagination ...

React Hooks Detailed Explanation

Table of contents What are hooks? Class Component...

Detailed explanation of Linux index node inode

1. Introduction to inode To understand inode, we ...

Four ways to combine CSS and HTML

(1) Each HTML tag has an attribute style, which c...

HTML/CSS Basics - Several precautions in HTML code writing (must read)

The warning points in this article have nothing t...

Using Docker to create static website applications (multiple ways)

There are many servers that can host static websi...

Summary of commonly used multi-table modification statements in Mysql and Oracle

I saw this question in the SQL training question ...

How to install Postgres 12 + pgadmin in local Docker (support Apple M1)

Table of contents introduce Support Intel CPU Sup...

Basic structure of HTML documents (basic knowledge of making web pages)

HTML operation principle: 1. Local operation: ope...

Detailed explanation of as, question mark and exclamation mark in Typescript

1. The as keyword indicates an assertion In Types...

Practical method of upgrading PHP to 5.6 in Linux

1: Check the PHP version after entering the termi...