Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What do rewrite and alias refer to in nginx redirect URI

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about what rewrite and alias in nginx redirect URI refer to. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

Students who are familiar with Nginx all know that Nginx can be used for load balancing and reverse proxy, which is very easy to use. It is also very convenient to separate the front and rear. Today we will talk about how to do URI redirection when using Nginx for front-end separation.

What is URI redirection?

That is, when accessing a certain or a regular URI, it is up to the configuration of the Nginx to decide to redirect to another URI or type of URI.

What's the use?

One of the functions is to map the request to a real directory.

At this point we can configure location as follows:

Location / ITwalking {root DVOGUGUP resourcesAccord;}

When configured with root, the ITwalking after location is used as the system directory.

That is, there should be a / ITwalking/page directory under D:/resources/.

There is no problem with this configuration.

But if our D:/resources/ is directly under page,

Then the file is directly below, and there is no file in the middle.

There is a directory of / ITwalking, what should I do? You might think, just create a new ITwalking and move the page and its files into it, and then use the above configuration.

Yes, but not necessary!

It is very unnecessary, because our resources/page directory may be clone from git, if we move in the new directory, have we modified the local git repository? what should we do when we submit the code or update? And move it out? What a hassle. (because we request that the backend must add / ITwalking)

To solve this problem, we can use Nginx's alias instruction or rewrite instruction.

1. The alias instruction is used to reset the directory of the current file, followed by a virtual directory after lacation.

Location / ITwalking/ {# use the nginx alias directive to alias DVR use resources to pageUnix;}

This way, when URL is

When http://www.abc.com/ITwalking/page/index.html

Will redirect this request to

D:/resources/page/index.html

At this time, ITwalking is a virtual directory, and access to this directory is reset to

D:/resources/page/

2. Rewrite redirection, which needs to be used with the root instruction.

Location / ITwalking/ {root DVOGRAPHER resources break;; # use the nginx rewrite directive rewrite ^ / ITwalking/ (. *?) $/ $1 page}

The two effects are the same. ^ / ITwalking/ (. *?) $is regular, which means that if this regular request is satisfied, it will be redirected to the address under D:/resources/page/, / $1 redirected, and break terminates the match.

Rewrite is for http requests. The path in it is the address in the URL, and rewrite does the translation between addresses.

Root and alias are the settings for the system file path.

The difference is that root is used to set the root directory, while alias is used to reset the directory of the current file.

The above is what rewrite and alias in nginx redirect URI refer to. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 278

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report