首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在位置块中组合具有不同根的重写

如何在位置块中组合具有不同根的重写
EN

Stack Overflow用户
提问于 2014-03-05 02:05:42
回答 1查看 274关注 0票数 0

当执行重写的位置块需要不同的根时,我正在努力理解如何控制对PHP脚本的重写。

这是一个简化的例子。我的通用前端控制器必须在web根目录中,但firewall.php脚本不能在此目录中。

这样做的目的是提供对不在根目录下的下载文件的门控访问。

代码语言:javascript
复制
server {

    # Requests for media forced through firewall script outside web root
    # /path/to/secure-area/firewall.php
    #
    location ^~ /downloads/ {
       root /path/to/secure-area;
       rewrite ^/.+$ /firewall.php last;
    }

    # Regular requests bootstrap front controller under web-root
    # /path/to/web-root/index.php;
    #
    location / {
        root /path/to/web-root;
        index index.php
        if ( -e $request_filename ) {
            break;
        }
        rewrite ^.+$ /index.php last;
    }

    # Execute PHP via php-fpm
    # rewrites hitting this are not mapping request_filename properly
    #
    location ~ \.php$ {
       if ( !-f $request_filename ) {
         return 404;
       }
       include /usr/local/nginx/conf/fastcgi_params;
       fastcgi_pass   unix:/tmp/php.sock;
       fastcgi_index  index.php;
    }
}

显而易见的解决方案是有一个共同的根,但这在我的情况下是不可能的。我不能将安全位置放在web根目录下,并且web根目录必须保持原样。

看起来root指令似乎只在定义它的location块中有效。各个重写都工作得很好,但是当命中~ \.php块时,根就会丢失。

很明显我做错了,那我该怎么做呢?

EN

Stack Overflow用户

发布于 2014-03-05 03:39:41

未经测试,但像这样的东西应该会对你有所帮助。它使用http://wiki.nginx.org/XSendfile从不同的根为受保护的内容提供服务。也使用try_files,这是一个更好的前端控制器模式。

代码语言:javascript
复制
    server {

    # More here: http://wiki.nginx.org/XSendfile
    #
    # To serve /downloads/some.zip
    # get php to set the http header:
    #
    # X-Accel-Redirect: /downloads/some.zip
    #
    # and then the file /path/to/secure-area/downloads/some.zip
    # will be sent by nginx
    location /downloads/ {
      internal;
      root   /path/to/secure-area;
    }        

    location / {
        root /path/to/web-root;
        index index.php
        try_files $uri $uri/ /index.php;
    }

    # make sure you read http://wiki.nginx.org/Pitfalls
    location ~* \.php$ {
      try_files $uri =404;
      fastcgi_pass   unix:/tmp/php.sock;
      fastcgi_index  index.php;
      include /usr/local/nginx/conf/fastcgi_params;
    }
}
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22179582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档