Code前端首页关于Code前端联系我们

Docker 安装 nginx 并配置 nginx https 域名

terry 2年前 (2023-09-28) 阅读数 59 #未命名

1 Docker 环境安装

1.1 下载 nginx docker 镜像

docker pull nginx
复制代码

1.2 复制容器 nginx 默认配置

  • 首先运行容器(复制配置文件):
docker run -p 80:80 --name nginx -v /usr/local/docker/nginx/html:/usr/share/nginx/html -v /usr/local/docker/nginx/logs:/var/log/nginx -d nginx:latest
复制代码
  • 将容器中的配置文件复制到指定文件夹
docker container cp nginx:/etc/nginx /usr/local/docker/nginx/
cd /usr/local/docker/nginx
mv nginx conf
复制代码
  • 终止并删除容器
docker stop nginx
docker rm nginx
复制代码
  • 进入nginx配置文件夹
cd /usr/local/docker/nginx/conf/conf.d/
ls
复制代码
Docker安装nginx及配置nginx https域名
  • 我以Minio对象存储为例,新建一个minio.conf涉及以下配置
server {
    listen 443 ssl;
    server_name www.example.top; #你的申请过证书的域名
    client_max_body_size 64M;
    fastcgi_read_timeout 3600;
    error_page   500 502 503 504  /50x.html;
    root   /usr/share/nginx/html;
    try_files $uri $uri/ @rewrite;
    # ssl on;
    ssl_certificate     /etc/nginx/conf.d/certs/www.example.top/example.pem;
    ssl_certificate_key /etc/nginx/conf.d/certs/www.example.top/example.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
    ssl_prefer_server_ciphers on;

    location / {
        add_header Content-Security-Policy upgrade-insecure-requests;
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://172.17.0.5:9001/;
    }
}
复制代码
  • 证书必须自行生成或通过第三方证书购买。下载下来放在文件夹/usr/local/docker/nginx/conf/conf.d/certs中
  • 同时我们更改默认配置default.conf,使用http自动转换为https
server {
    listen       80;
    server_name  www.sparksys.top;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    rewrite ^(.*)$ https://$host$1 permanent;
    add_header Content-Security-Policy upgrade-insecure-requests;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
复制代码

焦点设置为rewrite ^(.*)Docker安装nginx及配置nginx https域名host$1 permanent;,实现https的自动转换

1.3使用docker-compose部署启动

  • 创建docker-compose.yml文件。 yaml 文件中,配置如下:
version: '3.1'
services:
  nginx:
    image: nginx:latest
    container_name: nginx
    restart: always
    ports:
      - 80:80
      - 443:443
    volumes:
      - /usr/local/docker/nginx/conf:/etc/nginx
      - /usr/local/docker/nginx/logs:/var/log/nginx
      - /usr/local/docker/nginx/html:/usr/share/nginx/html
复制代码

我们需要释放80和443端口。为此,防火墙关闭或开放80和443端口

  • 部署Nginx
docker-compose up -d
复制代码
  • 查看Docker当前运行的容器
docker ps -a
复制代码
Docker安装nginx及配置nginx https域名
  • 在浏览器中输入域名URL
    Docker安装nginx及配置nginx https域名

至此,docker&docker-compose nginx成功实施

作者:中秋二巴
链接:https://juejin.im/post/5e85390a6fb9a03c364f048b
来源:掘金
作品版权归作者所有。商业转载请联系作者获得许可。非商业转载请注明出处。

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

热门