nginx pseudo static Rewrite regular resource aggregation

  • 2020-05-10 23:31:05
  • OfStack

One server of this site was originally windows system, and ISAPI_Rewrite was used to rewrite Url, among which one rule is

RewriteRule ^/(.{6})(\d{3})(.+)/php/ http://www.xxx.com/qq$2.apk [NC,L,NU]

In the middle, {6} is used to refer to the previous character to be duplicated 6 times, and then the linux system is moved flat. Rewrite of nginx is used to rewrite url and the error is reported when nginx is loaded


rewrite ^/(.{6})(\d{3})(.+)/php/ http://www.xxx.com/qq$2.apk break;

After a long search for resources, a solution was found in ken  's article

Wrap the first half of the rule in English with double quotation marks for normal application

Such as: rewrite "^ / (. {6}) (\ d {3}) (+)/php/" http: / / www xxx. com/qq $2. apk break;

You can parse it normally

By the way, the nginx support regular resources comprehensive arrangement 1, convenient for the next use;

nginx rewrite pseudo-static configuration parameters and examples with regular instructions

Regular expression matching:

~ matches for case sensitivity
~* is case insensitive
 ! ~ and! ~* are case sensitive mismatches and case insensitive mismatches, respectively

File and directory matching, where:

- f and! -f is used to determine if a file exists
- d and! -d is used to determine if a directory exists
- e and! -e is used to determine whether a file or directory exists
- x and! -x is used to determine whether the file is executable
flag is marked with:

last is equivalent to the [L] mark in Apache, indicating completion of rewrite
break terminates the match and no longer matches the subsequent rule
The redirect return 302 temporary redirect address bar displays the address after the jump
permanent return 301 permanent redirect address bar will show the address after the jump

$args this variable is equal to the parameter in the request line
$content_length is equal to the value of "Content_Length" in the request line.
$content_type is equivalent to the value "Content_Type" in the request header
$document_root is equivalent to the value specified in the currently requested root directive
$document_uri and $uri1 sample
$host looks like the value specified in the "Host" line in the request header or the name of server arrived by request (no Host line)
$limit_rate allows a limited connection rate
$request_method = method of request, usually "GET" or "POST"
$remote_addr client ip
$remote_port client port
$remote_user is equivalent to the username and is authenticated by ngx_http_auth_basic_module
$request_filename the pathname of the file currently requested is a combination of root or alias and URI request
$request_body_file
$request_uri contains the full initial URI parameter
$query_string and $args1 sample
$server_protocol equivalent to the request protocol, using "HTTP/1.0" or "HTTP/1.1"
$server_addr request arrives at server ip, 1 to get the value of this variable for the purpose of making a system call. To avoid system calls, it is necessary to specify ip in the listen directive and to use the bind parameter.
$server_name the name of the server to which the request arrived
$server_port the port number of the server to which the request arrived
$uri is the same as URI in the current request, but not the same as the initial value, such as when redirecting internally or using index

Combine the example of PHP


if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;

Multiple directories are turned into parameters

abc.domian.com/sort/2 = > abc.domian.com/index.php?act=sort&name=abc&id=2


if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}

Directory exchange

/123456/xxxx - > /xxxx?id=123456


rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;

For example, set nginx to redirect to the/nginx-ie directory if the user USES ie:


if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}

Directory automatically add "/"


if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}

Ban htaccess


location ~/\.ht {
deny all;
}

Disable multiple directories


location ~ ^/(cron|templates)/ {
deny all;
break;
}

Files starting with /data are prohibited

You can disable /data/ requests under multiple levels. log.txt, etc.


location ~ ^/data {
deny all;
}

Prohibit a single directory

Cannot forbid.log.txt can request


location /searchword/cron/ {
deny all;
}

Disable a single file


if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;
0

Set expiration times for favicon.ico and robots.txt;

This is favicon.ico for 99 days and robots.txt for 7 days without logging 404 error logs


if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;
1

Set the expiration time of a file; This is 600 seconds and no access logs are logged


location ^~ /html/scripts/loadhead_1.js {
access_log off;
root /opt/lampp/htdocs/web;
expires 600;
break;
}

The file is anti-hotlinking and sets the expiration time

Here, return 412 is a custom http status code with a default of 403 to help you find the correct hotlinking request


" rewrite ^/ http://leech.c1gstudio.com/leech.gif; "To show 1 An anti-hotlinking picture
" access_log off; "Do not log access to reduce stress
" expires 3d "All documents 3 Days of browser caching location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
if ($invalid_referer) {
rewrite ^/ http://leech.c1gstudio.com/leech.gif;
return 412;
break;
}
access_log off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}

Only allow fixed ip access to the site with a password


if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;
4

To enhance the seo effect, convert files in multiple directories into one file

/ job - 123-456-789. html point/job / 123/456/789. html


if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;
5

Point a folder in the root directory to a level 2 directory

Such as /shanghaijob/ pointing /area/shanghai/
If you change last to permanent, the browser's address bar will appear as /location/shanghai/


if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;
6

One problem with the above example is that it will not match when accessing /shanghai


if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;
7

So /shanghai can also be accessed, but the relative links in the page cannot be used,
For example, the real address of./ list_1.html is /area /shanghia/ list_1.html will become/list_1.html, which cannot be accessed.

Then I can't add automatic jump
(-d $request_filename) it has a condition that it must be a true directory, and my rewrite is not, so it has no effect


if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}

It's easy to do when you know why. Let me jump manually


if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;
9

Redirection when files and directories do not exist:


if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
0

The domain name to jump


server
{
listen 80;
server_name jump.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/ http://www.c1gstudio.com/;
access_log off;
}

Multiple domain redirection


server_name http://www.c1gstudio.com/ http://www.c1gstudio.net/;
index index.html index.htm index.php;
root /opt/lampp/htdocs;
if ($host ~ "c1gstudio\.net") {
rewrite ^(.*) http://www.c1gstudio.com$1/ permanent;
}

Level 3 domain name jump


if ($http_host ~* "^(.*)\.i\.c1gstudio\.com$") {
rewrite ^(.*) http://top.yingjiesheng.com$1/;
break;
}

Domain name mirror


server
{
listen 80;
server_name mirror.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
access_log off;
}

Some subdirectory mirrors to


location ^~ /zhaopinhui {
rewrite ^.+ http://zph.c1gstudio.com/ last;
break;
}

discuz ucenter home (uchome) rewrite


rewrite ^/(space|network)-(.+)\.html$ /$1.php?rewrite=$2 last;
rewrite ^/(space|network)\.html$ /$1.php last;
rewrite ^/([0-9]+)$ /space.php?uid=$1 last;

discuz 7 rewrite


if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
7

Give discuz a separate domain name


if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
8

discuz ucenter avatar rewrite optimization


location ^~ /ucenter {
location ~ .*\.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
} location /ucenter/data/avatar {
log_not_found off;
access_log off;
location ~ /(.*)_big\.jpg$ {
error_page 404 /ucenter/images/noavatar_big.gif;
}
location ~ /(.*)_middle\.jpg$ {
error_page 404 /ucenter/images/noavatar_middle.gif;
}
location ~ /(.*)_small\.jpg$ {
error_page 404 /ucenter/images/noavatar_small.gif;
}
expires 300;
break;
}
}

jspace rewrite


rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;
0

Nginx regular description

^~         identifier is followed by a string. Nginx will stop after the string matching of regular expressions matching (location instruction in regular expression matching results using priority), such as: location ^ ~ / images /, you want to 1 / images/this directory something special operations, such as increasing expires head, hotlinking prevention, etc., but also you want to in addition to this directory of images of all pictures only to increase the operating expires head, this operation might need another 1 location, such as: location ~* \.(gif|jpg|jpeg)$, so if there is a request /images/ 1.jpg, nginx how does nginx decide which operation to perform in location? The result depends on the identifier ^~, if you write location /images/, then nginx will match 1.jpg to location ~* \.(gif|jpg|jpeg)$in location, which is not the result you need, and when you add the ^~ identifier, it stops searching for other location with the regular after it matches the string /images/.
=         =       = location           = / it will only match the request for uri /, if the request is/index.html, it will look for another location and will not match this one. Of course, you can write two location, location = / and location /, so/index.html will match to the latter, If your site has a high volume of requests, you can use this method to speed up the response to requests.

          means to name one location, that is, to customize one location. This location cannot be accessed by the outside world, and can only be used for subrequests generated by Nginx, mainly error_page and try_files.

~          
~*         case-insensitive matching (match firefox's regular and match FireFox).
! ~         does not match
! ~*       does not match

.        matches any character other than a newline
\w         match letters or Numbers or underline or Chinese characters
\s         matches any blank character
\d         matching Numbers
\b         match the beginning or end of a word
^         matches the beginning of the string
$        matches the end of the string

*         repeat zero or more times
+         repeat once or more
The & # 63;         repeat zero times or once
{n}         repeat n times
{n,}         repeat n or more
{n,m}         repeat n to m times
* & # 63; Repeat         any time, but with as little repetition as possible
+ & # 63;         repeat once or more, but with as little repetition as possible
The & # 63; The & # 63;         repeat 0 or 1 times, but with as little repetition as possible
{n, m} & # 63;         repeat n to m as few times as possible
{n,} the & # 63;         repeat n more times, but with as little repetition as possible

\W         matches any character that is not a letter, number, underscore or Chinese character
\S         matches any character that is not a blank character
\D         matches any non-numeric character
\B         matches not the beginning or end of a word
[^x]         matches any character other than x
[^aeiou]         matches any character other than the letters aeiou

Capture         (exp)         match exp and capture the text into the automatically named group
(? < name > exp)         matches exp and captures the text into a group named name, which can also be written as (? 'name exp)
(& # 63; :exp)         matches exp, does not capture the matched text, and does not assign a group number to this group
Zero-width assertion         (? =exp)         match the position in front of exp
(? < =exp)         matches the position after exp
(& # 63; ! exp)         matches the position not followed by exp
(? < ! exp)         matches the previous position that is not exp
Note         (? This type of grouping has no effect on the processing of regular expressions and is used to provide comments for people to read


Related articles: