SoFunction
Updated on 2025-04-11

Implementation of redirection in nginx

1. Location

1. Location Matching

The location matches the URI behind

/wordpress

192.168.100.11/wordpress

2. Location matching classification

2.1 Exact Match

location = / Completely match the string, it must be fully matched

2.2 Regular Match

^~What is the prefix matching?

~Case sensitive match

~*Case insensitive

!~: case sensitive inverse

!~*: Insensitive case insensitive inverse

2.3 General matching (general matching)

location/string

3. Location Match Priority*

The highest priority of exact match is followed by regularity, and finally the general match

3.1 Match priority experiment

systemctl stop firewalld
setenforce 0
systemctl restart nginx
cd /usr/local/nginx/html/
Drag the photo in

vim /usr/local/nginx/conf/
location = / {
   root    /data/nginx/static1;
   index   ;
}
location / {
   root   /data/nginx/static2;
   index  ;
}
location ~*\.(jpglgif)$ {
    root   /data/nginx/static3;
    index  ;
}
wq!

nginx -t
systemctl restart nginx

mkdir -p /data/nginx/static1
cd /data/nginx   #See only one static1mkdir static2 static3   #See there is static1 static2 static3cd /usr/local/nginx/static1/
Drag in
cd /usr/local/nginx/static2/
Drag in
cd /usr/local/nginx/static3/
Drag in
[root@localhost static3]# mv  
[root@localhost static3]# ls


Page access
192.168.100.11/   # The picture you are visiting at this time
Page access
Comment out the exact match
nginx -t 
systemctl restart nginx
192.168.100.11/   # The picture you are visiting at this time

3.2 Priority summary

location = complete path = that is, a complete word cannot be missing

location ^~location ~ location ~*, location / (string) part start position, location /

4. The rules of use in actual websites

The first required rule

The website home page uses precision equal to bar (= /)

1.1 Home page

location = / {
   root html;
   index    ;
}
# = / indicates the beginning of the home directory, access = / is equivalent to visiting the home page of the website

The second required rule

Pages that handle static requests

1.2 Used to match static pages

location ^~ /static/ {
    root /web/static/;
    index  ;
}

1.3 Access the image or specified suffix name

location ~* \.(jpg|gif|jpeg|png|css)$ {
    root /web/pictures/;
    index  ;
}

The third rule

Generally, it is a general rule to forward dynamic requests with .php .js as the suffix to the backend server (database)

1.4 Forwarding backend requests and load balancing

location / {
  proxy_pass
}

2. Rewrite redirection

1. Concept

rewrite is to redirect the currently visited page to other pages.

2. Working method

Redirecting url through global variables or custom variables of nginx, combined with regular expressions and flags.

3. Nginx variables

$uri: The URL address requested by the client

$host: The requested host name

#http_user_agent: The browser and operating system requested by the client

$http_referer: The referer information of the request header, indicating the URL from the current page

$remote_addr: The IP address of the client

$remote_port: The client's port

$server_addr: The IP address of the server

$server_port: The port of the server

$request_method: Method to obtain client request

$scheme: The requested protocol, either http or https

x_forwarded_for: Used to obtain the real IP address of the client in the request header. Adding a proxy server is only the client's IP address in the proxy server.

X_Real_IP: The real IP address of the client

4. Nginx real IP address access settings

vim 
proxy_set_header X Real-IP $remote_addr 
# Add this field and the client's real IP address will be passed to the backend server

operate

vim /usr/local/nginx/conf/
existserverModification,Modified as follows

location / {
   root   html;
   default_type text/plain;
   return 200 "ip:$remote_addr";
}
wq!

systemctl restart nginx

Page access 192.168.100.11  
#The local address is displayed at this time ip 192.168.100.1
# return 200 "ip:$remote_addr\ntest1:$host\nport:$remote_port\nxieyi:$scheme";

systemctl restart nginx
Page access
# ip:192.168.100.1
# test1:192.168.100.11
# port:51360
# xieyi:http

5. Flag bit (flag)

permanent: Permanent redirection, return code is 301, the browser address bar will display the redirected URL address

redirect: Temporary redirection, return code is 302, the browser address bar will display the redirected URL address

break: Permanent redirection, the return code is 301, but after it matches the rule, it will not match other rules downwards, and the url will not change

last: Redirect, but will continue to match other location rules downwards

6. The execution order of rewrite

6.1 The server module's rewrite has the highest priority

6.2 Rules for matching location

6.3 Execute the selected location rules

7. Rewrite syntax

rewrite Regular expressions Content after jump Logo position;

7.1 Experiment

cd /usr/local/nginx/html
mkdir test1 
mkdir xy102
cd /usr/local/nginx/html/test1
echo 123 > 
cd /usr/local/nginx/html/xy102
echo 456 > 
# Check whether the test1 and xy102 directories are created
(1)
vim /usr/local/nginx/conf/
existserverModification,Modified as follows

location / {
   root   html;
   rewrite /test1/(.*) /xy102/$1 permanent;
#192.168.100.11/test1/ 192.168.100.11/xy102/ $1 is the capture group   index ;
}
wq!

nginx -t
systemctl restart nginx

Page access
192.168.100.11/test1
# The access result is 456, and the website ip jumps to 192.168.100.11/xy102
(2)
vim /usr/local/nginx/conf/
existserverModification,Modified as follows

location /test1 {
   root   html;
   rewrite /test1/(.*) /test2/$1 last;
   index ;
}
location /test2 {
   root   html;
   rewrite /test2/(.*) /test1/$1 last;
   index ;
}
wq!

nginx -t
systemctl restart nginx
cd /usr/local/nginx/html
mkdir test2
cd /usr/local/nginx/html/test2
echo 789 > 

Page access
192.168.100.11/test1
# The access result is 500
Open another one now192.168.100.11ofshellterminal
[root@localhost opt]# tail -f /usr/local/nginx/logs/
Continue to visit the page 192.168.100.11/test1
xshellThe following sentence appears in the error:rewrite or internal redirection cycle while processing
explain:
# It means that during the redirection process, the last method is used for redirection, but there is no ending and it falls into a dead loop. Nginx will automatically loop 10 times, and the last match can only be executed at most 10 times. If it does not end more than 10 times, it will stop, and then an error of 500 will be reported.

7.2 Jump based on domain names. Old domain names are not used, but they can still be accessed, and all jump to new domain names

vim /usr/local/nginx/conf/
existserverModification,Modified as follows

    # Modify server_name as follows    server_name   ;
    
    # Cancel the comment of charset koi8-r;, modify it as follows    charset ulf-8;

location / {
   root   html;
   if ( $host = '' ) {
      rewrite ^/(.*)$ /$1 permanent;
   }
   index ;
}
wq!

nginx -t
systemctl restart nginx

Domain name mapping
vim /etc/hosts
192.168.100.11  
wq!
# Visit/is access/
cd /usr/local/nginx/html
echo "this is new page" > 

Page access(exist虚拟机)

# Print result is this is new page Web page address jump

7.3 The client-based IP redirects, the company has new business online, and in the testing stage, other IPs can only be displayed and maintained, and only 192.1168.100.11 can access normally

vim /usr/local/nginx/cong/
existserverModularaccess_logAdd

set $rewrite true;
# Set a variable name, rewrite, the value is ture# to determine whether an IP is a legal IPif ( $remote_addr = "192.168.100.11" ) {
      set $rewrite false;
}
if ( $rewrite = true ) {
      rewrite (.+) /;
      # Redirect, 192.168.100.13/}
location = / {
   root html;
   
}
wq!

cd /usr/local/nginx/html
echo "Web page maintenance" > 

nginx -t
systemctl restart nginx

Page access
192.168.100.11    #Print result is page maintenance

Summarize·:

priority of location matching

This is the end of this article about the implementation of redirection in nginx. For more related nginx redirection content, please search for my previous articles or continue browsing the related articles below. I hope you will be in the future.