rewrite:

什么是Rewrite?

        Rewrite实际上就是URL Rewrite,即URL重写,就是把传入Web的请求重定向到其他URL的过程。URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术。比如http://www.123.com/news/index.asp?id=123 使用URL Rewrite 转换后可以显示为 http://www.123.com/news/123.html。


        nginx的rewrite规则采用PCRE(perl compatible regular expressions)perl兼容正则表达式的语法进行规则匹配,因此,需要安装PCRE库。

        对于追求完美主义的网站设计师,就算是网页的地址也希望看起来尽量简洁明快。

        形如http://www.123.com/news/index.asp?id=123的网页地址,自然是毫无美感可言,而用Url Rewrite技术,你可以轻松把它显示为 http://www.123.com/news/123.html。

        

            理论上,搜索引擎更喜欢静态页面形式的网页,搜索引擎对静态页面的评分一般要高于动态页面。所以,Url Rewrite可以让我们网站的网页更容易被搜索引擎所收录。从安全角度上讲,如果在url中暴露太多的参数,无疑会造成一定量的信息泄漏,可能会被一些***利用,对你的系统造成一定的破坏,所以静态化的url地址可以给我们带来更高的安全性。


    通过URL规则,可以实现规范的URL、根据变量来做URL装箱机选择设置。如:一些由于目录结构、域名变化的旧的URL,需跳转到新的URL上,就可以通过rewrite来处理等等。


rewrite规则的相关指令有if、rewrite、set、return、break等。其中rewrite是关键指令。

在rewrite里有4个关键字:

(1)last :表示完成rewrite

(2)break:本条规则匹配完成后,终止匹配,不再匹配后面的规则

    last和break用来实现URL重写,URL地址不变,但是在服务器端访问的路径发生变化。

(3)redirect :返回302临时重定向,浏览器地址栏会显示跳转后的URL地址

(4)permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

        redirect和permanent用来实现URL跳转,URL地址会显示跳转后的地址。

-------------------------

     面试题:常见的HTTP状态码

         200            OK

         301            Permanent Redirect

         302

         404            NOT FOUND

         403            Forbidden

         500            

         502

         504                

-------------------------

例子:

(1)如果访问的“.html”结尾的文件则返回403操作拒绝错误

        [root@nginx nginx]# vim /usr/local/webserver/nginx/conf/nginx.conf

          server {

       listen  80;

       server_name     www.baidu.com;

       access_log  logs/baidu.access.log  main;

       location / {

       root    /web/www/baidu;

       index  index.html index.htm;

       }

       location ~* \.html$ {       // ~* 表示匹配不区分大小写

       return 403;         

       }

         }

        [root@nginx nginx]# nginx -t

        [root@nginx nginx]# nginx -s reload

        [root@nginx nginx]# firefox www.baidu.com &    //会返回403错误

(2)访问http://www.baidu.com/b/ 被rewrite重写到访问与“b”目录的同级目录/a/。

        [root@nginx nginx]# cd /web/www/baidu/

        [root@nginx baidu]# mkdir b

        [root@nginx baidu]# mkdir a

        [root@nginx baidu]# echo bbbbbb > b/index.html

        [root@nginx baidu]# echo aaaaaa > a/index.html

        [root@nginx nginx]# vim /usr/local/webserver/nginx/conf/nginx.conf

              server {

           listen  80;

           server_name     www.baidu.com;

           access_log  logs/baidu.access.log  main;

           location / {

           root    /web/www/baidu;

           index  index.html index.htm;

           }

           location /b/ {

                        proxy_pass http://www.baidu.com/a/;

                        rewrite "^/b/(.*)\.html" /web/www/baidu/a/index.html break;    

                    }

             } 

        [root@nginx nginx]# nginx -t

        [root@nginx nginx]# nginx -s reload

        [root@nginx nginx]# firefox www.baidu.com/b &           //返回的页面是aaaaaa


(3)把访问自己的页面 rewrite 到别的虚拟主机里去

        [root@nginx nginx]# vim /usr/local/webserver/nginx/conf/nginx.conf

            server {

                listen  80;

                server_name     www.baidu.com;

                access_log  logs/baidu.access.log  main;

                location / {

                        root    /web/www/baidu;

                        index  index.html index.htm;

                }

                location ~ [a-z]+.html {

                        rewrite ^/[a-z]+.html$ http://www.google.com/index.html break;

                }

          }

        [root@nginx nginx]# nginx -t

        [root@nginx nginx]# nginx -s reload

        [root@nginx nginx]# firefox www.baidu.com &           //返回的页面是google

(4)拒绝访问以“/123”开头的文件

        [root@nginx nginx]# vim /usr/local/webserver/nginx/conf/nginx.conf

            server {

                listen  80;

                server_name     www.baidu.com;

                access_log  logs/baidu.access.log  main;

                location / {

                        root    /web/www/baidu;

                        index  index.html index.htm;

                }

                location ~ [a-z]+.html {

                        rewrite ^/[a-z]+.html$ http://www.google.com/index.html break;

                }

                location ~ ^/123 {

                deny all;

                }

          }

        [root@nginx nginx]# nginx -t

        [root@nginx nginx]# nginx -s reload

        [root@nginx nginx]# echo 123 > /web/www/baidu/123.html

        [root@nginx nginx]# echo 456 > /web/www/baidu/456.html

        [root@nginx nginx]# echo 12345 > /web/www/baidu/12345.html

        [root@nginx nginx]# firefox www.baidu.com/123.html &

        [root@nginx nginx]# firefox www.baidu.com/12345.html &

        [root@nginx nginx]# firefox www.baidu.com/456.html &

(5)拒绝访问多个目录

        [root@nginx nginx]# vim /usr/local/webserver/nginx/conf/nginx.conf

            server {

                listen  80;

                server_name     www.baidu.com;

                access_log  logs/baidu.access.log  main;

                location / {

                        root    /web/www/baidu;

                        index  index.html index.htm;

                }

                location ~ [a-z]+.html {

                        rewrite ^/[a-z]+.html$ http://www.google.com/index.html break;

                }

                location ~ ^/(a|b)/ {

                        deny all;

                        break;

                }

          }

        [root@nginx nginx]# nginx -t

        [root@nginx nginx]# nginx -s reload

        [root@nginx nginx]# firefox www.baidu.com/a &

        [root@nginx nginx]# firefox www.baidu.com/b &

        

      这几个实验注意问题:修改配置文件后需要重新加载,浏览器有时候需要清除缓存。

      

      ——————————————————————————————————

      

      解决办法:

         www.baidu.com需要解析,可以使用dns,也可以使用hosts文件

         修改nginx主机上的/etc/hosts文件

                172.16.254.200    www.baidu.com

     ——————————————————————————————————

  

  

  nginx就是一个web服务器

        1)nginx和apache区别

        2)nginx虚拟主机

        3)nginx实现7层负载均衡  upstream

        4)虚拟主机+负载均衡+反向代理      实现给多个网站做反向加速代理,实现每个网站的负载均衡

        5)nginx动静分离   fastcgi

               将静态页面交给nginx或者apache

               将动态页面交给tomcat或者php

       6)URL重写  实现URL重定向,重写,还可以将动态页面伪装成静态页面(美观、安全)

       

       7)http各种状态码