nginx address redirect method

  • 2020-05-15 03:30:17
  • OfStack

1. Suppose you want to redirect webroot/static/ index.html access to static/ index.html

For example, when we are through the browser to access http: / / 192.168.11.210 webroot static/index html, actual access is web directory static below/index html files, and also removes webroot this directory, use alias


location ^~ /webroot/ {
 alias /data/www/web/WebContent/;
}

Note:

1. When using alias, always add "/" to the 1 after the directory name.

2. alias can be specified by any name.

3. When using regular matching, alias must capture the content to be matched and use it at the specified content.

4. alias can only be located in location blocks. [/ warning]

http://192.168.11.210/webroot/test/static/index.html


location ^~ /webroot/test/ {
 alias /data/www/web/WebContent/;
}

This is also possible, the final access to the file is the same as the above.

2, put the webroot/static/index html access redirect to web test directory under the directory


location ~ ^/webroot/ {
 root /data/www/web/WebContent/test/;
}

http: / / 192.168.11.210 / webroot/static/index html actual access is web directory testwebroot/static/index html
And use root1 to redirect the access directory to a directory, but the access path must be in the repositioned directory

Notice the difference between alias and alias

Reprint 1:

Access to the domain name

www. adc. com/image automatically jump to www adc. com/make/image

How do I write this

This requirement can be met in several ways:

1. Internal jump realization using Nginx rewrite:


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

2. Use alias mapping


location /image {
    alias /make/image; # I'll write the absolute path here 
}

3. root mapping:


location /image {
   root  /make;
}

4. Absolute jump implementation using nginx's permanent 301


location /image {
    rewrite ^/image/(.*)$  http://www.adc.com/make/image/$1;
}

5. Judge the uri implementation


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

Related articles: