静态文件由Apache提供,而不是Nginx


Static files served by Apache but not by 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>

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 -files。