nginx rewrite pseudo static configuration parameter details

  • 2020-05-06 12:11:52
  • OfStack

regular expression match, where:

* ~ match for case sensitivity * ~* match for case insensitivity *! ~ and! ~* are case sensitive mismatches and case insensitive mismatches , respectively

files and directories match, where:

* - f and! -f to determine the existence of files * -d and! -d to determine if a directory exists * -e and! -e to determine if a file or directory exists * -x and! -x is used to determine whether a file is executable or not

flag is marked

* last is equivalent to the [L] tag in Apache, which means that rewrite* break terminates the match and no longer matches the following rule * redirect returns 302 temporary redirect address bar will show the address after the jump * permanent returns 301 permanent redirect address bar will show the address after the jump

Some of the available global variables are , which can be used as a conditional judgment (to be completed)

$args $content_length $content_type $document_root $document_uri $host $http_user_agent $http_cookie $limit_rate $request_body_file $request_method $remote_addr $remote_port $remote_user $request_filename $request_uri $query_string $scheme $server_protocol $server_addr $server_name $server_port $uri

combines QeePHP examples

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;

multi-directory is converted to parameter
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 swap
/123456/xxxx - > /xxxx?id=123456

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

for example, the following setting nginx redirects the user using ie to/nginx-ie:

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

directory automatically adds "/"

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

prohibits htaccess

location ~/\.ht { deny all; }

prohibits multiple directories

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

disallows files
that begin with /data /data/ can be disabled under the multilevel directory. Es156en.txt request;

location ~ ^/data { deny all; }

prohibits a single directory
log.txt can request

location /searchword/cron/ { deny all; }

prohibits a single file

location ~ /data/sql/data.sql { deny all; }

sets expiration times for favicon.ico and robots.txt;
Es193en.ico for 99 days, robots.txt for 7 days, no 404 error log

location ~(favicon.ico) { log_not_found off; expires 99d; break; } location ~(robots.txt) { log_not_found off; expires 7d; break; }

sets the expiration time of a file; Here it is 600 seconds and no access log

is recorded 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, default is 403, to find the correct hotlinking request
"rewrite ^ / http: / / leech c1gstudio. com/leech gif;" Displays an anti-hotlinking image
"access_log off;" No access logs to reduce stress
"expires 3d" browser cache of all files for 3 days

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 allows fixed ip access to the site with the password

root /opt/htdocs/www; allow 208.97.167.194; allow 222.33.1.2; allow 231.152.49.4; deny all; auth_basic "C1G_ADMIN"; auth_basic_user_file htpasswd;

transforms files in multiple directories into one file, enhancing the seo effect
/ job - 123-456-789. html point/job / 123/456/789. html

rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;

points a folder in the root directory to the level 2 directory
For example, / shanghai job/ point to /area/ shanghai /
If you change last to permanent, the browser address bar will be /location/shanghai/

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

One problem with the above example is that

will not match when accessing /shanghai rewrite ^/([0-9a-z]+)job$ /area/$1/ last; rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

So /shanghai is also accessible, but the relative links in the page are not available,
For example./ list_1.html the real address is /area/shanghia/ list_1.html will become/list_1.html, leading to no access.

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

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

It's easy to do after you know the reason. Let me jump

manually rewrite ^/([0-9a-z]+)job$ /$1job/ permanent; rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

Redirection when files and directories do not exist:

if (!-e $request_filename) { proxy_pass http://127.0.0.1; }

domain name 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; }

multi-domain goes to

server_name www.c1gstudio.com 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 mirrors

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; }

An 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

rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last; rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last; rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3 last; rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last; rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last; rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;

gives discuz a separate domain name

server_name bbs.c1gstudio.com news.c1gstudio.com; location = / { if ($http_host ~ news\.c1gstudio.com$) { rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last; break; } }

discuz ucenter heads rewrite optimized

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

location ~ .*\.php?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php;include fcgi.conf; } location ~* ^/index.php/ { rewrite ^/index.php/(.*) /index.php?$1 break; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; }

Related articles: