【发布时间】:2023-03-08 12:30:01
【问题描述】:
我正在为 PHP 应用程序构建一个 dockerised 测试“平台”——特别是(目前)为 WordPress。我正在使用 PHPFarm 在不同的端口上提供不同版本的 PHP。在前面使用 nginx,我已经完成了很多工作。 (https://github.com/richardtape/testit 是主要仓库)
我现在面临的大问题是让 WordPress 的“漂亮的永久链接”发挥作用。在标准的 nginx 设置中,这只是类似
的情况location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
但是为了能够从主机获得漂亮的 url,并且为了拥有一个代码库,我使用了以下几行:
server {
listen 80;
index index.php index.html index.htm;
server_name 52.spaces.dev;
location / {
proxy_pass http://phpfarm_52;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
root /var/www;
}
upstream phpfarm_52{
server phpfarm:8052;
}
就目前而言,这很有效。 (对于 PHP 5.3、5.4、5.5、5.6 和 7,还有 5 个类似的规则)主页从主机加载到每个不同的 server_names 上(如果您在每个上输出 PHP 版本,您可以看到你得到一个不同的 PHP 版本)。
但是,当我切换到“内部”网址(或任何非 root,即 http://52.spaces.dev/about/)时,我得到了 404。我尝试过类似于
location / {
try_files $uri $uri/ /index.php?$args
}
location ~ \.php$ {
proxy_pass http://phpfarm_52;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
我得到了一个重定向循环,这取决于我尝试过的几种不同方式,它要么只是一系列 301 重定向并且页面永远不会加载,要么是诸如
之类的错误nginx_1 | 2016/04/08 20:31:29 [error] 5#5: *4 rewrite or internal redirection cycle while processing "/index.php", client: 192.168.99.1, server: 52.spaces.dev, request: "GET /favicon.ico HTTP/1.1", host: "52.spaces.dev", referrer: "http://52.spaces.dev/"
我被困住了。我对 nginx 配置也很陌生(这可能很明显),所以我很可能会做一些完全错误和/或愚蠢的事情。有什么建议吗?
【问题讨论】:
-
从实例内部 curl phpfarm:8052/about 会发生什么? (基本上,这就是您最初的 proxy_pass 所做的)。如果您也收到 404,您希望对上游进行什么调用?
-
它本身应该是 404,但据我了解,
try_files $uri $uri/ /index.php?$args意味着它最终应该回退到 index.php(带有适当的 args),而不是 404 (如主页工作的事实所示)。如果我禁用“漂亮”永久链接(即所有内容都通过 index.php 运行),那么它会按预期工作。