由Apache提供但不由Nginx提供的静态文件

时间:2016-11-02 10:07:57

标签: php apache nginx

我的PHP应用程序静态文件在运行Apache时可以正常运行但在运行Nginx时被拒绝访问,尽管两个http服务器都使用我自己的用户(我在Linux机器上登录的用户)作为用户。

因此问题在于Nginx或php-fpm配置。

以下是一些nginx.conf内容:

user  stephane;
worker_processes  1;
http {
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  gzip  on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  upstream php5-fpm-sock {
    server unix:/home/stephane/programs/install/php5-fpm.sock;
  }
  server {
    listen       80;
    server_name  localhost;
    charset utf-8;
    location / {
      root   html;
      index  index.html index.htm;
    }
    location ~ \.php$ {
      try_files $uri = 404;
      fastcgi_index index.php;
      fastcgi_pass php5-fpm-sock;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
    }
    location ~ /\. {
      access_log off;
      log_not_found off;
      deny all;
    }
  }
  include conf.d/*.conf;
  include sites-enabled/*;
}

以下是Nginx虚拟主机配置:

server {
  listen 443;
  server_name dev.extrapack.group.com;
  root /home/stephane/dev/php/projects/Extrapack-Mon/public;

  ssl on;
  ssl_certificate /home/stephane/programs/install/nginx/conf/sites-available/extrapack.group.com.crt;
  ssl_certificate_key /home/stephane/programs/install/nginx/conf/sites-available/extrapack.group.com.key;

  location /simplesaml {
    index index.php;
    alias /usr/share/simplesaml/www;
    location ~ ^/simplesaml/(module\.php)(/.+)$ {
      include fastcgi_params;
      fastcgi_pass php5-fpm-sock;
      fastcgi_split_path_info ^/simplesaml/(module\.php)(/.+)$;
      fastcgi_param SCRIPT_FILENAME /usr/share/simplesaml/www/$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_path_info;
      fastcgi_index index.php;
    }
    location ~ \.php$ {
      include fastcgi_params;
      fastcgi_pass php5-fpm-sock;
    }
  }

  location / {
    include fastcgi_params;
    fastcgi_pass php5-fpm-sock;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    try_files $uri /index.php?$args;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param HTTPS 'on'; # Make PHP-FPM aware that this vhost is HTTPs enabled
    fastcgi_param APPLICATION_ENV development;
    fastcgi_index index.php;
  }
}

Apache虚拟主机配置(可以正常访问所有静态资源):

<VirtualHost *:443>
ServerName dev.extrapack.group.com
DocumentRoot "/home/stephane/dev/php/projects/Extrapack-Mon/public"
<Directory "/home/stephane/dev/php/projects/Extrapack-Mon/public">
Options Indexes FollowSymLinks Includes
AllowOverride All           
Require all granted                 
</Directory>                            
</VirtualHost>

1 个答案:

答案 0 :(得分:1)

location /替换为以下两个位置:

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

location ~ [^/]\.php(/|$) {
  fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  if (!-f $document_root$fastcgi_script_name) {
    return 404;
  }
  include fastcgi_params;
  fastcgi_param PATH_INFO $fastcgi_path_info;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_pass php5-fpm-sock;
  fastcgi_param HTTPS 'on'; # Make PHP-FPM aware that this vhost is HTTPs enabled
  fastcgi_param APPLICATION_ENV development;
  fastcgi_index index.php;
}

第一个位置处理静态文件。

第二个位置处理.php个文件。由于它是一个regexp-location(~),如果它匹配,它优先于第一个位置,所以.php - 文件被执行。