javascript - nginx反向代理静态资源403错误?
问题描述
部署上线测试的Node项目,使用nginx反向代理时出现静态资源403错误,本地配置正确,线上同样的配置却产生了错误.配置如下:
upstream nodeblog{server 127.0.0.1:3000;keepalive 65;}server {listen 443;ssl on;server_name ;ssl_certificate ;ssl_certificate_key ;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ;ssl_session_timeout 5m;ssl_prefer_server_ciphers on;location / {proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Host $http_host;proxy_set_header X-Nginx-Proxy true;proxy_set_header Connection ’’;proxy_pass http://nodeblog;}location ~ .*.(css|js|jpg|png|gif)$ {alias '/root/nodeApp/public/';expires 3d;}}
按照提示设置了该目录下所有文件777权限,依旧是403错误
问题解答
回答1:找到一个原因,因为是在root权限下操作的,可能是nginx没有该目录的权限.个人服务器因此也没有分配其他用户,所以打开nginx.conf中第一行user nobody修改为user root使得nginx以root权限运行.
这肯定不是好的解决方案,知识大致了解了,403的原因,nginx进程没有当前静态资源文件夹的相关权限,需要单独制定nginx对该目录的权限.希望有好的解决方案
回答2:原因是错误运用了alias指令。官方文档
If alias is used inside a location defined with a regular expressionthen such regular expression should contain captures and alias shouldrefer to these captures (0.7.40)http://nginx.org/r/alias
试下以下配置
location ~ .*.(css|js|jpg|png|gif)$ {alias '/root/nodeApp/public/$1';expires 3d;}
相关文章:
1. mysql 可以从 TCP 连接但是不能从 socket 链接2. sql语句 - 如何在mysql中批量添加用户?3. mysql 非主键做范围查找实现原理的一点困惑4. mysql - JAVA怎么实现一个DAO同时实现查询两个实体类的结果集5. mysql建表索引问题求助6. mysql - PHP定时通知、按时发布怎么做?7. 怎么php怎么通过数组显示sql查询结果呢,查询结果有多条,如图。8. 事务 - mysql共享锁lock in share mode的实际使用场景9. mysql - 数据库建字段,默认值空和empty string有什么区别 11010. node.js - mysql如何通过knex查询今天和七天内的汇总数据
