zoukankan      html  css  js  c++  java
  • nginx和apache下的url rewrite

    将服务器上面的数据同步到本地之后,发现打开首页显示不正常,本地服务器是apache,经过打开url rewrite之后本地首页正常显示。

    原因是phpwind本身支持了url rewrite的功能,但是本地的apache服务器没有开启这项功能,导致了首页的排版紊乱。

    远程服务器用的的nginx和本地的apache的url rewrite配置不能通用,借此机会学习下,url rewrite的功能。

    url rewrite是服务器的一个模块,功能包括,配置一些访问的网址的重写,其中的语句规则是基于正则表达式,建议先看我的另一篇博客正则表式http://www.cnblogs.com/yiluxiuxing/p/4307488.html

    其中涉及到的变量都是基于服务器上(apache或者nginx)通用的变量,具体一些变量详细解释以及nginx下rewrite的一些配置实例请参考http://www.cnblogs.com/yiluxiuxing/p/4309365.html

    比如为了使网址更加友好,可以将用户看到的网址www.simple.com/ming-tian-shi-ge-hao-tian-qi.html重定位到www.simple.com/ming/tian/shi/ge/hao/tian/qi.html,这样用户看到的就是一个网址而不是一个个的文件夹。

    其他还有很多有用的功能,比如,防止别的网站引用你网站的图片,如果别人使用的是你网站的图片的话,那么占用的是你的网站的流量,但是却不能给你带来访问量

    还比如可以自己写一个友好的404页面,如果发生404错误的时候就将页面定位到自己写的404页面。

    还可以将css文件还有js文件设置保存在用户浏览器上面的时间,加快网页的加载速度。

    下面是nginx上面的rewrite配置文件

     1 server {
     2         listen  80;
     3         server_name www.simple.com ;
     4         root /home/www/simple;
     5         index index.php index.html index.htm;
     6         charset utf-8;
     7         access_log logs/simple.access.log main;
     8         #如果请求主机字段不等于'www.simple.com'则重定向到http://www.simple.com/*
     9         if ( $host != 'www.simple.com' ) {
    10                 rewrite ^/(.*)$ http://www.simple.com/$1 permanent;
    11         }
    12         #如果当前请求的文件路径不存在,将出现/tool/的网址重定向到/tool/index.php,将出现kisswall/的网址重定向到/kisswall/index.php
    13         if ( !-e $request_filename ) { 
    14                 rewrite ^(.*)tool/(.*)$ $1tool/index.php last;
    15                 rewrite ^(.*)kisswall/(.*)$ $1kisswall/index.php last;
    16         }
    17         location / {
    18                 directio 1;
    19                 output_buffers 1 128k;
    20                 index index.php index.html index.htm ;
    21                 rewrite ^(.*?)-(.*)$ $1.php?$2;
    22         }
    23         #指定404错误页面
    24         error_page 404 /404.html;
    25                 location = /50x.html {
    26         }
    27         #设置js、css过期时间
    28         location ~ .(css|js)$ {
    29                 expires 1w;
    30         }
    31         #防盗链
    32         location ~ .(jpg|jpeg|png|gif|swf|ico)$ {
    33                 valid_referers none bloacked *.erqilu.com *.renren.com *.weibo.com;
    34                 if ( $invalid_referer ) {
    35                         return 404;
    36                 }
    37                 expires max;
    38         }    
    39         location ~ .php$ {
    40             fastcgi_pass   127.0.0.1:9000;
    41             fastcgi_index  index.php;
    42             fastcgi_param  SCRIPT_FILENAME  /$document_root$fastcgi_script_name;
    43             include        fastcgi_params;        
    44         }    
    45         #禁止htaccess
    46         location ~ /.ht { 
    47                 deny all;
    48         }
    49         #将出现/min/的网址定位到/min/index.php?*
    50         location /min/{ 
    51                 rewrite /min/([a-z]=.*)  /min/index.php?$1 last;
    52                 #expires 1w;
    53         }
    54 }   

    与之一部分对应的.htaccess文件

    如果访问的网址不是“localhost”或者“127.0.0.1”则跳转到http://localhost/
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^localhost [NC]
    RewriteCond %{HTTP_HOST} !^127.0.0.1 [NC]
    RewriteCond %{HTTP_HOST} !^$
    RewriteRule ^(.*) http://localhost/$1 [L] 
    
    RewriteEngine on RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule ^.*$ - [L]

    #如果访问的网址文件不存在,则如果网址中出现/tool/则将网址重写为$1tool/index.php 网址中若出现/kisswall/同理 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)tool/(.*)$ $1tool/index.php [L] RewriteRule ^(.*)kisswall/(.*)$ $1kisswall/index.php [L]
    RewriteEngine On DirectoryIndex index.php index.html index.htm
    #将以-分割的网址转换为$1.php?$2的格式 RewriteRule ^(.*)-htm-(.*)$ $1.php?$2 RewriteCond %{HTTP_HOST} !^localhost [NC] RewriteRule ^(.*) http://localhost/$1 [L] #定义404页面 ErrorDocument 404 /404.html #防盗链 RewriteBase / RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://localhost/.*$ [NC] RewriteRule .(jpg|jpeg|png|gif|swf|ico)$ - [R=302,L]

    对照可参考apache和nginx配置的异同,

    其中nginx的url rewrite配置文件存放的位置是:usr/local/nginx/conf 

  • 相关阅读:
    [LeetCode] 101. Symmetric Tree 对称树
    [LeetCode] 202. Happy Number 快乐数
    Windows任务计划创建计划,定时执行PowerShell命令
    读经典——《CLR via C#》(Jeffrey Richter著) 笔记_IL和验证
    读经典——《CLR via C#》(Jeffrey Richter著) 笔记_方法执行
    吐槽自己
    读经典——《CLR via C#》(Jeffrey Richter著) 笔记_元数据
    读经典——《CLR via C#》(Jeffrey Richter著) 笔记_CLR
    设置IIS,使客户端访问服务器上的文件
    SQL语句 ANSI_NULLS 值(ON|OFF)的含义
  • 原文地址:https://www.cnblogs.com/yiluxiuxing/p/4309347.html
Copyright © 2011-2022 走看看