douji8347 2015-06-04 00:21
浏览 32
已采纳

Nginx / Fcgi:index.php与伪别名位置的问题

Nginx 1.6.2 on Debian Jessie

I want to map all example.com/forum/ requests to /path/to/htdocs/phpbb and cut off the /forum/ part in the URI. Someone on Stackoverflow recommended the "rewrite" solution instead of "alias", because there are some bugs.

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;
        }
    }
}

The example configuration works fine – example.com/forum/viewtopic.php executes the script /path/to/htdocs/phpbb/viewtopic.php – but example.com/ (index.php) doesn't work:

"/var/www/html/index.php" failed (2: No such file or directory)

After removing the "index" line from server block:

directory index of "/path/to/htdocs/phpbb/" is forbidden

After moving the "index" and/or "try_files" line(s) into the location block:

index.php served without passing over to php-fpm…

Ok, what's wrong with my config? Any hints?

  • 写回答

1条回答 默认 最新

  • dou4381 2015-06-04 11:27
    关注

    Ok, alias is buggy (rewrite too…), but if you avoid try_files and use if instead (even if evil…) it should work!

    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() looks fine, but one question remains: Is it secure?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改