引言
在CentOS 7
系統上使用docker
安裝nginx
,使用nginx
部署一個由Vue
開發、打包的項目
docker安裝nginx
這里不多贅述,直接上docker-compose.yml
代碼
nginx:container_name: nginximage: nginx:1.27.2ports:- "80:80"volumes:- /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf- /docker/nginx/conf/conf.d:/etc/nginx/conf.d- /docker/nginx/html/dist:/etc/nginx/html- /docker/nginx/logs:/var/log/nginxnetworks:- myNetwork
需要注意的是容器卷里的/docker/nginx/html/dist
,到時候部署前端項目時把dist
文件夾直接放到/nginx/html
文件夾里就行
nginx部署
前端打包的dist
文件夾直接放到/nginx/html
目錄里
然后看nginx.conf
的代碼
# 此處注意用戶不能是別的, 否則會報403錯誤
user root;
worker_processes auto;error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;events {worker_connections 1024;
}http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;server {listen 80;listen [::]:80;server_name localhost;location / {#此處路徑對應nginx容器內的/etc/nginx/html,所以需要將容器卷dist掛載到容器內的htmlroot html;index index.html index.htm;}}
}
常見問題
404: 可能是容器內的項目文件夾html
為空, 可能是容器卷掛載錯誤
403: 禁止訪問index.html
,兩種情況:1.可能是容器內的項目文件夾html
不存在index.html
,可能是容器卷掛載錯誤;2.可能是權限問題,需要將nginx.conf開頭的用戶改為root: user root
500:需要自行排查
查看日志的兩種方式
docker logs nginx
cat /nginx/logs/error.log