rewrite模块
文档http://nginx.org/en/docs/http/ngx_http_core_module.html搜索rewriteSyntax: internal;Default: —Context: location #放在location里location /{ root html; index index.html index.htm; } 重写中用到的指令if (条件) {} 设定条件,再进行重写 #空格不能少set #设置变量return #返回状态码 break #跳出rewriterewrite #重写If 语法格式If 空格 (条件) { 重写模式}条件又怎么写?答:3种写法1: “=”来判断相等, 用于字符串比较2: “~” 用正则来匹配(此处的正则区分大小写) ~* 不区分大小写的正则3: -f -d -e来判断是否为文件,为目录,是否存在.[root nginx]# vi html/test-if.html[root nginx]# ./sbin/nginx -s reloadhttp://192.168.88.170/test-if.html[root nginx]# tail logs/access.log 192.168.88.1 - - [03/Mar/2016:03:25:55 -0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"192.168.88.1 - - [03/Mar/2016:03:25:55 -0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"192.168.88.1 - - [03/Mar/2016:03:25:56 -0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"192.168.88.1 - - [03/Mar/2016:03:26:06 -0800] "GET /test-if.html HTTP/1.1" 200 8 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';[root nginx]# vi conf/nginx.conf location /{ if ($remote_addr = 192.168.88.1) { return 403; } root html; index index.html index.htm; }[root nginx]# ./sbin/nginx -s reload[root@localhost nginx]# vi conf/nginx.conf location /{ if ($http_user_agent ~ MSIE) { rewrite ^.*$ /ie.htm; break; #(不break会循环重定向) } root html; index index.html index.htm; }[root@localhost nginx]# vi html/ie.htm[root@localhost nginx]# ./sbin/nginx -s reload用chrom访问正常到了nginx页面用ie访问不正常http://192.168.88.170/ 结果ie[root@localhost nginx]# ls conf/fastcgi.conf koi-win scgi_paramsfastcgi.conf.default mime.types scgi_params.defaultfastcgi_params mime.types.default uwsgi_paramsfastcgi_params.default nginx.conf uwsgi_params.defaultkoi-utf nginx.conf.default win-utf[root@localhost nginx]# more conf/fastcgi.conffastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param HTTPS $https if_not_empty;[root@localhost nginx]# vi html/404.htm if (!-e $document_root$fastcgi_script_name) { rewrite ^.*$ /404.html break; } 注, 此处还要加break,以 xx.com/dsafsd.html这个不存在页面为例,我们观察访问日志, 日志中显示的访问路径,依然是GET /dsafsd.html HTTP/1.1提示: 服务器内部的rewrite和302跳转不一样. 跳转的话URL都变了,变成重新http请求404.html, 而内部rewrite, 上下文没变,就是说 fastcgi_script_name 仍然是 dsafsd.html,因此 会循环重定向.set 是设置变量用的, 可以用来达到多条件判断时作标志用.达到apache下的 rewrite_condition的效果location /{ if ($http_user_agent ~* msie) { set $isie 1; } if ($fastcgi_script_name = ie.html) { set $isie 0; } if ($isie 1) { rewrite ^.*$ ie.html; } root html; index index.html index.htm; } }