nginx位置不解释php

时间:2017-12-08 15:50:11

标签: php nginx

我很疯狂地理解这个nginx vhost配置。我的问题是/ v2位置,它不会发送php的东西到php-fpm,而它在/ v2外正常工作。谁能指出我的错误?

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;
   }
 }

根据评论,我正在尝试嵌套的位置解决方案,但是当我尝试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;
   }
 }

2 个答案:

答案 0 :(得分:0)

你必须指向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;
     }   
}

答案 1 :(得分:0)

对于后人,我得到了工作配置:

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;
  }
}
相关问题