In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to use nginx's url rewriting. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Url rewriting of nginx
1.url rewriting module (rewrite)
Summary this module allows you to rewrite uri using regular expressions (pcre libraries are required), and can redirect and select different configurations based on related variables. If this instruction is specified in the server field, it will be executed before the requested location is determined, and if there are other rewriting rules in the selected location after instruction execution, they will also be executed. If executing this instruction in location results in a new uri, then location once again determines the new uri. Such a loop can be executed up to 10 times, after which nginx will return 500 errors.
Break
Syntax: break
Default value: none
Use fields: server, location, if
Complete the currently set rules and stop executing other rewrite instructions.
Example:
If ($slow) {limit_rate 10k; break;}
If
Syntax: if (condition) {... }
Default value: none
Use fields: server, location
Note: check if is evil page before using the if directive and try to consider using try_files instead.
Determine a condition that, if the condition is true, the statement in the following curly braces will be executed and the relevant configuration will be inherited from the parent.
You can specify the following values in the judgment statement:
The name of a variable; invalid values are: empty character pass "" or some string that starts with "0".
A comparison statement that uses the = or! = operator.
Regular expressions that use symbols ~ * and ~ patterns to match:
~ for case-sensitive matching.
~ * case-insensitive matching (firefox matching firefox).
! ~ and! ~ * means "mismatched".
Use-f and!-f to check whether a file exists.
Use-d and!-d to check if a directory exists.
Use-e and!-e to check whether a file, directory, or soft link exists.
Use-x and!-x to check whether a file is executable.
Part of the regular expression can be parenthesized to facilitate subsequent referencing with $1 music 9 in order.
Example configuration:
If ($http_user_agent ~ msie) {rewrite ^ (. *) $/ msie/$1 break;} if ($http_cookie ~ * "id= ([^;] +) (?:; | $)") {set $id $1 } if ($request_method = post) {return 405;} if (!-f $request_filename) {break; proxy_pass http://127.0.0.1; } if ($slow) {limit_rate 10k;} if ($invalid_referer) {return 403 } if ($args ~ post=140) {rewrite ^ http://example.com/ permanent;}
The built-in variable $invalid_referer is specified by the directive valid_referers.
Return
Syntax: return code
Default value: none
Use fields: server, location, if
This instruction ends the execution of the configuration statement and returns the status code for the client. You can use the following values: 204400402-406, 408, 410,411,413,416 and 500,504. In addition, non-standard code 444 closes the connection and does not send any headers.
Rewrite
Syntax: rewrite regex replacement flag
Default value: none
Use fields: server, location, if
Modify the uri according to the relevant regular expressions and strings, and the instructions are executed in the order in which they appear in the configuration file.
You can add a tag after the rewrite instruction.
If the replacement string begins with http://, the request is redirected and redundant rewrite instructions are no longer executed.
The tail tag (flag) can be the following values:
Last-complete the rewrite instruction and then search for the appropriate uri or location.
Break-complete the rewrite instruction.
Redirect-returns 302 temporary redirection, which is used if the replacement field begins with http://.
Permanent-returns 301 permanent redirection.
Note that if a redirect is relative (there is no hostname part), nginx will use the "host" header that matches the server_name instruction or the first name specified by the server_name directive during the redirect. If the header does not match or does not exist, if server_name is not set, the local hostname will be used, if you always want nginx to use the "host" header. You can use the "*" wildcard in server_name (see server_name in the http core module). For example:
Rewrite ^ (/ download/.*) / media/ (. *)\.. * $$1/mp3/$2.mp3 last;rewrite ^ (/ download/.*) / audio/ (. *)\.. * $$1/mp3/$2.ra last;return 403
But if we put it in a location named / download/, we need to change the last tag to break, otherwise nginx will execute 10 loops and return 500 errors.
Location / download/ {rewrite ^ (/ download/.*) / media/ (. *)\.. * $1/mp3/$2.mp3 break; rewrite ^ (/ download/.*) / audio/ (. *)\. * $1/mp3/$2.ra break; return 403;}
If the replacement field contains parameters, the rest of the request parameters are appended, and to prevent them, you can follow the last character with a question mark:
Rewrite ^ / users/ (. *) $/ show?user=$1? Last
Note: curly braces ({and}) can be used in both regular expressions and configuration blocks. To prevent conflicts, regular expressions need double quotes (or single quotes) to use curly braces. For example, to rewrite the following url:
/ photos/123456
Are:
/ path/to/photos/12/1234/123456.png
The following regular expression is used (note the quotation marks):
Rewrite "/ photos/ ([0-9] {2}) ([0-9] {2}) ([0-9] {2})" / path/to/photos/$1/$1 $2 Universe 1 $2 $3.png
If you specify a "?" At the end of the rewrite, nginx discards the parameter in the request, the variable $args, and you can use "?" at the end of rewrite when using $request_uri or $uri&$args. To prevent nginx from processing parameter strings twice.
Use $request_uri in rewrite to rewrite www.example.com to example.com:
Server {server_name www.example.com; rewrite ^ http://example.com$request_uri? Permanent;}
Similarly, rewriting operates only on paths, not parameters, and if you want to rewrite a url with parameters, you can use the following instead:
If ($args ^ ~ post=100) {rewrite ^ http://example.com/new-address.html? Permanent;}
Note that the $args variable is not compiled, unlike uri in the location process (see location in the http core module).
Rewrite_log
Syntax: rewrite_log on | off
Default value: rewrite_log off
Use fields: server, location, if
Variables: none
When enabled, the rewrite log of the notice tag is recorded in error log.
Set
Syntax: set variable value
Default value: none
Use fields: server, location, if
Directive sets a variable and assigns a value to it, which can be text, variables, and a combination of them.
You can use set to define a new variable, but you cannot use set to set the value of the $http_xxx header variable.
Uninitialized_variable_warn
Syntax: uninitialized_variable_warn on | off
Default value: uninitialized_variable_warn on
Use fields: http, server, location, if
Turns warning logging on or off in uninitialized variables.
In fact, the rewrite directive is compiled into internal code when the configuration file is loaded and is used when the interpreter makes a request.
This interpreter is a simple stack virtual machine, such as the following instructions:
Location / download/ {if ($forbidden) {return 403;} if ($slow) {limit_rate 10k;} rewrite ^ / (download/.*) / media/ (. *)\. * $/ $1/mp3/$2.mp3 break
Will be compiled in the following order:
Variable $forbiddenchecking to zerorecovery 403completion of entire codevariable $slowchecking to zerocheckings of regular excodessioncopying "/" copying $1copying "/ mp3/" copying $2copying ".mp3" completion of regular excodessioncompletion of entire sequence
Note that there is no code for limit_rate because it does not mention the ngx_http_rewrite_module module, and the "if" block can be similar to the "location" directive in the same part of the configuration file.
If $slow is true, the corresponding if block will take effect, and in this configuration the value of limit_rate is 10k.
Directive:
Rewrite ^ / (download/.*) / media/ (. *)\.. * $/ $1/mp3/$2.mp3 break
If we enclose the first diagonal bar in parentheses, we can reduce the execution order:
Rewrite ^ (/ download/.*) / media/ (. *)\.. * $1/mp3/$2.mp3 break
The subsequent order is similar to the following:
Checking regular excodessioncopying $1copying "/ mp3/" copying $2copying ".mp3" completion of regular excodessioncompletion of entire code
two。 Simple case
Note, as the configuration file contains a lot of content, in order to make it easier for you to see, let's back up the configuration file and open a new configuration file.
[root@nginx ~] # cd / etc/nginx/ [root@nginx nginx] # mv nginx.conf nginx.conf.proxy [root@nginx nginx] # cp nginx.conf.bak nginx.conf [root@nginx nginx] # vim / etc/nginx/nginx.confserver {listen 80; server_name localhost; # charset koi8-r; # access_log logs/host.access.log main; location / {root html; index index.html index.htm Rewrite ^ / bbs/ (. *) $http://192.168.18.201/forum/$1;}}
Prepare the forum directory and test files
[root@web1 ~] # cd / var/www/html/ [root@web1 html] # lsindex.html [root@web1 html] # mkdir forum [root@web1 html] # cd forum/ [root@web1 forum] # vim index.htmlforum page!
Test it
All right, let's test rewrite rewriting.
3. Reload the configuration file
[root@nginx 63] # service nginx reloadnginx: the configuration file / etc/nginx/nginx.conf syntax is oknginx: configuration file / etc/nginx/nginx.conf test is successful reload nginx: [OK]
4. Test it
Note, as you can see from the figure, status code 302 refers to temporary redirection, which means that our rewrite rewrite configuration is successful. We all know that 302 is a temporary redirect and 301 is a permanent redirect, so how to achieve a permanent redirect? In general, there is a temporary redirection between the server and the server, and a permanent redirection inside the server. Let's demonstrate permanent redirection.
5. Configure permanent redirection
[root@nginx nginx] # vim / etc/nginx/nginx.confserver {listen 80; server_name localhost; # charset koi8-r; # access_log logs/host.access.log main; location / {root html; index index.html index.htm; rewrite ^ / bbs/ (. *) $/ forum/$1;}}
Prepare the forum directory and test files
[root@nginx ~] # cd / usr/html/ [root@nginx html] # ls50x.html index.html [root@nginx html] # mkdir forum [root@nginx html] # cd forum/ [root@nginx forum] # vim index.html192.168.18.208 forum page
6. Reload the configuration file
[root@nginx ~] # service nginx reloadnginx: the configuration file / etc/nginx/nginx.conf syntax is oknginx: configuration file / etc/nginx/nginx.conf test is successful reload nginx: [OK]
7. Test it
Note, as you can see from the figure, our access to bbs/ directly helps us jump to forum/. This native jump is also called implicit redirection, which is permanent redirection.
These are all the contents of the article "how to rewrite url in nginx". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.