溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用Docker搭建一個支持https的nginx代理服務(wù)

發(fā)布時間:2021-06-26 14:46:21 來源:億速云 閱讀:172 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“怎么用Docker搭建一個支持https的nginx代理服務(wù)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“怎么用Docker搭建一個支持https的nginx代理服務(wù)”吧!

GitHub地址: https://github.com/wll-zhou/nginx_proxy_docker   

nginx不僅僅是一個高性能的web服務(wù)器軟件,還可以用來做正向代理和反向代理,但是nginx不支持https的正向代理,作者搜索已有的解決方案,并把最終服務(wù)集成到Docker,后續(xù)直接通過docker run就能使用了

首先說下nginx實現(xiàn)https正向代理,這個用的是別人開發(fā)好的ngx_http_proxy_connect_module模塊,詳細資料可以參考這篇文章,本文的重點是記錄怎么集成到Docker里面

首先準備好工作目錄

mkdir -p nginx/workdir && cd nginx/workdir

下載指定的nginx版本,對應(yīng)的ngx_http_proxy_connect_module模塊

wget http://nginx.org/download/nginx-1.17.4.tar.gz
git clone https://github.com/chobits/ngx_http_proxy_connect_module.git nginx_proxy

返回上一層nginx目錄,開始編寫Dockerfile

# 基礎(chǔ)鏡像,這個用的centos7比較大,一般使用alpine
FROM centos:7
# 安裝基礎(chǔ)依賴工具
RUN yum install -y patch gcc glibc-devel make openssl-devel pcre-devel zlib-devel gd-devel geoip-devel perl-devel
#添加nginx用戶組和用戶,用來啟動nginx的用戶,看自己情況,也有用www啟動的
RUN groupadd -g 101 nginx \
          && adduser  -u 101 -d /var/cache/nginx -s /sbin/nologin  -g nginx nginx 
#拷貝當(dāng)前workdir目錄到鏡像中的/workdir
COPY ./workdir /workdir
#切換當(dāng)前目錄為/workdir
WORKDIR /workdir
#安裝nginx服務(wù)(把對應(yīng)的ngx_http_proxy_connect_module加入)
#安裝完了之后把對應(yīng)目錄軟件包刪掉
RUN tar -zxvf nginx-1.17.4.tar.gz && cd nginx-1.17.4 \
       && patch -p1 < /workdir/nginx_proxy/patch/proxy_connect_rewrite_101504.patch \
      && ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.17.1/debian/debuild-base/nginx-1.17.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=/workdir/nginx_proxy \
     && make && make install \
     && cd /workdir && rm -rf /workdir/*
#啟動nginx服務(wù),注意要加后面的-g daemon off,表示關(guān)閉守護進程模式
CMD ["nginx", "-g", "daemon off;"]

以上就是Dockerfile的全部內(nèi)容,當(dāng)前工作目錄結(jié)構(gòu)

.

├── Dockerfile

└── workdir

    ├── nginx-1.17.4.tar.gz

    └── nginx_proxy

下面開始build鏡像,-t表示取的鏡像名字,后面那個.不能漏了,表示當(dāng)前目錄,整個過程需要一定時間,視機器的網(wǎng)絡(luò)情況

docker build -t nginx:proxy_1.17.4 .

build成功標識:

Successfully built 5e54788aa240

Successfully tagged nginx:proxy_1.17.4

如果失敗的話會有對應(yīng)提示,按照提示解決即可。

現(xiàn)在image生成了,docker image ls看看是不是已經(jīng)有nginx:proxy_1.17.4了

接下來可以運行了,當(dāng)然要準備好對應(yīng)nginx配置文見,把代理的配置加上

    server {
        listen                         8888;
        access_log /var/log/nginx/proxy.log; 
        # dns resolver used by forward proxying
        resolver                       8.8.8.8;
        # forward proxy for CONNECT request
        proxy_connect;
        proxy_connect_allow            443 563;
        proxy_connect_connect_timeout  10s;
        proxy_connect_read_timeout     10s;
        proxy_connect_send_timeout     10s;
        # forward proxy for non-CONNECT request
        location / { 
            proxy_pass http://$host;
            proxy_set_header Host $host;
        }   
    }

運行鏡像(對應(yīng)路徑和端口可以自己設(shè)定)

docker run -d -p 8888:8888 -v /home/www/image/nginx/nginx.conf:/etc/nginx/nginx.conf nginx:proxy_1.17.4

啟動之后測試下代理是否可用

curl https://www.geek-share.com -v -x 127.0.0.1:8888

至此,集成到docker完畢,后續(xù)換一個機器,直接把鏡像拷貝一下,然后docker run就可以了,方便很多

以上服務(wù)已經(jīng)發(fā)布到GitHub,clone下來后可以直接運行,當(dāng)然,自己的機器要已經(jīng)安裝docker

https://github.com/wll-zhou/nginx_proxy_docker 

到此,相信大家對“怎么用Docker搭建一個支持https的nginx代理服務(wù)”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI