Something about Nginx configuration files and ReWriteUrl~

WBOY
Release: 2016-07-30 13:29:34
Original
944 people have browsed it

Nginx Rewrite rule related instructions
Nginx Rewrite rule related instructions include if, rewrite, set, return, break, etc., among which rewrite is the most critical instruction. A simple Nginx Rewrite rule syntax is as follows:

rewrite ^/b/(.*).html /play.php?video=$1 break;

If you add an if statement, the example is as follows:

if (!- f $request_filename)

rewrite ^/img/(.*)$ /site/$host/images/$1 last;

Comparison of Rewrite rule examples between Nginx and Apache

There is not much difference between simple Nginx and Apache rewrite rules , basically fully compatible. For example:

Apache Rewrite rule:

RewriteRule ^/(mianshi|xianjing)/$ /zl/index.php?name=$1 [L]

RewriteRule ^/ceshi/$ /zl/ceshi.php [L]

RewriteRule ^/(mianshi)_([a-zA-Z]+)/$ /zl/index.php?name=$1_$2 [L]

RewriteRule ^/pingce([0-9]*) /$ /zl/pingce.php?id=$1 [L]

Nginx Rewrite rules:

rewrite ^/(mianshi|xianjing)/$ /zl/index.php?name=$1 last;

rewrite ^/ ceshi/$ /zl/ceshi.php last;

rewrite ^/(mianshi)_([a-zA-Z]+)/$ /zl/index.php?name=$1_$2 last;

rewrite ^ /pingce([0-9]*)/$ /zl/pingce.php?id=$1 last;

As can be seen from the above example, Apache’s Rewrite rules are changed to Nginx’s Rewrite rules. It is actually very simple: Apache’s The RewriteRule directive is replaced with Nginx's rewrite directive, Apache's [L] tag is replaced with Nginx's last tag, and the content in the middle remains unchanged.

If you change Apache's Rewrite rules to Nginx's Rewrite rules and use the nginx -t command to check and find that the nginx.conf configuration file has syntax errors, you can try adding quotes to the conditions. For example, the following Nginx Rewrite rule will report a syntax error:

rewrite ^/([0-9]{5}).html$ /x.jsp?id=$1 last;

Add quotation marks and it will be correct:
rewrite “^/([0-9]{5}).html$” /x.jsp?id=$1 last;

The Rewrite rules of Apache and Nginx have subtle differences when URL jumps:

Apache Rewrite rules :
RewriteRule ^/html/tagindex/([a-zA-Z]+)/.*$ /$1/ [R=301,L]

Nginx Rewrite rule:
rewrite ^/html/tagindex/([a -zA-Z]+)/.*$ http://$host/$1/ permanent;

In the above example, we noticed that "http://$host" was added to the replacement string of the Nginx Rewrite rule. This is required in Nginx.

In addition, the Rewrite rules of Apache and Nginx are also different in variable names, for example:

Apache Rewrite rule:
RewriteRule ^/user/login/$ /user/login.php?login=1&forward=http://% {HTTP_HOST} [L]

Nginx Rewrite rules:
rewrite ^/user/login/$ /user/login.php?login=1&forward=http://$host last;

Some functions of Apache and Nginx Rewrite rules Correspondence between the same or similar instructions and tags:

Apache’s RewriteCond instruction corresponds to Nginx’s if instruction;
Apache’s RewriteRule instruction corresponds to Nginx’s rewrite instruction;
Apache’s [R] tag corresponds to Nginx’s redirect tag;
Apache’s [ P] mark corresponds to Nginx’s last mark;
Apache’s [R, L] mark corresponds to Nginx’s redirect mark;
Apache’s [P, L] mark corresponds to Nginx’s last mark;
Apache’s [PT, L] mark corresponds to Nginx last tag;

Allow specified domain names to access this site, other domain names will jump to http://www.aaa.com

Apache Rewrite rules:
RewriteCond %{HTTP_HOST} ^(.*?).domain. com$
RewriteCond %{HTTP_HOST} !^qita.domain.com$
RewriteCond %{DOCUMENT_ROOT}/market/%1/index.htm -f
RewriteRule ^/wu/$ /market/%1/index.htm [ L]

Nginx’s if instruction does not support nesting, nor does it support multiple condition matching such as AND and OR. Compared with Apache’s RewriteCond, it is more troublesome. However, we can achieve this through the Nginx configuration writing method on the next page. Example:

Nginx Rewrite rule:
if ($host ~* ^(.*?).domain.com$) set $var_wupin_city $1;
set $var_wupin '1′;

if ($host ~* ^qita .domain.com$)

set $var_wupin '0′;

if (!-f $document_root/market/$var_wupin_city/index.htm)

set $var_wupin '0′;

if ($var_wupin ~ '1′)

rewrite ^/wu/$ /market/$var_wupin_city/index.htm last;
}

syntax of rewrite

syntax: rewrite regex replacement flag

Default: none

Scope: server, location, if

This directive changes URI in accordance with the regular expression and the replacement string. Directives are carried out in order of appearance in the configuration file.

This directive changes URI in accordance with the regular expression and the replacement string. Change the URI, or modify the string. The instructions are executed according to the order in the configuration file.

Be aware that the rewrite regex only matches the relative path instead of the absolute URL. If you want to match the hostname, you should use an if condition, like so:

Note that the rewrite regex only works for relative paths. If you want to pair hostnames you should use an if statement.

rewrite will only rewrite the path part and will not change the user's input parameters. Therefore, in the if rule here, you do not need to care about the parameters entered by the user in the browser. They will be added automatically after rewrite. Therefore, we just add Got one? No. and a small parameter we want later, ***https=1.

nginx rewrite rule reference:

  1. ~ is case-sensitive matching
  2. ~* is case-insensitive matching
  3. !~ and !~* are case-sensitive mismatch and case-insensitive mismatch respectively

  1. -f and !-f are used to determine whether a file exists
  2. -d and !-d are used to determine whether a directory exists
  3. -e and !-e are used to determine whether a file or directory exists
  4. -x and !-x are used to determine whether the file is executable

  1. last is equivalent to the [L] mark in Apache, indicating completion of rewrite, haha ​​This should be the most commonly used
  2. break to terminate the match and no longer match The following rules
  3. redirect return 302 temporary redirection address bar will display the address after the jump
  4. permanent return 301 permanent redirection address bar will display the address after the jump

  1. $args
  2. $content_length
  3. $content_type
  4. $document_root
  5. $document_uri
  6. $host
  7. $http_user_agent
  8. $http_cookie
  9. $limit_rate
  10. $request_body_file
  11. $request_method
  12. $remote_addr
  13. $remote_port
  14. $remote_user
  15. $ request_filename
  16. $request_uri
  17. $query_string
  18. $scheme
  19. $server_protocol
  20. $server_addr
  21. $server_name
  22. $server_port
  23. $uri

Example of combining QeePHP

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

Convert multiple directories into parameters
abc.domian.com/sort/2 => abc.domian.com/index .php?act=sort&name=abc&id=2

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

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

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

For example, the following setting nginx redirects to the /nginx-ie directory when the user uses ie:

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

The directory automatically adds "/"

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

htaccess prohibited

  1. location ~/.ht {
  2. deny all;
  3. }

Ban multiple directories

  1. location ~ ^/(cron|templates)/ {
  2. deny all;
  3. break;
  4. }

Prohibit files starting with /data
You can ban /data/ Requests such as .log.txt under multi-level directories;

  1. location ~ ^/data {
  2. deny all;
  3. }

Single directory is prohibited
Cannot prohibit .log.txt requests

  1. location /searchword/cron/ {
  2. deny all;
  3. }

Disallow individual files

  1. location ~ /data/sql/data.sql {
  2. deny all;
  3. }

to favicon .ico and robots.txt set the expiration time;
here is 99 days for favicon.ico, 7 days for robots.txt and no 404 error log is recorded

  1. location ~(favicon.ico) {
  2. log_not_found off;
  3. expires 99d;
  4. break;
  5. }

  6. location ~(robots.txt) {
  7. log_not_found off;
  8. expires 7d;
  9. break;
  10. }

Set a file The expiration time; here is 600 seconds, and no access log is recorded

  1. location ^~ /html/scripts/loadhead_1.js {
  2. access_log off;
  3. root /opt/lampp/htdocs/web;
  4. expires 600;
  5. break;
  6. }

File anti-hotlinking and set expiration time
The return 412 here is a custom http status code, the default is 403, which is convenient for finding the correct hotlinking request
"rewrite ^/ http://leech.divmy.com/leech.gif;" Display an anti-hotlink picture
"access_log off;" Do not record access logs, reduce stress
"expires 3d" Browser cache of all files for 3 days

  1. location ~* ^.+.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
  2. valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194 ;
  3. if ($invalid_referer) {
  4. rewrite ^/ http://leech.divmy.com/leech.gif;
  5. return 412;
  6. break;
  7. }
  8. access_log off;
  9. root /opt /lampp/htdocs/web;
  10. expires 3d;
  11. break;
  12. }

Only allow fixed IP to access the website and add password

  1. root /opt/htdocs/www;
  2. allow 208.97.167.194;
  3. allow 222.33.1.2;
  4. allow 231.152.49.4;
  5. deny all;
  6. auth_basic “C1G_ADMIN”;
  7. auth_basic_user_file htpasswd ;

Convert files in multi-level directories into one file , enhance seo effect
/job-123-456-789.html points to /job/123/456/789.html

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

Point a folder in the root directory to the second-level directory
such as /shanghaijob/ points to / area/shanghai/
If you change last to permanent, the browser address bar displays /location/shanghai/

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

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

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

In this way, /shanghai can also be accessed, but the relative links in the page cannot Use,
For example, the real address of ./list_1.html is /area/shanghia/list_1.html, which will become /list_1.html, making it inaccessible.

Then it won’t work if I add automatic jump
(-d $request_filename) It has a condition that it must be a real directory, but my rewrite is not, so it has no effect

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

It will be easier once you know the reason, let me jump manually

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

Redirect when files and directories do not exist:

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

Domain name jump

  1. server
  2. {
  3. listen   80;
  4. server_name jump.88dgw.com;
  5. index index.html index.htm index.php;
  6. root /opt/lampp/htdocs/www;
  7. rewrite ^/ http://www.88dgw.com/;
  8. access_log off;
  9. }

Multiple domain name redirection

  1. server_name www.7oom.com/ www.divmy.com/;
  2. index index.html index.htm index.php;
  3. root /opt/lampp/htdocs;
  4. if ($host ~ “c1gstudio.net”) {
  5. rewrite ^(.*) http://www. 7oom.com$1/ permanent;
  6. }

Third-level domain name jump

  1. if ($http_host ~* “^(.*).i.c1gstudio.com$”) {
  2. rewrite ^( .*) http://top.88dgw.com$1/;
  3. break;
  4. }

Domain name mirror

  1. server
  2. {
  3. listen 80;
  4. server_name mirror.c1gs tudio.com ;
  5. index index.html index.htm index.php;
  6. root /opt/lampp/htdocs/www;
  7. rewrite ^/(.*) http://www.divmy.com/$1 last;
  8. access_log off;
  9. }

Mirror a subdirectory

  1. location ^~ /zhaopinhui {
  2. rewrite ^.+ http://zph.divmy.com/ last;
  3. break;
  4. }

discuz ucenter home (uchome) rewrite

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

discuz 7 rewrite

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

to discuz Configure the domain name separately for the section

  1. server_name bbs.c1gstudio.com news.c1gstudio.com;

  2. location = / {
  3. if ($http_host ~ news.divmy.com$) {
  4. rewrite ^.+ http://news.divmy.com/forum-831-1.html last;
  5. break;
  6. }
  7. }

discuz ucenter avatar rewrite optimization

  1. location ^~ /ucenter {
  2. location ~ .*.php?$
  3. {
  4. #fastcgi_pass unix:/tmp/php-cgi.sock;
  5. fastcgi_pass 127.0.0.1:9000;
  6. fastcgi_index index.php;
  7. include fcgi.conf;
  8. }

  9. location /ucenter/data/avatar {
  10. log_not_found off;
  11. access_log off;
  12. location ~ /(.*)_big.jpg$ {
  13. error_page 404 /ucenter/images/noavatar_big. gif;
  14. }
  15. location ~ /(.*)_middle.jpg$ {
  16. error_page 404 /ucenter/images/noavatar_middle.gif;
  17. }
  18. location ~ /(.*)_small.jpg$ {
  19. error_page 404 /ucenter/images/noavatar_small.gif;
  20. }
  21. expires 300;
  22. break;
  23. }
  24. }

jspace rewrite

  1. location ~ .*. php?$
  2. {
  3. #fastcgi_pass unix:/tmp/php-cgi.sock;
  4. fastcgi_pass 127.0.0.1:9000;
  5. fastcgi_index index.php;
  6. include fcgi.conf;
  7. }

  8. location ~* ^/index.php/
  9. {
  10. rewrite ^/index.php/(.*) /index.php?$1 break;
  11. fastcgi_pass 127.0.0.1:9000;
  12. fastcgi_index index.php;
  13. include fcgi .conf;
  14. }

In addition, there is a tool that can directly convert apache rules into nginx rules

http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

Reference :

http://wiki.nginx.org/NginxChsHttpRewriteModule

http://blog.csdn.net/cnbird2008/archive/2009/08/04/4409620.aspx

http://www.divmy. com/

The above has introduced some things about Nginx configuration file and ReWriteUrl~, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!