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?

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

报告相同问题?

悬赏问题

  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败
  • ¥150 计算DC/DC变换器平均模型中的参数mu
  • ¥25 C语言代码,大家帮帮我
  • ¥15 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?
  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)