【问题标题】:nginx location does not interpret phpnginx位置不解释php
【发布时间】:2018-03-20 23:08:29
【问题描述】:

我要疯了才能理解这个 nginx vhost 配置。我的问题是 /v2 位置,当它在 /v2 之外正常工作时,它不会将 php 内容发送到 php-fpm。谁能指出我的错误?

server {
  listen 443 ssl;
  include ssl.conf;
   include hardening.conf;
   server_name myapp.domain.com myapp;
   ssl_certificate /etc/pki/tls/certs/myapp.domain.com.crt;
   ssl_certificate_key /etc/pki/tls/private/myapp.domain.com.key;
   access_log /var/log/nginx/myapp.domain.com-access.log main;
   error_log /var/log/nginx/myapp.domain.com-error.log notice;
   root /var/www/html/myapp.domain.com;

   location ~ /\.ht {
     deny  all;
   }

   location ~ /v2 {
     alias /var/www/html/myapp.domain.com/version-2/web;
     try_files $uri index.php$is_args$args;
   }

   location ~ [^/]\.php(/|$) {
     fastcgi_split_path_info ^(.+?\.php)(/.*)$;
     if (!-f $document_root$fastcgi_script_name) {
       return 404;
     }

     # Mitigate https://httpoxy.org/ vulnerabilities
     fastcgi_param HTTP_PROXY "";

     fastcgi_pass localhost:9000;
     fastcgi_index index.php;
     include fastcgi_params.conf;
   }
 }

根据 cmets,我正在尝试嵌套位置解决方案,但是当我尝试 https://myapp.domain.com/v2/index.php 而文件系统上存在 /var/www/html/myapp.domain.com/version-2/web/index.php 时,我现在收到 404。同样正如给出的链接所解释的,我将我的位置从^ 修改为^~。知道有什么问题吗?

server {
  listen 443 ssl;
  include ssl.conf;
   include hardening.conf;
   server_name myapp.domain.com myapp;
   ssl_certificate /etc/pki/tls/certs/myapp.domain.com.crt;
   ssl_certificate_key /etc/pki/tls/private/myapp.domain.com.key;
   access_log /var/log/nginx/myapp.domain.com-access.log main;
   error_log /var/log/nginx/myapp.domain.com-error.log notice;
   root /var/www/html/myapp.domain.com;

   location ~ /\.ht {
     deny  all;
   }

   location ^~ /v2 {
     alias /var/www/html/myapp.domain.com/version-2/web;
     try_files $uri index.php$is_args$args;

     location ~ [^/]\.php(/|$) {
       fastcgi_split_path_info ^(.+?\.php)(/.*)$;
       if (!-f $document_root$fastcgi_script_name) {
         return 404;
       }

       # Mitigate https://httpoxy.org/ vulnerabilities
       fastcgi_param HTTP_PROXY "";

       fastcgi_pass localhost:9000;
       fastcgi_index index.php;
       include fastcgi_params.conf;
     }

   }

   location ~ [^/]\.php(/|$) {
     fastcgi_split_path_info ^(.+?\.php)(/.*)$;
     if (!-f $document_root$fastcgi_script_name) {
       return 404;
     }

     # Mitigate https://httpoxy.org/ vulnerabilities
     fastcgi_param HTTP_PROXY "";

     fastcgi_pass localhost:9000;
     fastcgi_index index.php;
     include fastcgi_params.conf;
   }
 }

【问题讨论】:

  • 您有两个具有不同根目录的 PHP 应用程序。您将需要两个不同的 location 块来处理 .php URI。考虑使用嵌套的 location 块 - 例如 this answer
  • @RichardSmith 我用你的建议更新了帖子,但现在我得到了一些 404,如帖子中所述,你能再看看吗?
  • $document_root$fastcgi_script_name 不适用于alias,请改用:$request_filename。此外,您需要为SCRIPT_FILENAME 设置一个值。另外,由于this issue,我会避免同时使用try_filesalias
  • 确实我没有看到那个细节,用$request_filename 替换$document_root$fastcgi_script_name 并用重写规则替换try_files 也重新排列了php 位置中的一些东西使其工作。
  • @RichardSmith 非常感谢!我需要发布工作版本吗?

标签: php nginx


【解决方案1】:

您必须指向 php5-fpm 位置。像这样:

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

看一个完整的例子:

server {
    listen 8082;
    listen [::]:8082;
    server_name 192.168.2.60;

    root /usr/share/nginx/html/phpmyadmin/;
    index index.php index.html index.htm;


    location / {
            try_files $uri $uri/ /index.php?uri=$uri;
    }

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

     location ~ /\.ht {
            deny all;
     }   
}

【讨论】:

    【解决方案2】:

    为了后代,我得到了工作配置:

    server {
      listen 443 ssl;
      include ssl.conf;
      include hardening.conf;
      server_name myapp.domain.com myapp;
      ssl_certificate /etc/pki/tls/certs/myapp.domain.com.crt;
      ssl_certificate_key /etc/pki/tls/private/myapp.domain.com.key;
      access_log /var/log/nginx/myapp.domain.com-access.log main;
      error_log /var/log/nginx/myapp.domain.com-error.log notice;
      root /var/www/html/myapp.domain.com;
    
      location ^~ /v2/admin/web/index[_dev]*.php/command {
        if (!-f $request_filename) {
          rewrite ^ /v2/admin/web/index.php$is_args$args last;
        }
      }
    
      location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
          return 404;
        }
        include fastcgi_params.conf;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 2017-06-06
      • 1970-01-01
      • 2020-11-07
      相关资源
      最近更新 更多