温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用Docker搭建一个支持https的nginx代理服务

发布时间:2021-06-26 14:46:21 来源:亿速云 阅读:155 作者:chen 栏目:大数据

本篇内容主要讲解“怎么用Docker搭建一个支持https的nginx代理服务”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Docker搭建一个支持https的nginx代理服务”吧!

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

nginx不仅仅是一个高性能的web服务器软件,还可以用来做正向代理和反向代理,但是nginx不支持https的正向代理,作者搜索已有的解决方案,并把最终服务集成到Docker,后续直接通过docker run就能使用了

首先说下nginx实现https正向代理,这个用的是别人开发好的ngx_http_proxy_connect_module模块,详细资料可以参考这篇文章,本文的重点是记录怎么集成到Docker里面

首先准备好工作目录

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

下载指定的nginx版本,对应的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

# 基础镜像,这个用的centos7比较大,一般使用alpine
FROM centos:7
# 安装基础依赖工具
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 
#拷贝当前workdir目录到镜像中的/workdir
COPY ./workdir /workdir
#切换当前目录为/workdir
WORKDIR /workdir
#安装nginx服务(把对应的ngx_http_proxy_connect_module加入)
#安装完了之后把对应目录软件包删掉
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服务,注意要加后面的-g daemon off,表示关闭守护进程模式
CMD ["nginx", "-g", "daemon off;"]

以上就是Dockerfile的全部内容,当前工作目录结构

.

├── Dockerfile

└── workdir

    ├── nginx-1.17.4.tar.gz

    └── nginx_proxy

下面开始build镜像,-t表示取的镜像名字,后面那个.不能漏了,表示当前目录,整个过程需要一定时间,视机器的网络情况

docker build -t nginx:proxy_1.17.4 .

build成功标识:

Successfully built 5e54788aa240

Successfully tagged nginx:proxy_1.17.4

如果失败的话会有对应提示,按照提示解决即可。

现在image生成了,docker image ls看看是不是已经有nginx:proxy_1.17.4了

接下来可以运行了,当然要准备好对应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;
        }   
    }

运行镜像(对应路径和端口可以自己设定)

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完毕,后续换一个机器,直接把镜像拷贝一下,然后docker run就可以了,方便很多

以上服务已经发布到GitHub,clone下来后可以直接运行,当然,自己的机器要已经安装docker

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

到此,相信大家对“怎么用Docker搭建一个支持https的nginx代理服务”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI