NGINX + Passenger w/ Rails + WordPress permalinks



环境如下:

我有 https://website.com 和博客在 https://website.com/blog

根路径指向乘客托管的Rails应用程序,博客子目录通过php-fpm指向WordPress应用程序。

我的 Nginx 配置一切正常,但是当我尝试将永久链接结构更改为"普通"以外的任何内容时,我从 Rails 应用程序得到一个 404 页面,就好像没有使用位置块一样。我尝试在调试模式下查看错误日志,我确实看到它试图try_files,但最终它在 Rails 404 页面中失败。

值得注意的是,整个网站都在Cloudflare后面。不确定它是否与此有关,尽管我有点怀疑。

这是我使用的几乎可以工作的Nginx配置:

server {
listen 80 default_server;
server_name IP_ADDRESS;
passenger_enabled on;
passenger_app_env production;
passenger_ruby /home/ubuntu/.rbenv/shims/ruby;
root /web/rails/public;
client_max_body_size 20M;
location ^~ /blog {
passenger_enabled off;
alias /web/blog;
index index.php index.htm index.html;
# Tried the commented line below, but then nothing works.
# try_files $uri $uri/ /blog/index.php?$args;
# The line below works, but peramlinks don't.
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
location ~ .php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
# Tried the commented line below, but then nothing works
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# The line below works, but peramlinks don't.
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
我想

简短地发表评论,但我没有足够的声誉。

  • 使用了以下块并为我工作。我添加了一个add_header指令只是为了调试我的请求是否到达正确的块。
location ^~ /blog {
try_files $uri $uri/ /index.php?$args;
add_header reached blog;
location ~ .php$ {
include fastcgi_params;
fastcgi_pass php;
}
}
  • 如果您的服务器落后于 CloudFlare,如果您使用的是 Ubuntu/Mac,则可以尝试在本地计算机上使用/etc/hosts条目。这将停止DNS查找,并且站点将直接从IP地址访问。

  • 检查是否由于任何其他 Nginx 配置而发生任何重定向。

  • 此外,您在问题中提到站点是https://的,而您的服务器块只有listen 80意味着非HTTPS。

  • 使用 检查响应标头

    curl -XGET -IL site-name.tld
    

    这可能有助于您更多地调试情况。

  • alias指令和root指令之间的区别 https://stackoverflow.com/a/10647080/12257950

最新更新