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. mac连接阿里云docker集群,已经卡了2天了,求问?2. selenium-基于python2-126邮箱登陆问题3. javascript - vue组件通过eventBus通信时,报错a.$on is not a function4. boot2docker无法启动5. css3 - 背景图自定义比例缩小6. javascript - axios设置自定义请求头问题7. javascript - 请教,用原生js插入html的问题8. css - 为何flex的align-items: center无法垂直居中?9. css - chrome下a标签嵌套img 显示会多个小箭头?10. javascript - vue webpack打包问题

网公网安备