为什么我通过nginx / php-fpm获取静态文件的403 Forbidden状态代码?

时间:2016-08-30 04:11:05

标签: php nginx

我有一个像这样的文件夹结构:

/usr/share/nginx/idevaffiliate/install/templates/bootstrap/css/bootstrap.css

尝试通过网络服务器访问此文件和其他静态文件时,我收到了403条回复:

[30/Aug/2016:04:56:33 +0100] "GET /idevaffiliate/install/templates/bootstrap/css/bootstrap.css HTTP/1.1" 403 46 "http://example.com/idevaffiliate/install/install.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"

这是我的nginx位置指令:

location ~ ^/idevaffiliate/(images|javascript|js|css|flash|media|static)/  {
 rewrite        ^ https://$server_name$request_uri? permanent;
 root /usr/share/nginx/;
 expires 30d;
}

location ^~ /idevaffiliate {
    root /usr/share/nginx/;
    index index.html index.htm index.php;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include /etc/nginx/fastcgi.conf;
}

所有文件和子目录的文件夹权限:

[root@BTCUKSW001 nginx]# ls -la
total 20
drwxr-xr-x 12 nginx nginx 4096 Feb  3  2014 idevaffiliate

摘自/etc/php-fpm.d/www.conf:

[www]
listen = /var/run/php5-fpm.sock
listen.allowed_clients = 127.0.0.1
listen.owner = nginx
listen.group = nginx
listen.mode = 0666
user = nginx
group = nginx

我不知道为什么我有这个“访问被拒绝”的问题。谁能告诉我为什么以及如何解决?

1 个答案:

答案 0 :(得分:3)

您在问题中显示两个位置区块。第一个看起来像一个重定向循环,但永远不会输入,因为第二个位置块使用^~修饰符覆盖它。有关位置块处理,请参阅this document

您收到静态文件的403响应的原因是因为您要求php5-fpm为其提供服务。 fastcgi_pass块中的location ^~ /idevaffiliate指令会将所有请求发送到php5-fpm,只需要查看PHP脚本。

有许多模式可用,但常见的解决方案是使用嵌套位置仅提供PHP文件:

location ^~ /idevaffiliate {
    root /usr/share/nginx;
    index index.html index.htm index.php;

    ...

    location ~ \.php$ {
        try_files $uri =404;

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include /etc/nginx/fastcgi.conf;
    }
}
相关问题