【问题标题】:Nginx/Fcgi: index.php issue with pseudo-alias locationNginx/Fcgi:伪别名位置的 index.php 问题
【发布时间】:2015-06-04 23:51:32
【问题描述】:

Debian Jessie 上的 Nginx 1.6.2

我想将所有 example.com/forum/ 请求映射到 /path/to/htdocs/phpbb 并切断 /forum/ em> 部分在 URI 中。 Stackoverflow 上有人推荐了 "rewrite" 解决方案,而不是 "alias",因为存在一些错误。

server
{
    listen [::]:80;
    server_name example.com;
    root /var/www/html;

    index index.php index.html;
    #try_files $uri $uri/ =404;

    location /forum/
    {
        root /path/to/htdocs/phpbb;
        rewrite ^/forum/(.*)$ /$1 break;

        location ~ .+\.php$
        {
            rewrite ^/forum/(.*)$ /$1 break;
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    }
}

示例配置工作正常 - example.com/forum/viewtopic.php 执行脚本 /path/to/htdocs/phpbb/viewtopic.php - 但是 example.com/ (index.php) 不起作用:

“/var/www/html/index.php”失败(2:没有那个文件或目录)

从服务器块中删除 "index" 行后:

“/path/to/htdocs/phpbb/”的目录索引被禁止

"index" 和/或 "try_files" 行移动到位置块后:

index.php 没有传递给 php-fpm…

好的,我的配置有什么问题?有什么提示吗?

【问题讨论】:

    标签: php nginx fastcgi


    【解决方案1】:

    好吧,别名是错误的(rewrite 太...),但如果你避免 try_files 并使用 if 代替(即使邪恶...)它应该工作!

    server
    {
        listen [::]:80;
        server_name example.com;
        root /var/www/html;
    
        location /forum/
        {
            alias /path/to/htdocs/phpbb/;
            index index.php index.html;
    
            location ~ "^(/forum/)(.+\.php)(/.+){0,1}$"
            {
                if (!-f $document_root$2)
                {
                    return 404;
                }
    
                fastcgi_index index.php;
                include fastcgi.conf;
    
                fastcgi_param  SCRIPT_FILENAME    $document_root$2;
                fastcgi_param  SCRIPT_NAME        $1$2;
                fastcgi_param  PATH_INFO          $3;
    
                fastcgi_pass unix:/var/run/php5-fpm.sock;
            }
        }
    }
    

    phpinfo() 看起来不错,但还有一个问题:它安全吗?

    【讨论】:

      猜你喜欢
      • 2018-08-06
      • 2017-05-29
      • 2016-02-10
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 2016-10-20
      相关资源
      最近更新 更多