nginx 配置说明 rewrite

项目配置:


server {

    listen 807;
 
    server_name localhost;
    root /data/web/myblog/web;
    client_max_body_size 501m;
 
    error_log /data/server/nginx/symfony2.error.log;
    access_log /data/server/nginx/symfony2.access.log;
 

    # strip app.php/ prefix if it is present

   #  如果出现192.168.1.53:807/app.php/blog/index 那么直接重定向到 192.168.1.53:807/blog/index

    rewrite ^/app\.php/?(.*)$ /$1 permanent;

 

  # 上面重定向后  符合到这个location  那么又进行try_files  找这个uri  如果找到直接返回uri 找不到重定向到 rewriteapp 这个loacation 进行重写 又再次变成

 #192.168.1.53:807/app.php/blog/index


    location / {
        index index.php;
        try_files $uri @rewriteapp;
    }
 
    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }
 
    # pass the PHP scripts to FastCGI server from upstream phpfcgi
    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/tmp/php.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi.conf;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS off;
    }

 location ~ \.php$ {
        fastcgi_pass unix:/tmp/php.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi.conf;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS off;

    }




解析

fastcgi_split_path_info

目的:让php能够解析类似这样的url http://www.92csz.com/index.php/abc/def
在默认情况下我们打开这个url时会出现无法找到该页。这就需要在nginxpath_info设置了。
原理:把index.php做为php执行的脚本,把/abc/def做为参数传给php-cgi执行。
实现:nginx版本0.7.31以上支持fastcgi_split_path_info,这个指令可以设置SCRIPT_FILENAME和PATH_INFO的变量,用正则表达式将这两部分分开.
例子:我们在nginx配置文件"local"区块中加入以下代码。

  1. location ~ ^.+\.php   
  2. {   
  3.     fastcgi_pass 127.0.0.1:9000;   
  4.     fastcgi_split_path_info ^(.+\.php)(.*)$;   
  5.     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;   
  6.     fastcgi_param PATH_INFO $fastcgi_path_info;   
  7.     fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;   
  8.     (...)   
  9. }  

重启nginx服务

  1. /usr/local/nginx/sbin/nginx -s reload

当nginx处理http://www.92csz.com/index.php/abc/def请求时,将会把"index.php"做为php的脚本,/abc/def做为index.php脚本的参数提交给php-cgi执行


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值