How to redirect URL using nginx rewrite

How to redirect URL using nginx rewrite

I often need to change nginx configuration at work recently, and learned how to use rewrite in nginx

URL redirection

The URL redirection mentioned here means that when a user visits a URL, it is redirected to another URL.

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

Redirect static file requests to CDN, etc.

Jump to different sites (PC version, WAP version), etc. according to the user's device.

URL redirection can be achieved by setting window.location on the page through js

It can also be achieved by setting the header in PHP

Of course, you can also use nginx's rewrite function to achieve

nginx rewrite module

rewrite is the static rewrite module of nginx

The basic usage is rewrite pattern replace flag

patten is a regular expression. URLs matching patten will be rewritten as replace. flag is optional.

For example, redirect 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 redirecting to a new domain

server
{
 listen 80;
 server_name www.old.com;
 rewrite "^/(.*)$" http://www.new.com/$1;
}
Rewrite and location work together to jump image files to cdn
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
 expires 30d;
 rewrite "^/uploadfile\/(.*)$" http://static.XXX.com/uploadfile/$1;
}

You can add a flag after rewrite . The flag tags are:

last is equivalent to the [L] mark in Apache, indicating that rewrite is completed

break terminates the match and no longer matches the following rules

redirect returns 302 temporary redirection. The address bar will display the redirected address.

Permanent return 301 permanent redirection address bar will display the address after the jump

The above method of using nginx rewrite to achieve URL redirection is all I have to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Nginx's rewrite module
  • Nginx URL rewriting mechanism principle and usage examples
  • Nginx rewrite regular matching rewriting method example
  • Detailed explanation of location and rewrite usage in nginx
  • Detailed explanation of nginx rewrite and location according to URL parameters
  • Nginx rewrite rewrite basics and examples sharing
  • Nginx Rewrite rules and usage introduction and skills examples
  • nginx rewrite pseudo-static configuration parameters and usage examples
  • Analysis of nginx rewrite function usage scenarios

<<:  MySQL 5.7.23 installation and configuration graphic tutorial

>>:  JavaScript implements class lottery applet

Recommend

Solve the problem of no my.cnf file in /etc when installing mysql on Linux

Today I wanted to change the mysql port, but I fo...

How to install and persist the postgresql database in docker

Skip the Docker installation steps 1. Pull the po...

A brief discussion on the CSS overflow mechanism

Why do you need to learn CSS overflow mechanism i...

WeChat Mini Program Lottery Number Generator

This article shares the specific code of the WeCh...

...

JS practical object-oriented snake game example

Table of contents think 1. Greedy Snake Effect Pi...

Example code for implementing triangles and arrows through CSS borders

1. CSS Box Model The box includes: margin, border...

How to build SFTP server and image server on Linux cloud server

First of all, you can understand the difference b...

JavaScript to achieve dynamic table effect

This article shares the specific code for JavaScr...

How to install and deploy zabbix 5.0 for nginx

Table of contents Experimental environment Instal...

Detailed explanation of script debugging mechanism in bash

Run the script in debug mode You can run the enti...

Vue.js style layout Flutter business development common skills

Correspondence between flutter and css in shadow ...

vue perfectly realizes el-table column width adaptation

Table of contents background Technical Solution S...

A brief discussion on the use and analysis of nofollow tags

Controversy over nofollow There was a dispute bet...

How to implement the webpage anti-copying function (with cracking method)

By right-clicking the source file, the following c...