您的位置:首页 > 运维架构 > Docker

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

2019-09-28 16:38 4235 查看

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


说明:本文所提的服务只是作者平常测试使用,可能含有未知bug或不成熟的解决方案,仅供参考,请不要用于正式环境,当然,使用过程中有任何问题欢迎提给我,我可以不断改进

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 


欢迎指正问题!


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx Docker 正向代理