Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx gives a 403 error for CSS/JS files

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"
like image 895
Adil Avatar asked Oct 14 '10 13:10

Adil


People also ask

How do I fix 403 Forbidden nginx?

However, if the specified index files are not in the directory, Nginx will return 403 forbidden error. One way to resolve this issue is to add the index file specified in the configuration file or add the available index file to the config file.

What causes 403 Forbidden nginx?

Causes of 403 Forbidden Often, HTTP 403 forbidden errors are caused by an access misconfiguration on the client-side, which means you can usually resolve the issue yourself. A common cause of these errors is the file or folder permission settings, which control who can read, write, and execute the file or folder.

How do I fix a 403 Forbidden error?

A 403 Forbidden Error occurs when you do not have permission to access a web page or something else on a web server. It's usually a problem with the website itself. However, you can try refreshing the page, clearing your cache and cookies, and disconnecting from any VPN you might be using.

What does fix 403 Forbidden mean?

Clear Your Web History/Cache. Your browser's cache and cookies may also cause a 403 error. Cache stores data to make a website load faster the next time you visit it. However, it's possible that the website's link has been updated, and the actual web page link is now different from the cached version.


2 Answers

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!

like image 74
Michael Butler Avatar answered Oct 06 '22 09:10

Michael Butler


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;
like image 29
Avindra Goolcharan Avatar answered Oct 06 '22 09:10

Avindra Goolcharan