javascript - nginx反向代理靜態(tài)資源403錯(cuò)誤?
問(wèn)題描述
部署上線測(cè)試的Node項(xiàng)目,使用nginx反向代理時(shí)出現(xiàn)靜態(tài)資源403錯(cuò)誤,本地配置正確,線上同樣的配置卻產(chǎn)生了錯(cuò)誤.配置如下:
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;}}
按照提示設(shè)置了該目錄下所有文件777權(quán)限,依舊是403錯(cuò)誤
問(wèn)題解答
回答1:找到一個(gè)原因,因?yàn)槭窃趓oot權(quán)限下操作的,可能是nginx沒有該目錄的權(quán)限.個(gè)人服務(wù)器因此也沒有分配其他用戶,所以打開nginx.conf中第一行user nobody修改為user root使得nginx以root權(quán)限運(yùn)行.
這肯定不是好的解決方案,知識(shí)大致了解了,403的原因,nginx進(jìn)程沒有當(dāng)前靜態(tài)資源文件夾的相關(guān)權(quán)限,需要單獨(dú)制定nginx對(duì)該目錄的權(quán)限.希望有好的解決方案
回答2:原因是錯(cuò)誤運(yùn)用了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;}
相關(guān)文章:
1. javascript - 使用form進(jìn)行頁(yè)面跳轉(zhuǎn),但是很慢,如何加一個(gè)Loading?2. python 計(jì)算兩個(gè)時(shí)間相差的分鐘數(shù),超過(guò)一天時(shí)計(jì)算不對(duì)3. docker-compose中volumes的問(wèn)題4. javascript - 后臺(tái)管理系統(tǒng)左側(cè)折疊導(dǎo)航欄數(shù)據(jù)較多,怎么樣直接通過(guò)搜索去定位到具體某一個(gè)菜單項(xiàng)位置,并展開當(dāng)前菜單5. docker-machine添加一個(gè)已有的docker主機(jī)問(wèn)題6. angular.js - 輸入郵箱地址之后, 如何使其自動(dòng)在末尾添加分號(hào)?7. javascript - ES6規(guī)范下 repeat 函數(shù)報(bào)錯(cuò) Invalid count value8. javascript - html5的data屬性怎么指定一個(gè)function函數(shù)呢?9. html5 - 為什么使使用vue cli 腳手架,post-css 沒有自動(dòng)對(duì)css3屬性自動(dòng)添加瀏覽器前綴呢?10. javascript - 如何使用nodejs 將.html 文件轉(zhuǎn)化成canvas
