【问题标题】:nginx, how to serve all static files and forward all other traffic to a fastcginginx,如何提供所有静态文件并将所有其他流量转发到 fastcgi
【发布时间】:2014-11-10 04:37:20
【问题描述】:

我想直接提供所有现有文件(.php 文件除外)

其余所有请求(不仅是 .php),将它们转发到 fastcgi 服务器。

我可以使用以下命令将所有 .php 文件重定向到 fastcgi:

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
}

并提供其他静态文件:

location / {
}

但我想首先提供“所有存在且不以 .php 结尾的静态文件” 然后,将“所有其他请求”(不必以 php 结尾)提供给 fastcgi,

有什么想法可以实现吗?

谢谢!

【问题讨论】:

    标签: php nginx fastcgi


    【解决方案1】:

    您可以为此使用try_files

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
    
        // other CGI parameters
    }
    

    确保您了解常见的pitfalls

    【讨论】:

    • 不工作,我需要区分 .php 文件
    • 仍然不工作,index.php 工作正常,现有文件(如 .jpg)工作正常,但与任何现有文件不匹配的路由不会转发到 fastcgi:404 不找到
    • 似乎已修复为: location / { try_files $uri $uri/ /index.php?$args; }
    • 我不确定你是否在使用前端控制器,所以试着让它更通用。更新了答案。
    • 谢谢!耐心是一种美德:-)
    猜你喜欢
    • 2015-09-01
    • 2017-04-07
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2011-10-04
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多