由Apache提供但不由Nginx提供的静态文件(Static files served by Apache but not by Nginx)

编程入门 行业动态 更新时间:2024-10-27 22:21:25
由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>

My PHP application static files are served all right when running Apache but they are denied access when running Nginx although both http servers use my own user (the one I log in my Linux machine with) as their user.

The issue therefore lies within the Nginx or php-fpm configuration.

Here is some of the nginx.conf content:

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

Here is the Nginx virtual host configuration:

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

And the Apache virtual host configuration (that works fine accessing all the static resources):

<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 / { 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被执行。

Replace your location / with these two locations:

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

The first location handles static files.

The second location handles .php files. Since it is a regexp-location (with ~) it has precedence over the first location if it matches, so .php-files get executed.

更多推荐

本文发布于:2023-08-07 14:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464919.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   文件   Nginx   Apache   files

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!