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 查詢身份證號字段值有效的數據2. python bottle跑起來以后,定時執行的任務為什么每次都重復(多)執行一次?3. 視頻文件不能播放,怎么辦?4. html5 - HTML代碼中的文字亂碼是怎么回事?5. python - 爬蟲模擬登錄后,爬取csdn后臺文章列表遇到的問題6. visual-studio - Python OpenCV: 奇怪的自動補全問題7. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處8. javascript - 彈出一個子窗口,操作之后關閉,主窗口會得到相應的響應,例如網站的某些登錄界面,django后臺的管理等,這是怎么實現的呢?9. javascript - ios返回不執行js怎么解決?10. android - 分享到微信,如何快速轉換成字節數組
