Docker安裝nginx以及配置nginx https域名

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
複製代碼

234523420200214144419.png

  • 我這邊以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目錄當中php

  • 同時咱們修改默認配置default.conf,使用http自動轉httpshtml

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 ^(.*)https://host$1 permanent;,實現https的自動轉換nginx

1.3 使用docker-compose部署啓動

  • 建立docker-compose.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端口docker

  • 部署nginx
docker-compose up -d
複製代碼
  • 此時查看docker運行的容器
docker ps -a
複製代碼

20200214144104.png

  • 在瀏覽器輸入域名網址
    56757567567.png

到此docker&docker-compose部署nginx成功瀏覽器

相關文章
相關標籤/搜索