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

HTML tag marquee realizes various scrolling effects (without JS control)

The automatic scrolling effect of the page can be...

Recommend a cool flashing alarm button

The effect is as follows: The code is as follows ...

Using zabbix to monitor the ogg process (Windows platform)

This article introduces how to monitor the ogg pr...

Using keras to judge SQL injection attacks (example explanation)

This article uses the deep learning framework ker...

jQuery implements time selector

This article example shares the specific code of ...

Example of integrating Kafka with Nginx

background nginx-kafka-module is a plug-in for ng...

Solution to the timeout problem when installing docker-compose with PIP

1: Installation command pip install docker-compos...

Example of implementing GitHub's third-party authorization method in Vue

Table of contents Creating OAuth Apps Get the cod...

How to uninstall MySQL 5.7 on CentOS7

Check what is installed in mysql rpm -qa | grep -...

How to deploy and start redis in docker

Deploy redis in docker First install Docker in Li...

Why MySQL does not recommend using subqueries and joins

To do a paginated query: 1. For MySQL, it is not ...

A detailed introduction to for/of, for/in in JavaScript

Table of contents In JavaScript , there are sever...

30 Tips for Writing HTML Code

1. Always close HTML tags In the source code of p...

Example of implementing text wrapping in html (mixed text and images in html)

1. Text around the image If we use the normal one...

JavaScript implements displaying a drop-down box when the mouse passes over it

This article shares the specific code of JavaScri...