【问题标题】:Debian 9 + PHP7.0-FPM + NGINX 1.10.3-1 path_info issueDebian 9 + PHP7.0-FPM + NGINX 1.10.3-1 path_info 问题
【发布时间】:2017-09-13 17:06:39
【问题描述】:

我正在使用 DigitalOcean Debian 9 + PHP 7.0 + NGINX 1.10.3-1 并尝试安装 Joomla! CMS,但在第一个安装屏幕 (example.com/installation/index.php) 我注意到一个损坏的图像(这是 Joomla 徽标),它看起来像这样:

该图像的 img src 属性包含“/template/images/joomla.png”,但该图像实际上位于“/installation/template/images/joomla.png”,这意味着我缺少“/installation /" 部分。

这是我的 PHP 的 nginx conf 部分:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}

“/etc/php/7.0/fpm/php.ini”处的“cgi.fix_pathinfo”行被取消注释,值改为0。

并且“/sn-ps/fastcgi-php.conf”文件包含以下内容:

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

我注意到,在我评论 PATH_INFO 部分后,图像正在加载:

set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

但我所有进一步尝试找出问题根源的尝试均未成功,请帮助我解决此问题。

【问题讨论】:

    标签: php nginx joomla path fastcgi


    【解决方案1】:

    我们的一位客户遇到了同样的问题,我们在this post 中解释了如何解决它。本质上,您的 try_files 行不正确。

    顺便说一句,cgi.fix_pathinfo 必须设置为 1,而不是零(默认为 0,因此取消注释并不能解决问题。)

    【讨论】:

      【解决方案2】:

      当您定义位置时,nginx 会按出现的顺序处理它们。 匹配文件的第一条规则被执行,其他规则被忽略。 这就是你如何将一些安全性放在首位,然后是静态资产,最后是 php:

      server {
      listen 80;
      server_name  example.com;
      root   /full/path/to/your/joomla/root/directory;
      
      # allow letsencrypt
      location ~ /.well-known/acme-challenge {
      allow all;
      access_log off;
      log_not_found off;
      }
      
      # handle some security and logs
      location = /favicon.ico {
      access_log off;
      log_not_found off;
      }
      
      location = /robots.txt {
      allow all;
      access_log off;
      log_not_found off;
      }
      
      # Deny access to htaccess and other hidden files
      location ~ /\. {
      deny  all;
      access_log off;
      log_not_found off;
      }
      
      # handle static xml files
      location ~* \.(xml)$ {
      access_log off;
      log_not_found off;
      add_header Pragma no-cache;
      add_header Cache-Control "no-cache, no-store, must-revalidate, post-check=0, pre-check=0";
      gzip off;
      }
      
      # include in each host for static content to run with cache and without logs, with CORS enabled (for fonts)
      location ~* \.(jpg|jpeg|gif|png|bmp|css|js|ico|txt|pdf|swf|flv|mp4|mp3|eot|ttf|svg|woff|woff2)$ {
      add_header Access-Control-Allow-Origin *;
      add_header  Cache-Control "public";
      access_log off;
      log_not_found off;
      tcp_nopush on;
      sendfile on;
      expires   15d;
      
      # Enable gzip
      gzip on;
      gzip_comp_level 6;
      gzip_vary on;
      gzip_types text/richtext text/plain text/css text/x-script text/x-component text/x-java-source application/javascript application/x-javascript text/javascript text/js image/x-icon application/x-perl application/x-httpd-cgi text/xml application/xml application/xml+rss application/json multipart/bag multipart/mixed application/xhtml+xml font/ttf font/otf image/svg+xml application/vnd.ms-fontobject application/ttf application/x-ttf application/otf application/x-otf application/truetype application/opentype application/x-opentype application/eot application/font application/font-sfnt;
      }
      
      # html
      location ~* \.(html|htm|shtml)$ {
      max_ranges 0;
      etag off;
      if_modified_since off;
      add_header Last-Modified "";
      gzip              on;
      gzip_buffers      64 128k;
      gzip_comp_level   9;
      gzip_http_version 1.1;
      gzip_min_length   0;
      gzip_types       text/plain;
      gzip_vary         off;
      }
      
      
      # allow wordpress, joomla, etc to work properly
      location / {
      try_files $uri $uri/ /index.php?q=$uri&$args;
      }
      
      # handle php
      location ~ .php$ {
      fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
      try_files $uri $uri/ /index.php?q=$uri&$args;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_index   index.php;
      include fastcgi_params;
      fastcgi_param PATH_INFO         $fastcgi_path_info;
      fastcgi_param PATH_TRANSLATED   $document_root$fastcgi_path_info;
      fastcgi_param QUERY_STRING      $query_string;
      fastcgi_param REQUEST_METHOD    $request_method;
      fastcgi_param CONTENT_TYPE      $content_type;
      fastcgi_param CONTENT_LENGTH    $content_length;
      fastcgi_param SCRIPT_NAME       $fastcgi_script_name;
      fastcgi_param REQUEST_URI       $request_uri;
      fastcgi_param DOCUMENT_URI      $document_uri;
      fastcgi_param DOCUMENT_ROOT     $document_root;
      fastcgi_param SERVER_PROTOCOL   $server_protocol;
      fastcgi_param REQUEST_SCHEME    $scheme;
      fastcgi_param HTTPS             $https if_not_empty;
      fastcgi_param HTTP_PROXY        "";
      fastcgi_param REMOTE_ADDR       $remote_addr;
      fastcgi_param REMOTE_PORT       $remote_port;
      fastcgi_param SERVER_ADDR       $server_addr;
      fastcgi_param HTTP_CF_IPCOUNTRY   $http_cf_ipcountry;
      fastcgi_param SCRIPT_FILENAME $request_filename; 
      }
      
      }
      

      【讨论】: