1. 程式人生 > 其它 >nginx伺服器配置問題心得

nginx伺服器配置問題心得

技術標籤:伺服器篇nginxlinux

事情經過

今天把新的Vue工程部署到nginx伺服器上的時候,莫名其妙的網站就打不開了,報500系列錯誤。nginx伺服器error.log日誌提示各種錯誤(之前是沒有這個問題的)
一開始報錯favicon.ico訪問不到,發現是專案許可權問題chmod一下就好了。但還是有錯誤…

出現的各種錯誤

1、rewrite or internal redirection cycle 系列錯誤。在這裡插入圖片描述

解決辦法

如圖,將該行的last改為break

2、"/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)問題

在關閉nginx伺服器後,執行重新載入nginx配置命令的時候,報錯沒有nginx.pid檔案

: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

解決辦法:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

原理:

使用nginx -c的引數指定nginx.conf檔案的位置

3、懷疑是工程許可權問題

一開始檢視專案的檔案許可權的時候,確實是許可權不夠的原因
解決辦法

chmod -r 777 dist/
# 更改dist/資料夾的許可權

4、"/root/VueProject/dist/index.html" failed (13: Permission denied)系列問題

4.1、路徑配置問題

在這裡插入圖片描述
如上圖,因為我把專案放在了root使用者裡面,所以user引數改成使用者名稱(我的是root)
在這裡插入圖片描述
我們通常把Vue專案放在上圖41行的目錄(var/)下,但是我放在了root使用者的VueProject目錄下,所以要進行修改,如圖43行(之前用42行的路徑也是行的,但是這次更新了Vue專案包後就不行了)。

總結

總結一下nginx.conf配置檔案要修改的地方吧

#user  nobody;
user  root;
# 如果專案不在根目錄(在使用者目錄下),要指定使用者名稱 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8081; # 你的服務的埠號 server_name localhost; # server_name 39.102.63.221; #root /var/www/VueProject/dist; root /root/VueProject/dist; # 路徑,必改。 location / { try_files $uri $uri/ @router; #root html; index index.html index.htm; } location @router { rewrite ^.*$ /index.html break; # break引數不報錯不改 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }