Docker安裝、配置Nginx(學習筆記2020.09.06)
1.0 擷取Nginx
# 搜尋nginx鏡像
[[email protected] /]# docker search nginx
# 下載下傳最新nginx鏡像
[[email protected] /]# docker pull nginx
# 檢視本地鏡像是否下載下傳成功
[[email protected] /]# docker images nginx
1.1 建立(啟動) Nginx
容器
Nginx
[[email protected] /]# docker run -id --name=nginx -p80:80 4bb46517cac3(鏡像ID)
這個時候就可以通路,看看是否正常啟動。
1.2 登入進去 Nginx
容器
Nginx
[[email protected] /]# docker exec -it f5be6c7819a9(容器ID) /bin/bash
退出使用檢視檔案使用指令
exit
ls
[外鍊圖檔轉存失敗,源站可能有防盜鍊機制,建議将圖檔儲存下來直接上傳(img-6lR47vXj-1599537851355)(https://s1.ax1x.com/2020/09/07/wuY28S.jpg)]
可以,可以看到相關的nginx配置檔案都在
cd /etc/nginx
目錄下, 而
/etc/nginx
容器内的預設首頁html檔案目錄為
nginx
,日志檔案位于
/usr/share/nginx/html
這樣對于需要頻繁修改
/var/log/nginx
nginx
配置檔案來說很不友好。
下面使用目錄挂載的方式來啟動容器。
2.0 使用目錄挂載方式啟動 Nginx
容器
Nginx
将nginx容器内部配置檔案挂載到主機,之後就可以在主機對應目錄修改即可。适合頻繁修改,複雜使用的情況。
2.1 建立3個對應檔案夾
在容器外部建立3個對應檔案夾。 我這裡建立在 /usr/local/
[[email protected] local]# mkdir -p ./nginx/{html,logs}
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcsQXYtJ3bm9CXldWYtlWPzNXZj9mcw1ycz9WL49zYHNWc1MUVUh2VZ5EZzwENBpHT1EkeMdXSE1Ue4MlY25UbMRjREVGa1MVT6lTeMZTTINGMShUYvwlbj5yZtlmbkN3YuQnclZnbvN2Ztl2Lc9CX6MHc0RHaiojIsJye.jpg)
2.2 将容器内部配置檔案對應拷貝到建立的檔案夾下面
執行下面指令進行拷貝, docker cp 容器ID:需要拷貝的檔案 拷貝到那裡(./代表目前目錄下)
[[email protected] local]# docker cp f5be6c7819a9:/etc/nginx ./
2.3 停止之前的 Nginx
容器,并進行删除
Nginx
[[email protected] local]# docker stop f5be6c7819a9
[[email protected] local]# docker rm f5be6c7819a9
2.4 啟動目錄挂載的 Nginx
容器
Nginx
運作以下指令進行目錄挂載 -v
(一段一段的複制過去!) (前面是虛拟機檔案路徑: 容器路徑)
[[email protected] local]# docker run -id --name=nginx -p80:80 -v /usr/local/nginx:/etc/nginx
-v /usr/local/nginx/logs:/var/log/nginx -v /usr/local/nginx/html:/usr/share/nginx/html
--privileged=true 4bb46517cac3
2.5 進行通路
第一次通路,發現頁面出現了403, 這是因為我們挂載的
html
檔案夾還是空白的,
我們上傳之前
下學習的
linux
nginx
配置頁面上去在通路,
是否看到歡迎頁面! 看到說明正常啟動!
2.6 進行反向代理
2.6.1 檢視docker容器内部 IP
位址
IP
并找到容器内部
docker0
位址
IP
[[email protected] local]# ifconfig
[外鍊圖檔轉存失敗,源站可能有防盜鍊機制,建議将圖檔儲存下來直接上傳(img-zg9RbxmW-1599537851370)(https://s1.ax1x.com/2020/09/08/wMw6PA.jpg)]
本主控端挂載的配置檔案增加
nginx.config
節點
service
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;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
#域名可以有多個,用空格隔開
server_name zhihao.free.idcfengye.com;
location / {
# 請注意此處是容器内部IP, 我們通路的項目是容器内部項目, 外部就寫外部ip
proxy_pass http://172.17.0.1:8080/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
#後端的Web伺服器可以通過X-Forwarded-For擷取使用者真實IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
配置好後, 重新開機容器
nginx
這裡有個坑自己被坑蠻久的。 之前一直proxy_pass http: 寫的是
docker restart 容器ID
127.0.0.1
IP位址,一直通路不上。
或者直接寫外部公網可以通路項目的
ip位址加端口(http://47.115.80.6:8080/)
3.0 其他Nginx配置看之前的一樣配就可以了!
1