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

The method of realizing URL Jump by nginx rewrite

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Recently, I often have to change nginx configuration in my work, and I have learned the usage of rewrite in nginx.

URL jump

URL jump refers to when a user visits a URL and jumps it to another URL.

A common application scenario is to have multiple domain names jump to the same URL (for example, to have an old domain name jump to a new domain name)

Redirect static file requests to cdn

Jump to different sites (pc version, wap version) according to user equipment, etc.

URL jump can be achieved by js setting_window.location on the page

This can also be done by setting the header in PHP.

Of course, it can also be implemented with nginx rewrite function.

nginx rewrite module

rewrite is a static rewrite module for nginx

Rewrite Patten Replace Flag

patten is a regular expression, URL matching patten will be rewritten as replace, flag optional

For example, jump the old domain name to the new domain name

server{ listen 80; server_name www.old.com; rewrite ".* " http://www.new.com;}

Keep the path when jumping to a new domain name

server{ listen 80; server_name www.old.com; rewrite "^/(.*)$ " http://www.new.com/$1;}rewrite with location to achieve picture file jump to cdnlocation ~ .*\. (gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; rewrite "^/uploadfile\/(.*)$ " http://static.XXX.com/uploadfile/$1;}

Rewrite can be followed by flag, flag marks include:

last is equivalent to the [L] tag in Apache, indicating that the rewrite is complete.

break Terminates matching and no longer matches subsequent rules

redirect Return 302 Temporary redirect The address bar will show the address after the jump

permanent return 301 permanent redirect address bar will show the address after jump

The above nginx rewrite method to achieve URL jump is all the content shared by Xiaobian to everyone, I hope to give you a reference, but also hope that you support more.

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: 0

*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

Servers

Wechat

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

12
Report