在Docker上部署自动更新ssl证书的nginx + .NET Core

突发奇想要搞一个ssl的服务器,然后我就打起了docker的主意,想着能不能搞一个基于Docker的服务器,这样维护起来也方便一点。

设想#
想法是满足这么几点:

.NET Core on Docker
Let’s Encypt on Docker
nginx on Docker用于反向代理
Let’s Encypt证书有效期很短,需要能够自动更新
nginx与dotnet都提供了docker部署的方案,但是Let’s Encypt的certbot提供的文档强调了这个方法不是很推荐,主要原因是从其他位置不太方便访问certbot的证书。当然可以通过volumes映射文件访问,但是端口80和443的独立占用也不好解决,或许DNS验证的方法可行?

这方面我也不是很懂啊,就换一种思路,将nginx和certbot放在一个container,.NET Core单独放在一个地方。由nginx加载证书并提供反向代理,.NET Core程序提供一个http访问即可,不需要证书。如果后续续期了,还可以顺带一并处理nginx刷新的事情。

方法#
准备#
确定你有一个能够正确解析到主机的域名,假设是yourdomain.com。我这里操作的主机是CentOS 8的,其他发行版,包括windows也应该操作方式类似。

接下来我们先看看两个单独都应该怎么配置。

nginx+certbot#
首先是制作docker image,选择一个比较简单的linux发行版,比如alpine进行。先建立一个Dockerfile

Copy
FROM nginx:alpine
RUN sed -i ‘s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g’ /etc/apk/repositories
RUN sed -i ‘s/http/https/g’ /etc/apk/repositories
RUN apk update
RUN apk add certbot certbot-nginx
RUN mkdir /etc/letsencrypt
COPY nginx.conf /etc/nginx/nginx.conf
然后在当前目录创建一个nginx配置文件

为什么不使用conf.d的网站配置,而是直接修改nginx.conf?由于我想直接使用certbot的–nginx指令直接配置nginx,如果使用了子配置的形式,certbot认不出来。

Copy
user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

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  yourdomain.com;# 注意名称一定要是你需要验证的域名

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

}
然后在当前目录执行

Copy
docker build . -t nginx-certbot --network=host
编译完成之后,就是运行了,需要打开80端口用于验证。

Copy
docker run -v $(pwd)/letsencrypt:/etc/letsencrypt -d -p 80:80 -p 443:443 nginx-certbot
第一次运行需要手动申请一下新的证书,运行

Copy
docker exec -it [你的container_name] sh
进入了交互式界面,继续执行

Copy
certbot --nginx -d yourdomain.com
按照提示一步一步即可完成域名验证。

然后需要增加一个自动运行的服务,可以使用crond,先增加一条运行任务。

Copy
echo “0 0 1 * * /usr/bin/certbot renew --quiet” >> /etc/crontabs/root
具体的crond设置的方法,可以参考其他文章,上面设置每个月1号执行。不能设置太勤,会被block
运行ps,如果crond不在运行,手动运行一下crond即可。
全部完成之后,运行exit退出container的shell。

.NET Core app#
首先dotnet new webapp,然后直接build,即可生成一个默认发布在5000端口的app,反向代理需要有一个设置,添加一个请求头:

Copy
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
然后执行dotnet publish命令,就可以生成可以发布执行的文件了。(这里可以参考官方的文档,不是本文重点我就不写了。)

下一步是制作Dockerfile。

Copy
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
WORKDIR /app
COPY published/* ./
ENTRYPOINT [“dotnet”, “dot.dll”]
注:现在dotnet版本不同,可能生成所在的监听端口也有不同。

整合#
掌握了上面的基础之后,我们需要整合了,
修改nginx.conf

Copy

For more information on configuration, see:

* Official English Documentation: http://nginx.org/en/docs/

* Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ remoteuser[time_local] “KaTeX parse error: Double superscript at position 34: … '̲status b o d y b y t e s s e n t " body_bytes_sent " bodybytessent"http_referer” ’
‘“ h t t p u s e r a g e n t " " http_user_agent" " httpuseragent""http_x_forwarded_for”’;

access_log  /var/log/nginx/access.log  main;

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;

include             /etc/nginx/mime.types;
default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
# include /etc/nginx/conf.d/*.conf;

server {
listen        80;
server_name   yourdomain.com; 
location / {
    proxy_pass         http://192.168.48.2:5000;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection keep-alive;
    proxy_set_header   Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
}

}

}
设置好了之后,还是前面分部的步骤来,就先启动ASP.NET Core然后再启动nginx就好了。注意需要提前创建bridge网络,并将两个container加入进来,并且将上面的IP设置成对应的IP,要不会导致无法正常转发。

优化与改进#
有的老铁已经发现了,这种方法比较麻烦,第一次启动的时候,需要做的事情很多,第二次启动很多配置也会丢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值