2

I got node.js express site on port 3000. And it is configured to work with nginx. It worked well through nginx until I decided to configure nginx to get css, js and image files directly from public folder without node.js express:

/etc/nginx/sites-enabled# cat myDomain.com.public 
server {
    listen 80;
    server_name myDomain.com;
    access_log /var/log/nginx/myDomain.com.access.log;
    location / {
        proxy_pass    http://127.0.0.1:3000/;
    }
    location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
          root /root/pathToNodeJsExpressSiteFolder/public; 
          access_log off;
          expires max;
        }

}

But now this site works without css and js. It returns 403 forbidden requesting them:

style.css
/stylesheets
GET
403
Forbidden

What I did wrong?

2
  • Just a comment, but add modified to expires, so it will look: expires modified max. So browser will check if resources are changed and will reload them in that case, otherwise you will face issues with caching and complexities over clearing the cache in browsers.
    – moka
    Sep 13, 2013 at 12:22
  • @MaksimsMihejevs I changed row expires max; to expires modified max; and it now it throws error on this line: nginx: [emerg] "expires" directive invalid value Sep 14, 2013 at 7:12

2 Answers 2

4
+50

it requires both read and execute privilege to the directory and all parent directories of this directory for those static resources on file system for nginx. If you found some entries in your nginx error log like:

open() "/root/pathToNodeJsExpressSiteFolder/public/stylesheets/style.css" failed (13: Permission denied)

Then you need:

$ sudo chmod +rx /root/pathToNodeJsExpressSiteFolder/public
$ sudo chmod +rx /root/pathToNodeJsExpressSiteFolder
$ sudo chmod +rx /root

Note: $ sudo chmod +rx /root is not recommended because it always limits non-root user to access /root.

0

A 403 forbidden response may be related to the nginx process don't having permission to read the files. Check that the folder public is recursively readable by the user running nginx and change the permissions if needed:

sudo chmod -R 744 /root/pathToNodeJsExpressSiteFolder/public
6
  • I have changed rights to public folder and checked them: ls -ld /root/pathToNodeJsExpressSiteFolder/public drwxr--r-- 5 root root 4096 Sep 6 09:45 /root/pathToNodeJsExpressSiteFolder/public. Restarted nginx. But result is the same 403 forbidden Sep 6, 2013 at 13:42
  • @efr Hum... and the files inside are 744 too?
    – akhanubis
    Sep 6, 2013 at 13:45
  • yep /root/pathToNodeJsExpressSiteFolder/public/stylesheets# ls -ld style.css -rwxr--r-- 1 root root 2463 Sep 6 09:58 style.css Sep 6, 2013 at 14:14
  • Thank you, Pablo. Dont know what to do :) Sep 6, 2013 at 18:02
  • @efr would you mind checking the nginx error log to see what causes the 403 response? and it sees that your files are owned by user root. what is the user that runs the nginx process?
    – shawnzhu
    Sep 14, 2013 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.