【问题标题】:Apache try_files analogApache try_files 模拟
【发布时间】:2017-09-16 02:32:14
【问题描述】:

如何重新制作这个 nginx 配置

location / {
    try_files /public/$uri @app;
}

location @app {
    fastcgi_pass php5-fpm;
}

}

到 Apache 配置?

【问题讨论】:

  • 这是一个有趣的问题,因为即使 /public 中的 php 文件也会以纯文本形式返回,并且 /public 上的位置过滤无法使用,因为如果文件不存在,它仍会被传递给php.

标签: apache nginx url-rewriting configuration-files


【解决方案1】:

这实际上只是做两件事。

  1. 如果该文件存在于root的公共子目录中,则返回。
  2. 否则,发送给php。

所以在 Apache 中我们需要先做测试

RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} -f
RewriteRule (.*) /public/$1 [L]

RewriteCond 对以下规则设置条件,-f 表示文件存在。所以我们知道该文件存在于公共子目录中,所以应用重写请求到公共子目录的规则。 L 标志意味着 Apache 会将此视为最后一个重写规则,而不处理任何其他重写规则。

现在是第二部分。

RewriteRule . "-" [H=application/x-httpd-php]

当然,这假设所有适当的 apache 模块都已安装并处于活动状态,尤其是 mod_rewrite 并且 RewriteEngine on 已被调用。值得一提的是,在 nginx 和 apache 中,必须设置更多选项才能使其正常工作。

更多文档在这里:http://httpd.apache.org/docs/current/mod/mod_rewrite.html

【讨论】:

    【解决方案2】:

    这是你可以使用的东西(我猜需要一些调整):

    Alias /public /xxxxx/public
    
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteCond /xxxxx/public/%{REQUEST_URI} !-f
    RewriteCond /xxxxx/public/%{REQUEST_URI} !-d
    RewriteRule . "-" [H=application/x-httpd-php]
    
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule (.) /public/$1 [L,QSA]
    

    application/x-httpd-php 是 php 处理程序,它可以在您的设置上有所不同(fcgid-script / application/php ...)检查您的 php 设置定义的处理程序。

    【讨论】:

      猜你喜欢
      • 2016-08-18
      • 2017-05-30
      • 2015-01-30
      • 1970-01-01
      • 2018-07-08
      • 2020-12-28
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多