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. 如何解决docker宿主机无法访问容器中的服务?2. html - element ui 对话框嵌套3. 利用百度地图API定位及附件商家信息服务4. javascript - 在页面最顶部插个广告,怎么做才能不破坏页面布局(不影响下面的内容)?5. javascript - 最近用echarts做统计图时遇到两个问题!!6. css3 - less或者scss 颜色计算的知识应该怎么学?或者在哪里学?7. git - webstorm窗口中左侧列表的文件名颜色怎么修改8. node.js - 微信的自动回复问题9. mysqld无法关闭10. android - 优酷的安卓及苹果app还在使用flash技术吗?

网公网安备