Nginx URL rewriting mechanism principle and usage examples

Nginx URL rewriting mechanism principle and usage examples

URL rewriting helps determine the preferred domain of the website. 301 redirects for multiple paths of the same resource page help to concentrate the URL weight.

Introduction to Nginx URL Rewriting

Like Apache and other web service software, the main function of rewrite is to redirect the URL address. Nginx's rewrite function requires the support of PCRE software, that is, rule matching is performed through perl-compatible regular expression statements. Compiling nginx with default parameters will support the rewrite module, but PCRE support is also required

rewrite is the key instruction to implement URL rewriting. It redirects to replacement according to the regex (regular expression) part, and ends with a flag tag.

The rewrite syntax format and parameter syntax are as follows:

rewrite <regex> <replacement> [flag];

Keyword regular replacement content flag tag

Keywords: The keyword error_log cannot be changed

Regular: Perl compatible regular expression statement for rule matching

Replacement content: Replace the content matched by the regular expression with replacement

Flag tag: flag tag supported by rewrite

Flag description:

  • last #After matching this rule, continue to match the new location URI rule
  • break #This rule will terminate when it is matched and will not match any subsequent rules
  • redirect #Return 302 temporary redirection, the browser address will display the URL address after the jump
  • permanent #Return 301 permanent redirection, the browser address bar will display the URL address after the jump

The label segment position of the rewrite parameter:

server,location,if

example:

rewrite ^/(.*) http://www.czlun.com/$1 permanent;

illustrate:

  • rewrite is a fixed keyword, indicating the start of rewrite matching rules
  • The regex part is ^/(.*), which is a regular expression that matches the complete domain name and the path address that follows it.
  • The replacement part is http://www.czlun.com/$1 $1, which is taken from the content in the regex part (). The URL to jump to after a successful match.
  • The flag part permanent indicates a permanent 301 redirection flag, which means jumping to the new http://www.czlun.com/$1 address

regex Common regular expression description

character

describe

\

Marks the following character as a special character or a literal character or a backreference. For example, "\n" matches a newline character, and "\$" matches "$".

^

Matches the start of the input string

$

Matches the end of the input string

*

Matches the preceding character zero or more times. For example, "ol*" can match "o" and "ol", "oll"

+

Matches the preceding character one or more times. For example, "ol+" can match "ol", "oll", and "oll", but cannot match "o"

?

Matches the preceding character zero or one time. For example, "do(es)?" can match "do" or "does". "?" is equivalent to "{0,1}"

.

Matches any single character except "\n". To match any character including "\n", use a pattern such as "[.\n]".

(pattern)

Matches the pattern in the brackets and can get the corresponding match later. The $0...$9 attributes are often used to get the matching content in the brackets. To match the parentheses, you need \(Content\)

rewrite Enterprise Application Scenarios

Nginx's rewrite function is widely used in enterprises:

  • u You can adjust the URL that users browse to make it look more standardized and meet the needs of developers and product personnel.
  • u In order to allow search engines to index website content and provide better user experience, companies will disguise dynamic URL addresses as static addresses to provide services.
  • u After the URL is changed to a new domain name, the old visits will be redirected to the new domain name. For example, visiting JD.com's 360buy.com will redirect you to jd.com
  • u Adjust URLs based on special variables, directories, client information, etc.

Introduction to Nginx rewrite configuration process

(1) Create a rewrite statement

vi conf/vhost/www.abc.com.conf

#vi edit the virtual host configuration file

File Contents

server {
    listen 80;
    server_name abc.com;
    rewrite ^/(.*) http://www.abc.com/$1 permanent;
}
server {
    listen 80;
    server_name www.abc.com;
    location / {
        root /data/www/www;
        index index.html index.htm;
    }
    error_log logs/error_www.abc.com.log error;
    access_log logs/access_www.abc.com.log main;
}

or

server {
    listen 80;
    server_name abc.com www.abc.com;
    if ( $host != 'www.abc.com' ) {
        rewrite ^/(.*) http://www.abc.com/$1 permanent;
    }
    location / {
        root /data/www/www;
        index index.html index.htm;
    }
    error_log logs/error_www.abc.com.log error;
    access_log logs/access_www.abc.com.log main;
}

(2) Restart the service

After confirmation, you can restart. The operation is as follows:

nginx -t

#If the result shows ok and success, you can restart

nginx -s reload

(3) Check the jump effect

Open your browser and visit abc.com

After the page is opened, abc.com in the URL address bar becomes www.abc.com, indicating that the URL is rewritten successfully.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Nginx's rewrite module
  • Nginx rewrite regular matching rewriting method example
  • Detailed explanation of location and rewrite usage in nginx
  • How to redirect URL using nginx rewrite
  • 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

<<:  Summary of MySql import and export methods using mysqldump

>>:  Common array operations in JavaScript

Recommend

Implementing timed page refresh or redirect based on meta

Use meta to implement timed refresh or jump of th...

Vue implements picture verification code when logging in

This article example shares the specific code of ...

Several methods to clear floating (recommended)

1. Add an empty element of the same type, and the...

IIS 7.5 uses URL Rewrite module to achieve web page redirection

We all know that Apache can easily set rewrites f...

Docker+nacos+seata1.3.0 installation and usage configuration tutorial

I spent a day on it before this. Although Seata i...

Table setting background image cannot be 100% displayed solution

The following situations were discovered during d...

How to add vector icons to web font files in web page production

As we all know, there are two types of images in c...

Vue.js Textbox with Dropdown component

A Textbox with Dropdown allows users to select an...

Sharing several methods to disable page caching

Today, when developing, I encountered a method wh...

Vue3.0 handwriting magnifying glass effect

The effect to be achieved is: fixed zoom in twice...