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

Detailed explanation of the use of custom parameters in MySQL

MySQL variables include system variables and syst...

Basic application methods of javascript embedded and external links

Table of contents Basic application of javascript...

Solution to Linux CentOS 6.5 ifconfig cannot query IP

Recently, some friends said that after installing...

Linux Disk Quota Management Graphical Example

Disk quota is the storage limit of a specified di...

The most commonly used HTML tags to create web pages

1. Optimization of commonly used HTML tags HTML s...

How to use VirtualBox to build a local virtual machine environment on Mac

1. Big Data and Hadoop To study and learn about b...

MySQL 8.0.11 compressed version installation tutorial

This article shares the installation tutorial of ...

Mysql database scheduled backup script sharing

BackUpMysql.sh script #!/bin/bash PATH=/bin:/sbin...

15 JavaScript functions worth collecting

Table of contents 1. Reverse the numbers 2. Get t...

A brief analysis of the best way to deal with forgotten MySQL 8 passwords

Preface Readers who are familiar with MySQL may f...

MySQL Database Indexes and Transactions

Table of contents 1. Index 1.1 Concept 1.2 Functi...

Ubuntu20.04 VNC installation and configuration implementation

VNC is a remote desktop protocol. Follow the inst...

Detailed explanation of Vue component reuse and expansion

Table of contents Overview Is the extension neces...