带有 Ubuntu 18.04 NGINX 的 VPS 下载 PHP 文件

带有 Ubuntu 18.04 NGINX 的 VPS 下载 PHP 文件

我有一个 VPS:

  • Ubuntu 18.04
  • Nginx
  • php7.2-fpm

服务器以 homeurl (example.com) 的形式运行 Ruby on rails 项目,以目录 (example.com/blog) 的形式运行 wordpress。首先,VPS 配置了 Apache2,一切运行正常,直到我必须集成实时聊天。不得不为 Action Cable 切换到 Nginx。

现在,聊天应用程序在 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 个晚上解决问题,但还是没能解决。任何提示都欢迎。谢谢

答案1

fastcgi_pass您有两个正则表达式匹配位置,但第一个位置没有指令。正则表达式匹配位置会从第一个位置到最后一个位置进行检查,因此fastcgi_pass永远不会到达第二个位置(您有指令的位置)。这意味着使用/blog/URI 前缀访问的任何 PHP 文件都将被视为简单文件,不会传递给 PHP-FPM 后端执行。您可以改用两个嵌套位置:

location ~ ^/blog(/.*|$) {
    passenger_enabled off;       
    index index.php;
    try_files $uri $uri/ /blog/index.php?$args;
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}

您不需要root /var/www/html/public/blog;为此位置定义。您已经root /var/www/html/public;在上层定义了,因此 nginx 将在目录中搜索index.php请求的文件(请参阅/blog/index.php/var/www/html/public/blogrootaliasnginx 指令)。

相关内容