Nginx为CSS / JS文件提供403错误

问题描述 投票:10回答:6

我在Ubuntu10.10上设置了nginx 0.7.67以及php-cli。我正在尝试运行基于前端控制器的PHP框架,但除index.php之外的所有页面都会出现403错误。

例如:

  1. http://mysite.com/styles/style.css - 403禁止
  2. http://mysite.com/scripts/script.css - 403禁止
  3. http://mysite.com/index.php - 作品

我的/ etc / nginx / sites-enabled / default如下

server {
    listen          80;
    server_name     mysite.com;

    access_log      /var/log/nginx/access.log;
    error_log       /var/log/nginx/error.log warn;

    index           index.php index.html;
    root        /full/path/to/public_html;

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
            expires max;
    }


    location ~ index.php {
            include     /etc/nginx/fastcgi_params;
            keepalive_timeout 0;
            fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_pass    127.0.0.1:9000;
    }

}

有关如何解决上述问题的任何建议?

PS:这是错误日志中的条目

2010/10/14 19:56:15 [error] 3284#0: *1 open() "/full/path/to/public_html/styles/style.css" 
failed (13: Permission denied), client: 127.0.0.2, server: quickstart.local, 
request: "GET /styles/style.css HTTP/1.1", host: "mysite"
regex nginx http-status-code-403
6个回答
19
投票

有同样的问题。通过简单地在我的CSS和JS文件和文件夹上设置正确的权限来修复它。设置权限时要小心!但是对于可在Web上读取的文件,运行服务器进程的用户必须能够读取该文件。

chmod -R +rx css
chmod -R +rx js

提供读取和执行权限。 -R用于递归。只对想要世界可读的文件执行此操作!


2
投票

Alt解决方案:通过将nginx.conf(例如/usr/local/nginx/conf/nginx.conf)编辑为这些文件的所有者来更改运行用户:

user myuser mygroup;

0
投票

在您的位置线中尝试此操作:

location ~* ^.+\.(js|css|png|jpg|jpeg|gif|ico|html)$ {

要么

location ~* ^.+.(js|css|png|jpg|jpeg|gif|ico|html)$ {

-1
投票

确保每个顶级目录都具有权限级别和index.html。大多数情况下,您只需复制要在/ www中查看的文件并将其重命名为index.html,确保新的index.html具有正确的权限(确保777仅用于测试课程)。


-1
投票

我需要为这个问题编写替代解决方案。我将wordpress迁移到plesk服务器,我用css和js脚本得到这个错误。 Nginx配置产生了获取js和css文件的错误。如果wordpress错误加载CSS和JS脚本(禁止),可能是由于nginx和apache的配置。

解锁配置框nginx和apache:

智能处理静态文件 - >未检查

这解决了我的问题。我希望别人也是pymessoft.com


-3
投票
    server {
        listen          80;
        server_name     mysite.com;

        access_log      /var/log/nginx/access.log;
        error_log       /var/log/nginx/error.log warn;

        index           index.php index.html;
        root        /full/path/to/public_html;

} <--- you forgot this one

        location ~* \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
                expires max;
        }


        location ~ index.php {
                include     /etc/nginx/fastcgi_params;
                keepalive_timeout 0;
                fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_pass    127.0.0.1:9000;
        }

    }

您必须在定义位置之前关闭服务器{}。

或者,如果您无权访问这些文件,请将文件的所有者更改为www-data或apache。

© www.soinside.com 2019 - 2024. All rights reserved.