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?

图片转代码服务由CSDN问答提供 功能建议

Debian Jessie上的Nginx 1.6.2

我想将所有 example.com/forum / 请求映射到 / path / to / htdocs / phpbb 并切断 / forum / 参与URI。 Stackoverflow上的某个人推荐“重写”解决方案而不是“别名”,因为存在一些错误。

  server  
 {
 listen [::]:80; 
 server_name example.com; 
 root / var / www / html; 
 
 index index.php index.html; 
 #try_files $ uri $ uri  / = 404; 
 
位置/论坛/ 
 {
 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 /”被禁止< / p>

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

index.php在没有传递给php-fpm的情况下提供...

好的,出了什么问题 用我的配置? 任何提示?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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?

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题