Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to redirect an address using nginx

Shulou Source: shulou.com Published: 2022-06-01 20:56:01 10月05日 Update

In this issue, the editor will bring you about how to redirect addresses using nginx. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

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

For example, when we access http://192.168.11.210/webroot/static/index.html through a browser, we actually access the static/index.html file under the web directory, and delete the webroot directory and use alias

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

Note:

1. When using alias, be sure to add "/" to the directory name.

2. Alias can specify any name.

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

4. Alias can only be in a location block. [/ 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 above.

2. Redirect access to webroot/static/index.html to the test directory under the web directory

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

Http://192.168.11.210/webroot/static/index.html actually accesses testwebroot/static/index.html under the web directory.

And using root generally redirects the access directory to a directory, but the access path must be in the relocated directory

Pay attention to the difference between distinction and alias

Reprint one:

Access domain name

Www.adc.com/image automatically jumps to www.adc.com/make/image

How to write this?

There are several ways to achieve this requirement:

1. Using Nginx rewrite internal jump to achieve:

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

two。 Using alias mapping

Location / image {alias / make/image; # write the absolute path here}

3. Take advantage of root mapping:

Location / image {root / make;}

4. Using permanent 301absolute Jump implementation of nginx

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;} the above is how to redirect addresses using nginx. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Directory content address actual file path faceted analysis professional small and medium rich in content name domain name variety namely articles methods more rules browsers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft MariaDB MySQL Shulou Information