您的位置:首页技术文章
文章详情页

javascript - nginx反向代理静态资源403错误?

【字号: 日期:2023-05-09 13:18:38浏览:109作者:猪猪

问题描述

部署上线测试的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;}

标签: JavaScript
相关文章: