[英]configure nginx to get js and css directly from public folder of node.js express site
我在端口3000
上获得了node.js express
网站。 并且它被配置为与Nginx一起使用。 它在nginx上运行良好,直到我决定配置nginx来直接从public
文件夹中获取css,js和图像文件,而无需使用node.js表示:
/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;
}
}
但是现在该站点可以在没有CSS和JS的情况下运行。 它返回403 forbidden
请求它们:
style.css
/stylesheets
GET
403
Forbidden
我做错了什么?
对于nginx的文件系统上的那些静态资源,它需要对该目录以及该目录的所有父目录具有读取和执行特权。 如果您在nginx错误日志中找到了一些条目,例如:
open() "/root/pathToNodeJsExpressSiteFolder/public/stylesheets/style.css" failed (13: Permission denied)
然后,您需要:
$ sudo chmod +rx /root/pathToNodeJsExpressSiteFolder/public
$ sudo chmod +rx /root/pathToNodeJsExpressSiteFolder
$ sudo chmod +rx /root
注意 :不建议使用$ sudo chmod +rx /root
因为它总是限制非root用户访问/root
。
403 forbidden
响应可能与nginx进程无权读取文件有关。 检查文件夹public
是由用户运行递归可读nginx
,如果需要更改权限:
sudo chmod -R 744 /root/pathToNodeJsExpressSiteFolder/public
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.