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;

    }


扫描二维码关注公众号,回复: 11257686 查看本文章



解析

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执行


猜你喜欢

转载自blog.csdn.net/a519640026/article/details/9144027