ÔÎÄ£ºhttp://www.cnblogs.com/cgli/archive/2011/05/16/2047920.html
×î½üÔÚVPSÉϳ¢ÊÔÅäÖð²×°Ò»¸öÍøÕ¾£¬VPS°²×°ÁËLNMP£¨Linux+Nginx+MySQL+php£©ÔÚÅäÖÃÖض¨¹æÔòµÄʱºò¾³£Óöµ½Ò»Ð©ÎÊÌ⣬ֱ½ÓÓÃApacheµÄ¹æÔòµ½NginxÏÂûÆð×÷Óá£ÔÀ´Apache ÖØдµÄ¹æÔòµ½nginxÉÏ»¹ÓÐһЩ²»Ì«Ò»Ñù¡£
ÕâÀïÖ»ÊǼòµ¥¼Ç¼һЩѧϰʾÀý£¬¸ßÊÖÂÔ¹ý£¬ÐÂÊÖ¿ÉÒÔ¿´Ò»Ï¡£
Nginx Rewrite¹æÔòÏà¹ØÖ¸Áî
Nginx Rewrite¹æÔòÏà¹ØÖ¸ÁîÓÐif¡¢rewrite¡¢set¡¢return¡¢breakµÈ£¬ÆäÖÐrewriteÊÇ×î¹Ø¼üµÄÖ¸Áî¡£Ò»¸ö¼òµ¥µÄNginx Rewrite¹æÔòÓï·¨ÈçÏ£º
rewrite ^/b/(.*)\.html /play.php?video=$1 break;
Èç¹û¼ÓÉÏifÓï¾ä£¬Ê¾ÀýÈçÏ£º
if (!-f $request_filename)
rewrite ^/img/(.*)$ /site/$host/images/$1 last;
NginxÓëApacheµÄRewrite¹æÔòʵÀý¶Ô±È
¼òµ¥µÄNginxºÍApache ÖØд¹æÔòÇø±ð²»´ó£¬»ù±¾ÉÏÄܹ»ÍêÈ«¼æÈÝ¡£ÀýÈ磺
Apache Rewrite ¹æÔò£º
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 ¹æÔò£º
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;
ÓÉÒÔÉÏʾÀý¿ÉÒÔ¿´³ö£¬ApacheµÄRewrite¹æÔò¸ÄΪNginxµÄRewrite¹æÔò£¬ÆäʵºÜ¼òµ¥£ºApacheµÄRewriteRuleÖ¸Áî»»³ÉNginxµÄrewriteÖ¸ÁApacheµÄ[L]±ê¼Ç»»³ÉNginxµÄlast±ê¼Ç£¬ÖмäµÄÄÚÈݲ»±ä¡£
Èç¹ûApacheµÄRewrite¹æÔò¸ÄΪNginxµÄRewrite¹æÔòºó£¬Ê¹ÓÃnginx -tÃüÁî¼ì²é·¢ÏÖnginx.confÅäÖÃÎļþÓÐÓï·¨´íÎó£¬ÄÇô¿ÉÒÔ³¢ÊÔ¸øÌõ¼þ¼ÓÉÏÒýºÅ¡£ÀýÈçһϵÄNginx Rewrite¹æÔò»á±¨Óï·¨´íÎó£º
rewrite ^/([0-9]{5}).html$ /x.jsp?id=$1 last;
¼ÓÉÏÒýºÅ¾ÍÕýÈ·ÁË£º
rewrite ¡°^/([0-9]{5}).html$¡± /x.jsp?id=$1 last;
ApacheÓëNginxµÄRewrite¹æÔòÔÚURLÌøתʱÓÐϸ΢µÄÇø±ð£º
Apache Rewrite ¹æÔò£º
RewriteRule ^/html/tagindex/([a-zA-Z]+)/.*$ /$1/ [R=301,L]
Nginx Rewrite ¹æÔò£º
rewrite ^/html/tagindex/([a-zA-Z]+)/.*$ http://$host/$1/ permanent;
ÒÔÉÏʾÀýÖУ¬ÎÒÃÇ×¢Òâµ½£¬Nginx Rewrite ¹æÔòµÄÖû»´®ÖÐÔö¼ÓÁË¡°http://$host¡±£¬ÕâÊÇÔÚNginxÖÐÒªÇóµÄ¡£
ÁíÍ⣬ApacheÓëNginxµÄRewrite¹æÔòÔÚ±äÁ¿Ãû³Æ·½ÃæÒ²ÓÐÇø±ð£¬ÀýÈ磺
Apache Rewrite ¹æÔò£º
RewriteRule ^/user/login/$ /user/login.php?login=1&forward=http://%{HTTP_HOST} [L]
Nginx Rewrite ¹æÔò£º
rewrite ^/user/login/$ /user/login.php?login=1&forward=http://$host last;
ApacheÓëNginx Rewrite ¹æÔòµÄһЩ¹¦ÄÜÏàͬ»òÀàËƵÄÖ¸Áî¡¢±ê¼Ç¶ÔÓ¦¹Øϵ£º
ApacheµÄRewriteCondÖ¸Áî¶ÔÓ¦NginxµÄifÖ¸Á
ApacheµÄRewriteRuleÖ¸Áî¶ÔÓ¦NginxµÄrewriteÖ¸Á
ApacheµÄ[R]±ê¼Ç¶ÔÓ¦NginxµÄredirect±ê¼Ç£»
ApacheµÄ[P]±ê¼Ç¶ÔÓ¦NginxµÄlast±ê¼Ç£»
ApacheµÄ[R,L]±ê¼Ç¶ÔÓ¦NginxµÄredirect±ê¼Ç£»
ApacheµÄ[P,L]±ê¼Ç¶ÔÓ¦NginxµÄlast±ê¼Ç£»
ApacheµÄ[PT,L]±ê¼Ç¶ÔÓ¦NginxµÄlast±ê¼Ç£»
ÔÊÐíÖ¸¶¨µÄÓòÃû·ÃÎʱ¾Õ¾£¬ÆäËûÓòÃûÒ»ÂÉÌøתµ½http://www.aaa.com
Apache Rewrite ¹æÔò£º
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µÄifÖ¸Áî²»Ö§³ÖǶÌ×£¬Ò²²»Ö§³ÖAND¡¢ORµÈ¶àÌõ¼þÆ¥Å䣬Ïà±ÈÓÚApacheµÄRewriteCond£¬ÏÔµÃÂ鷳һЩ£¬µ«ÊÇ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÏÂÒ»Ò³µÄNginxÅäÖÃд·¨À´ÊµÏÖÕâ¸öʾÀý£º
Nginx Rewrite ¹æÔò£º
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;
}
rewrite µÄÓï·¨
Óï·¨: rewrite regex replacement flag
ĬÈÏ: none
×÷ÓÃÓò: 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.
Õâ¸öÖ¸Áî¸ù¾Ý±í´ïʽÀ´¸ü¸ÄURI£¬»òÕßÐÞ¸Ä×Ö·û´®¡£Ö¸Áî¸ù¾ÝÅäÖÃÎļþÖеÄ˳ÐòÀ´Ö´ÐС£
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:
×¢ÒâÖØд±í´ïʽֻ¶ÔÏà¶Ô·¾¶ÓÐЧ¡£Èç¹ûÄãÏëÅä¶ÔÖ÷»úÃû£¬ÄãÓ¦¸ÃʹÓÃifÓï¾ä¡£
rewriteÖ»ÊÇ»á¸Äд·¾¶²¿·ÖµÄ¶«¶«£¬²»»á¸Ä¶¯Óû§µÄÊäÈë²ÎÊý£¬Òò´ËÕâÀïµÄif¹æÔòÀïÃ棬ÄãÎÞÐè¹ØÐÄÓû§ÔÚä¯ÀÀÆ÷ÀïÊäÈëµÄ²ÎÊý£¬rewriteºó»á×Ô¶¯Ìí¼ÓµÄ£¬Òò´Ë£¬ÎÒÃÇÖ»ÊǼÓÉÏÁËÒ»¸ö£¿ºÅºÍºóÃæÎÒÃÇÏëÒªµÄÒ»¸öССµÄ²ÎÊý ***https=1¾Í¿ÉÒÔÁË¡£
nginxµÄrewrite¹æÔò²Î¿¼£º
- ~ ΪÇø·Ö´óСдƥÅä
- ~* Ϊ²»Çø·Ö´óСдƥÅä
- !~ºÍ!~*·Ö±ðΪÇø·Ö´óСд²»Æ¥Åä¼°²»Çø·Ö´óСд²»Æ¥
- -fºÍ!-fÓÃÀ´ÅжÏÊÇ·ñ´æÔÚÎļþ
- -dºÍ!-dÓÃÀ´ÅжÏÊÇ·ñ´æÔÚĿ¼
- -eºÍ!-eÓÃÀ´ÅжÏÊÇ·ñ´æÔÚÎļþ»òĿ¼
- -xºÍ!-xÓÃÀ´ÅжÏÎļþÊÇ·ñ¿ÉÖ´ÐÐ
- last Ï൱ÓÚApacheÀïµÄ[L]±ê¼Ç£¬±íʾÍê³Érewrite£¬ºÇºÇÕâÓ¦¸ÃÊÇ×î³£ÓõÄ
- break ÖÕֹƥÅä, ²»ÔÙÆ¥ÅäºóÃæµÄ¹æÔò
- redirect ·µ»Ø302ÁÙʱÖض¨Ïò µØÖ·À¸»áÏÔʾÌøתºóµÄµØÖ·
- permanent ·µ»Ø301ÓÀ¾ÃÖض¨Ïò µØÖ·À¸»áÏÔʾÌøתºóµÄµØÖ·
- $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
½áºÏQeePHPµÄÀý×Ó
- 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;
¶àĿ¼ת³É²ÎÊý
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;
- }
Ŀ¼¶Ô»»
/123456/xxxx -> /xxxx?id=123456
- rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;
ÀýÈçÏÂÃæÉ趨nginxÔÚÓû§Ê¹ÓÃieµÄʹÓÃÖض¨Ïòµ½/nginx-ieĿ¼Ï£º
- if ($http_user_agent ~ MSIE) {
- rewrite ^(.*)$ /nginx-ie/$1 break;
- }
Ŀ¼×Ô¶¯¼Ó¡°/¡±
- if (-d $request_filename){
- rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
- }
½ûÖ¹htaccess
- location ~/\.ht {
- deny all;
- }
½ûÖ¹¶à¸öĿ¼
- location ~ ^/(cron|templates)/ {
- deny all;
- break;
- }
½ûÖ¹ÒÔ/data¿ªÍ·µÄÎļþ
¿ÉÒÔ½ûÖ¹/data/϶༶Ŀ¼ÏÂ.log.txtµÈÇëÇó;
- location ~ ^/data {
- deny all;
- }
½ûÖ¹µ¥¸öĿ¼
²»ÄܽûÖ¹.log.txtÄÜÇëÇó
- location /searchword/cron/ {
- deny all;
- }
½ûÖ¹µ¥¸öÎļþ
- location ~ /data/sql/data.sql {
- deny all;
- }
¸øfavicon.icoºÍrobots.txtÉèÖùýÆÚʱ¼ä;
ÕâÀïΪfavicon.icoΪ99Ìì,robots.txtΪ7Ìì²¢²»¼Ç¼404´íÎóÈÕÖ¾
- location ~(favicon.ico) {
- log_not_found off;
- expires 99d;
- break;
- }
- location ~(robots.txt) {
- log_not_found off;
- expires 7d;
- break;
- }
É趨ij¸öÎļþµÄ¹ýÆÚʱ¼ä;ÕâÀïΪ600Ã룬²¢²»¼Ç¼·ÃÎÊÈÕÖ¾
- location ^~ /html/scripts/loadhead_1.js {
- access_log off;
- root /opt/lampp/htdocs/web;
- expires 600;
- break;
- }
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ