Nginx

帶有 Ubuntu 18.04 NGINX 的 VPS 下載 PHP 文件

  • August 21, 2021

我有一個 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 後端執行。您可以改用兩個嵌套位置:

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的文件(請參閱和nginx 指令/var/www/html/public/blog之間的區別)。rootalias

引用自:https://serverfault.com/questions/1075226