Nginx的CSS/JS文件给出403错误

问题描述:

我已经在Ubuntu10.10上安装了nginx 0.7.67以及php-cli。我试图让我的基于前端控制器的PHP框架运行,但除index.php之外的所有页面都会出现403错误。Nginx的CSS/JS文件给出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的/ /默认是启用的站点 - 如下

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" 
+2

该死的。这是Linux权限问题。其中一个顶级目录对其他人没有“r”权限。问题已解决。 – Adil 2010-10-17 11:03:43

有同样的问题。通过简单地在我的CSS和JS文件和文件夹上设置正确的权限来修复它。请注意设置权限!但是,要使文件在Web上可读,必须由运行服务器进程的用户读取。

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

授予读取和执行权限。 -R是递归的。只对你想要的世界可读的文件做这件事!

+0

我解决这个问题已经3年了!虽然答案很好。 – Adil 2013-12-13 09:58:43

+0

不应该是'chmod -R + rx css'来代替吗? – 2014-07-24 11:33:51

+0

@MichaelButler谢谢。尽管这在Mac OS X中不被接受。 – 2014-07-27 08:14:36

在您所在地线试试这个:

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

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

它仍然给出了同样的错误,两个选项 – Adil 2010-10-14 15:56:32

+0

错误日志说2010/10/14 19:56:15 [error] 3284#0:* 1 open()“/ home/adil/NetBeansProjects-old/CrowdSourcedWeb/applications /csw/web/styles/style.css“失败(13:权限被拒绝),客户端:127.0.0.2,服务器:quickstart.local,请求:”GET /styles/style.css HTTP/1.1“,主机:”quickstart .local“ – Adil 2010-10-14 15:57:53

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。

+1

位置标记必须位于服务器标记内。 – Adil 2010-10-16 17:18:24

+0

这很奇怪,在我的情况下,它可以在定义位置之前关闭服务器{}。但是我很高兴你找到了解决方案! – Maarten 2010-10-18 19:26:00

+0

Maarten。只是为了解释我上面的评论,我之所以这么说是因为nginx抱怨。它的“非法”将位置标签保留在服务器标签之外。也许你应该再次看看你的conf,看看它为什么这样做。很高兴知道。并且,感谢您的帮助。 – Adil 2010-10-20 11:58:28

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

+2

777“可以肯定”是一个可怕的想法。 “我有我的钥匙,但我应该离开我的前门解锁,以确保我可以重新进入。” – Dan 2012-09-27 04:28:13

Alt键溶液:

user myuser mygroup; 

我需要:通过编辑nginx.conf(例如,/usr/local/nginx/conf/nginx.conf)对这些文件的所有者改变运行用户写出这个问题的替代解决方案。 我将wordpress迁移到plesk服务器,并使用css和js脚本得到此错误。 Nginx配置产生一个获取js和css文件的错误。 如果加载CSS和JS脚本的wordpress错误(禁止), 它可能是由于nginx和apache的配置。

UNLOCK配置nginx的和Apache的盒子:静态文件

智能处理 - > DONT CHECKED

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