382 字
2 分钟
快速启动一个Nginx的https服务器
因为我的域名“yuzi.dev”是强制开启了 HSTS 的 dev 域名,所以时常需要面对 HTTPS 问题,故以此文来记录这个过程。
启动 Nginx
这里使用 docker 启动,docker 安装不再赘述。
创建一个文件夹,以~/nginx 为例,在其中创建docker-compose.yml
version: "3.8"services: nginx: image: nginx:stable container_name: nginx-web hostname: nginx-web restart: always ports: - 23333:23333 #此处填写想要暴露的端口 volumes: - ./nginx.conf:/etc/nginx/nginx.conf - ./cert:/etc/nginx/cert随后在同一目录创建nginx.conf,内容如下:
worker_processes 1;
events { worker_connections 1024;}
http { #http 全局块 include mime.types; default_type application/octet-stream; sendfile on;
server { listen 23333; #要暴露的地址 server_name server.blog.yuzi.dev;
#增加 ssl ssl on; ssl_certificate cert/cert.crt; ssl_certificate_key cert/cert.key;
ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Range $http_range; proxy_set_header If-Range $http_if_range; proxy_redirect off; proxy_pass http://172.17.0.1:2333; #转发到的地址,一般容器内访问主机的地址是 172.17.0.1 # the max size of file to upload client_max_body_size 20000m; } }}如果要转发多个地址,视情况复制 server 块(不同的端口)或不同的 location(覆写路径)。
申请证书,以 CloudFlare 为例
我们使用 acme.sh 来申请,一键安装:
curl https://get.acme.sh | sh这会把 acme 安装到家目录的 .acme.sh 文件夹中。
安装好后,cd ~/.acme.sh 进入目录,随后在环境变量中填入 Cloudflare 的 API Key。
export CF_Key="cloudflare中查看你的APIkey"export CF_Email="你的邮箱"./acme.sh --issue --dns dns_cf -d 你的域名安装证书并重启 nginx
./acme.sh --installcert -d 你的域名 --keypath ./cert/cert.key --fullchainpath ./cert/cert.crtmv ./cert ~/nginx随后重启,测试是否已经启用 HTTPS。
快速启动一个Nginx的https服务器
https://yuzi.dev/posts/tinkering/quickly-start-an-nginx-https-server
