1: Understand the meaning of address rewriting and address forwarding. Address rewriting and address forwarding are two different concepts. Address rewriting is to achieve address standardization. For example, we can enter www.baidu.com in the address bar. We can also enter www.baidu.cn. In the end, it will be rewritten to www.baidu.com. The browser's address bar will also display www.baidu.com. Address forwarding: It refers to the process in which after a data packet reaches a router or bridge during network data transmission, the device checks the packet address and forwards the data to the nearest local area network. Therefore, address rewriting and address forwarding have the following differences: 1. Address rewriting will change the address in the browser and rewrite it to the latest address of the browser. Address forwarding will not change the browser's address. 2: Understanding the use of Rewrite instructions This directive is used to change the URI through the use of regular expressions. One or more directives can exist at the same time. The URLs need to be matched and processed in sequence. This directive can be configured in the server block or location block. Its basic syntax structure is as follows: rewrite regex replacement [flag]; The meaning of rewrite: This instruction is used to implement URL rewriting. The flag has the following values:
For example, the following examples: rewrite ^/(.*) http://www.baidu.com/$1 permanent; illustrate: Let's do a simple demo to simulate this: 1. There is an app.js in our test project. The code is as follows: const Koa = require('koa'); const app = new Koa(); const router = require('koa-router')(); // Add route router.get('/', ctx => { ctx.body = '<h1>Welcome to the index page</h1>'; }); router.get('/home', ctx => { ctx.body = '<h1>Welcome to the home page</h1>'; }); router.get('/404', ctx => { ctx.body = '<h1>404...</h1>' }); // Load routing middleware app.use(router.routes()); app.listen(3001, () => { console.log('server is running at http://localhost:3001'); }); Then run node app.js in the command line, and then we can visit http://localhost:3001 in the browser to access our corresponding page. But now I want to deploy the node project to my local nginx server. Please read my article for nginx installation. Then I want to use the domain name to access our project, so we need to configure it in our nginx.conf: cd /usr/local/etc/nginx Then use the command: sudo open /usr/local/etc/nginx/nginx.conf -a 'sublime text' command to open nginx.conf and configure it as follows: worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8081; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 8088; server_name xxx.abc.com; location / { proxy_pass http://127.0.0.1:3001; rewrite ^/(.*) http://www.baidu.com permanent; } } } As shown in the above code, my listening port number is 8088, and the server_name configuration is set to xxx.abc.com. Then when we visit http://xxx.abc.com:8088/, it will first be reverse-proxyed to the page corresponding to the node under our http://127.0.0.1:3001. After the reverse proxy is completed, rewrite will be used to redirect to the Baidu page. After the above configuration is completed, we need to restart the nginx server; use the command: Then when we visit http://xxx.abc.com:8088/ in the browser, it will execute as shown in the following figure. It will first permanently redirect http://xxx.abc.com:8088/ (301), then visit Baidu (307), temporarily redirect to the Baidu page, and finally load the address of the Baidu page; as shown in the following demonstration: But if I change permanent to redirect, for example, after nginx configuration: rewrite ^/(.*) http://www.baidu.com redirect;, it will become a 302 temporary redirect. As shown below: Three: Understanding the if instruction This directive is used to support conditional judgment and select different nginx configurations based on the results of conditional judgment. We can configure this directive in the server block or location block. Its grammatical structure is: if (condition) { // .... } condition is a boolean value with true/false meaning. The global variables available to the Rewrite directive are as follows: 1. $args: This variable stores the request instructions in the request URL. For example, in http://127.0.0.1:3001?arg1=value1&arg2=value2 The basic syntax of a regular expression is: 1. Match variables '~' means the matching process is case sensitive. For example: if ($http_user_agent ~ MSIE) { // Meaning of the code: whether the $http_user_agent value contains the MSIE string, if it does, it is true, otherwise it is false } 2. Determine whether the requested file exists '-f' This condition is true if the requested file exists. if (-f $request_filename) { // Check if the requested file exists} if (!-f $request_filename) { // Check if the requested file does not exist} 3. Check if the requested directory exists using '-d' and '!-d' With '-d', returns true if the requested directory exists. Otherwise returns false. Now we use the if directive to add some judgment to nginx; for example, when we visit http://xxx.abc.com:8080/home, if $host = 'xxx.abc.com', we will redirect. The nginx configuration code is as follows: server { listen 8088; server_name xxx.abc.com; location / { proxy_pass http://127.0.0.1:3001; if ($host = 'xxx.abc.com') { rewrite ^/(.*) http://www.cnblogs.com redirect; } } } With nginx configured as above, if we visit http://xxx.abc.com:8088, it will redirect to http://www.cnblogs.com. For example, more judgments, such as if the user agent is accessed by a mobile phone, jump directly to a certain page, or use if judgment. For example: if ( $http_user_agent ~* "(Android)|(iPhone)|(Mobile)|(WAP)|(UCWEB)" ){ rewrite ^/$ http://www.cnblogs.com permanent; } 4: Understanding anti-hotlinking and nginx configuration What is Anti-Hotlinking? Hotlinking can be understood as stealing image links, that is, stealing other people's pictures and using them on your own server. Then anti-hotlinking can be understood as preventing others from stealing my pictures. The implementation principle of anti-hotlinking: When a client requests resources from a server, in order to reduce network bandwidth and improve response time, the server generally does not transmit all resources back to the client in full at one time. For example, when requesting a web page, the text content of the web page will be returned first. When the client browser finds that there is an image in the process of parsing the text, it will initiate a request for the image resource to the server again, and the server will send the stored image resource to the client. But what if this image is linked to the server of another site? For example, in my project, I referenced an image from Taobao. Then when our website is reloaded, it will request the Taobao server, which is likely to cause a burden on the Taobao server. So this is hotlinking. Therefore, we need to implement anti-hotlinking. Implement anti-hotlinking: Use the Referer header field in the request header of the http protocol to determine the source address of the currently accessed web page or file. Through the value of this header field, we can detect the source address of accessing the target resource. If the target source address is not the URL of our own site, then in this case, we take blocking measures to prevent hotlinking. But please note that the value in the Referer header field can be changed. Therefore, this method cannot completely prevent hotlinking. Use the Rewrite function of the Nginx server to implement anti-hotlinking. There is a directive valid_referers in Nginx. This directive can be used to obtain the value in the Referer header field and assign a value to the Nginx global variable $invalid_referer based on the value. If the Referer header field does not contain a value that matches the valid_referers directive, the $invalid_referer variable will be assigned a value of 1. The basic syntax of the valid_referers directive is as follows: valid_referers none | blocked | server_names | string none: Detects the absence of the Referer header field. server_names: Set one or more URLs and check whether the value of the Referer header field is one of the URLs. Therefore, if we have the valid_referers directive and the $invalid_referer variable, we can implement anti-hotlinking through the Rewrite function. 1. The following table lists the anti-hotlink configuration based on the requested file type: server { listen 8080; server_name xxx.abc.com location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ { valid_referers none blocked www.xxx.com www.yyy.com *.baidu.com *.tabobao.com; if ($invalid_referer) { rewrite ^/ http://www.xxx.com/images/forbidden.png; } } } With the above basic configuration, when a network connection is used to request image resources with suffixes of gif, jpg, or png, media resources with suffixes of swf or flv, or compressed resources with suffixes of rar or zip, if it is detected that the Referer header field does not comply with the valid_referers directive, it means that the request is not for resources from this site. location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ This configuration means setting the file type for anti-hotlinking. valid_referers none blocked www.xxx.com www.yyy.com *.baidu.com *.tabobao.com; It can be understood as a whitelist, a whitelist of domain names that allow file links. If the requested resource file does not start with these domain names, it means that the requested resource file is not a request under this domain, so it can be judged as a hotlink. Therefore, if the request is not under this domain, Rewrite will be used to redirect to the image http://www.xxx.com/images/forbidden.jpg. For example, if this image is an x or other logo, then other websites will not be able to access your image. 2. The configuration for implementing anti-hotlinking based on the request directory is as follows: server { listen 8080; server_name xxx.abc.com location /file/ { root /server/file/; valid_referers none blocked www.xxx.com www.yyy.com *.baidu.com *.tabobao.com; if ($invalid_referer) { rewrite ^/ http://www.xxx.com/images/forbidden.png; } } } This concludes this article on the detailed explanation of the redirection configuration and practice of Rewrite in Nginx. For more relevant Nginx Rewrite redirection content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: vue+springboot realizes login function
>>: mysql command line script execution example
Preface Review and summary of mobile terminal rem...
Table of contents Features Advantages Installatio...
Table of contents 1. Self-enumerable properties 2...
Table of contents 1. Dockerfile 2. pom configurat...
When doing DIV+CSS layout of the page, it is very...
background In the early stages of learning Japane...
Table of contents Code Optimization Using key in ...
If you have installed the Win10 system and want t...
Table of contents 1. Introduction to UDP and Linu...
I have recently been developing a visual operatio...
mysqladmin is an official mysql client program th...
Table of contents Nesting Parent-child component ...
Table of contents 1. Job Execution Fault Toleranc...
The custom encapsulation code of the vue button c...
This article example shares the specific code for...