【问题标题】:Slash arguments in Nginx, PHPNginx,PHP中的斜线参数
【发布时间】:2012-05-21 16:27:18
【问题描述】:

安装 Symfony2 后,我正在尝试访问此 URL:

http://test/symfony/web/app_dev.php/

但它不起作用,因为尾部斜杠,我得到一个 404。

这些工作正常:

http://test/symfony/web/app_dev.php
http://test/symfony/web/app_dev.php?/

在 Apache 中,/app_dev.php/something 和 /app_dev.php?/something 是相同的。关于如何在 Nginx 中进行这项工作的任何想法?

我尝试添加重写,但没有成功:

    location / {
            try_files $uri $uri/ @rewrite index.html;
    }

    location @rewrite {
            rewrite ^/(.*\.php)/(.*)$ $1?/$2;
    }

【问题讨论】:

    标签: php apache symfony nginx


    【解决方案1】:

    这就是我所拥有的,并且工作正常:

        location / {
                # Remove app.php from url, if somebody added it (doesn't remove if asked for /app.php on the root)
                rewrite ^/app\.php/(.*) /$1 permanent;
    
                try_files $uri $uri/ /app.php?$query_string;
        }
    
        location ~ \.php$ {
                try_files $uri = 404;
                # Below is for fastcgi:
                # fastcgi_pass    127.0.0.1:9000;
                # fastcgi_index   app.php;
                # include         /etc/nginx/fastcgi_params;
                # fastcgi_param   SCRIPT_FILENAME /mnt/www/live/current/web$fastcgi_script_name;
                # fastcgi_param   SERVER_PORT 80;
                # fastcgi_param   HTTPS off;
        }
    

    【讨论】:

    • try_files 行应该是try_files $uri =404 而不是try_files $uri = 404。额外的空间会使 nginx 寻找$document_root$uri$document_root=$document_root404
    【解决方案2】:

    我终于偶然发现了一个适合我的答案:

        location ~ /directory/(.*\.php)(/.*)$ {
                set $script_filename $1;
                set $path_info $2;
                fastcgi_param  PATH_INFO        $2;
                alias /place/directory/$1;
                include fastcgi_params;
    

    https://github.com/plack/Plack/issues/281

    这是文档中相关的 sn-p:请注意,它们都没有使用 nginx 提供的 FCGI 参数作为 script_name 或 path_info,因为已知它是不正确的。

    location / {
     set $script "";
     set $path_info $uri;
     fastcgi_pass unix:/tmp/fastcgi.sock;
     fastcgi_param  SCRIPT_NAME      $script;
     fastcgi_param  PATH_INFO        $path_info;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-17
      • 2017-02-05
      • 2014-12-21
      • 1970-01-01
      • 2021-02-02
      • 2013-06-29
      • 2013-04-08
      • 1970-01-01
      相关资源
      最近更新 更多