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

发布于 2024-09-27 03:33:02 字数 1454 浏览 6 评论 0原文

我在 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"

I have setup nginx 0.7.67 on Ubuntu10.10 along with php-cli . I'm trying to get my front-controller based PHP framework to run, but all pages except index.php give a 403 error.

Ex :

  1. http://mysite.com/styles/style.css - 403 Forbidden
  2. http://mysite.com/scripts/script.css - 403 Forbidden
  3. http://mysite.com/index.php - Works

My /etc/nginx/sites-enabled/default is as follows

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

}

Any suggestions on how to fix the above?

PS: This is the entry from the error log

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不语却知心 2024-10-04 03:33:02

有同样的问题。只需在我的 CSS 和 JS 文件和文件夹上设置正确的权限即可修复此问题。设置权限要小心!但要使文件在网络上可读,运行服务器进程的用户必须能够读取该文件。

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

授予读取和执行权限。 -R 用于递归。仅对您希望全世界都可读的文件执行此操作!

Had the same problem. Fixed it by simply setting the right permissions on my CSS and JS files and folders. Be careful about setting permissions! But for a file to be readable on the web, it has to be readable by the user running the server process.

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

Gives read and execute permissions. The -R is for recursive. Only do this for files you want readable by the world!

赴月观长安 2024-10-04 03:33:02

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

user myuser mygroup;

Alt solution: change the run user by editing the nginx.conf (e.g., /usr/local/nginx/conf/nginx.conf) to the owner of those files:

user myuser mygroup;
望笑 2024-10-04 03:33:02

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

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

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

Try this in your location line:

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

or

location ~* ^.+.(js|css|png|jpg|jpeg|gif|ico|html)$ {
遗失的美好 2024-10-04 03:33:02

我也遇到了这个问题

遇到的原因是我把项目放在了 /root 下

root 的权限是

drwx-------- 14 root root

所以我就把项目移到了 /home 下。

I also encountered this problem

The reason I encountered was that I placed the project in /root

The permissions of root are

drwx------ 14 root root

So I just moved the project to /home.

删除→记忆 2024-10-04 03:33:02

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

Make sure that every top level directory has permission levels AND the index.html. Most of the times you will just copy the file you want to see in /www and rename it index.html make sure the new index.html has the right permissions (to be sure 777 just for testing ofcourse).

相对绾红妆 2024-10-04 03:33:02

我需要写这个问题的替代解决方案。
我将 wordpress 迁移到 plesk 服务器,并在 css 和 js 脚本中收到此错误。
Nginx 配置在获取 js 和 css 文件时产生错误。
如果 WordPress 加载 CSS 和 JS 脚本时出错(禁止),
可能是nginx和apache的配置问题。

解锁配置nginx和apache的盒子:

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

这解决了我的问题。我希望其他人也一样
pymessoft.com

i need to write alterative solution to this problem.
I migrate wordpress into plesk server and I get this error with css and js scripts.
Nginx configuration produce a bug getting js and css files.
If a wordpress error loading CSS and JS scripts (forbidden),
it may be due to the configuration of nginx and apache.

UNLOCK the box of configuration nginx and apache:

Intelligent processing of static files -> DONT CHECKED

This solves my problem. I hope others too
pymessoft.com

你与昨日 2024-10-04 03:33:02
    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。

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

    }

you have to close server{} before defining the location.

or change the owner of the files to www-data or apache if you don't have access to the files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文