我有一个 VPS:
- Ubuntu 18.04
- Nginx
- php7.2-fpm
服务器将 Ruby on rails 项目作为 homeurl (example.com) 和 wordpress 作为目录 (example.com/blog) 运行。首先,VPS 配置了 Apache2,一切正常,直到我不得不集成实时聊天。不得不切换到 Nginx 的 Action Cable。
现在,聊天应用程序在 RoR 上运行良好,但如果我尝试访问博客,我会下载 index.php 文件,但它不会执行。
这是我的 nginx 默认配置:
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default_server ipv6only=on; ## listen for ipv6
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/asdfsf.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/sdafasdf.net/privkey.pem;
root /var/www/html/public;
passenger_enabled on;
passenger_ruby /usr/local/bin/ruby;
location /var/www/html/public {
try_files $uri $uri/ =404;
}
location ~ ^/blog(/.*|$) {
root /var/www/html/public/blog;
try_files $uri $uri/ /blog/index.php?$args;
passenger_enabled off;
index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
在故障排除上花了 3 个晚上,我无法正确解决问题。任何提示都非常受欢迎。谢谢
您有两个正则表达式匹配位置,而第一个没有
fastcgi_pass
指令。fastcgi_pass
正则表达式匹配位置从第一个到最后一个进行检查,因此永远不会到达第二个位置(您确实有指令的位置)。这意味着使用/blog/
URI 前缀访问的任何 PHP 文件都将被视为一个简单文件,并且不会传递到 PHP-FPM 后端执行。您可以改用两个嵌套位置:您无需
root /var/www/html/public/blog;
为此位置定义。您已经root /var/www/html/public;
在上层拥有,因此 nginx 将搜索目录中index.php
请求/blog/index.php
的文件(请参阅和nginx 指令/var/www/html/public/blog
之间的区别)。root
alias