mac系統(tǒng) docker 配置 nginx 反向代理案例

? ? ? ? 使用mac運行docker容器,遇到了很多坑,比如本地?zé)o法訪問容器ip什么的,導(dǎo)致配置nginx案例時,花費了很長時間,最終不斷嘗試,終于配置出了nginx的反向代理案例,有問題,歡迎提問,一起進步~

?1. 實現(xiàn)效果

(1)打開瀏覽器,在瀏覽器地址欄輸入地址 www.123.com,通過nginx反向代理跳轉(zhuǎn)到docker容器tomcat主頁面中

2. 具體實現(xiàn)

(1)拉取nginx鏡像

? ? docker pull nginx

? (2)? ?拉取tomcat鏡像

? ? ?docker pull tomcat

(3)查看本地已經(jīng)安裝的鏡像,如果有我們剛拉取的鏡像:

? ?docker images

(4) 分別運行nginx、tomcat 容器

? ?4.1 nginx 為了方便配置 使用數(shù)據(jù)卷掛載(準備)

? ?4.1.1 創(chuàng)建 Nginx 臨時容器,用于拷貝所需配置文件

? ? docker run --name tmp-nginx -d nginx

? ?4.1.2?拷貝 Nginx 配置文件

docker cp tmp-nginx:/etc/nginx/nginx.conf /Users/yinmb/docker/nginx/nginx.conf

? ?4.1.3 拷貝默認配置文件:

docker cp tmp-nginx:/etc/nginx/conf.d/default.conf /Users/yinmb/docker/nginx/conf.d/default.conf

? ?4.1.4 強制刪除臨時nginx容器

docker rm -f tmp-nginx

? ?4.2 運行Nginx 容器 并映射?Nginx 配置文件、站點配置文件目錄

docker run --name nginx -p 80:80 -v /Users/yinmb/docker/nginx/nginx.conf:/etc/nginx/nginx.conf?-v /Users/yinmb/docker/nginx/conf.d:/etc/nginx/conf.d? -d nginx

? ?4.3 運行tomcat

docker run --name tomcat -p 8080:8080 -d tomcat

? 4.4 docker ps 查看運行的容器

? (5) 配置mac的hosts文件,添加:127.0.0.1? www.123.com?

vim /etc/hosts

(6)配置nginx 文件

vim?/Users/yinmb/docker/nginx/conf.d/default.conf

server {

? ? listen? ? ? 80;

? ? server_name? 172.17.0.3;

? ? #charset koi8-r;

? ? #access_log? /var/log/nginx/host.access.log? main;

? ? location / {

? ? ? ? root? /usr/share/nginx/html;

? ? ? ? proxy_pass http://172.17.0.4:8080;

? ? ? ? index? index.html index.htm;

? ? }

? ? #error_page? 404? ? ? ? ? ? ? /404.html;

? ? # redirect server error pages to the static page /50x.html

? ? #

? ? error_page? 500 502 503 504? /50x.html;

? ? location = /50x.html {

? ? ? ? root? /usr/share/nginx/html;

? ? }

? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80

? ? #

? ? #location ~ \.php$ {

? ? #? ? proxy_pass? http://127.0.0.1;

? ? #}

? ? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

? ? #

? ? #location ~ \.php$ {

? ? #? ? root? ? ? ? ? html;

? ? #? ? fastcgi_pass? 127.0.0.1:9000;

? ? #? ? fastcgi_index? index.php;

? ? #? ? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name;

? ? #? ? include? ? ? ? fastcgi_params;

? ? #}

? ? # deny access to .htaccess files, if Apache's document root

? ? # concurs with nginx's one

? ? #

? ? #location ~ /\.ht {

? ? #? ? deny? all;

? ? #}

}

6.1 查看容器ip 命令(補充)

docker inspect nginx

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容