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:
The label segment position of the rewrite parameter: server,location,if example: rewrite ^/(.*) http://www.czlun.com/$1 permanent; illustrate:
regex Common regular expression description
rewrite Enterprise Application Scenarios Nginx's rewrite function is widely used in enterprises:
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:
|
<<: Summary of MySql import and export methods using mysqldump
>>: Common array operations in JavaScript
Table of contents Current Issues Solution process...
Use meta to implement timed refresh or jump of th...
1. This afternoon, due to the requirements of the...
This article example shares the specific code of ...
1. Add an empty element of the same type, and the...
We all know that Apache can easily set rewrites f...
I spent a day on it before this. Although Seata i...
The following situations were discovered during d...
1. Download 1. Download the installation package ...
As we all know, there are two types of images in c...
Effect If you use it, please optimize the code an...
A Textbox with Dropdown allows users to select an...
Today, when developing, I encountered a method wh...
The effect to be achieved is: fixed zoom in twice...
XML/HTML CodeCopy content to clipboard < butto...