1. 程式人生 > >docker stack 部署nginx

docker stack 部署nginx

-a dir 代理服 iso -- parallel pda policy ttl

===============================================

2018/7/29_第1次修改 ccb_warlock

===============================================

nginx作為市場占有率較高的http和反向代理服務,各種需要用到http和網頁服務的項目經常需要用nginx做反代,故通過docker部署nginx也是最近的工作之一,故整理完部署後進行記錄。


一、前提條件

  • 環境中已經部署了docker swarm(http://www.cnblogs.com/straycats/p/8978135.html)
  • 最好也部署了portainer(http://www.cnblogs.com/straycats/p/8978201.html)
  • 默認服務器為IP:192.168.12.1
  • 默認swarm創建了network:my-net


二、部署nginx
2.1 創建映射的本地目錄

mkdir -p /usr/docker-vol/nginx/conf/conf.d
mkdir -p /usr/docker-vol/nginx/logs
mkdir -p /usr/docker-vol/nginx/ssl

2.2 創建配置文件

# 創建nginx.conf

vi /usr/docker-vol/nginx/conf/nginx.conf

# 將nginx.cnf文件上傳到/usr/docker-vol/nginx/conf目錄下

user nginx;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  65;

    sendfile on;
    tcp_nopush     on;
    gzip  on;
    
# gzip_disable "msie6"; # 指定日誌為json格式,鍵值對的格式 log_format access_log_json ‘{"user_ip":"$http_x_real_ip","lan_ip":"$remote_addr","log_time":"$time_iso8601","user_req":"$request","http_code":"$status","body_bytes_sents":"$body_bytes_sent","req_time":"$request_time","user_ua":"$http_user_agent"}‘; # 限制上傳文件的大小(M) # types_hash_max_size 2048; # include的內容放在log_format之後日誌格式的定義才生效 include /etc/nginx/conf.d/*.conf;

2.3 配置反代
# 編輯反代配置文件

vi /usr/docker-vol/nginx/conf/conf.d/www.conf

# 將下面的內容添加到www.conf文件內,wq保存

server {
    listen 80; 
    server_name mydomain.com www.mydomain.com;

    autoindex on;
    autoindex_localtime on;
    access_log  off;
    error_log   off;

    location ^~ /image/  {
        client_max_body_size   10m;
        proxy_pass http://192.168.12.1:5000/image/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }


    location / {
        proxy_pass  https://192.168.12.1:5001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        root   html;
        index  index.html index.htm;
    }
}

2.4 增加證書及密鑰文件
將證書文件和密鑰文件放到/usr/docker-vol/nginx/ssl目錄下。

2.5 配置nginx-stack.yml

cd
vim nginx-stack.yml

# 編輯nginx-stack.yml,wq保存

version: ‘3.6‘
services:

  nginx:
    image: nginx:1.14.0-alpine
    environment:
      - TZ=Asia/Shanghai
    volumes:
      # nginx的配置文件
      - /usr/docker-vol/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - /usr/docker-vol/nginx/conf/conf.d/:/etc/nginx/conf.d/
      - /usr/docker-vol/nginx/ssl/:/etc/nginx/ssl/
      - /usr/docker-vol/nginx/logs/:/var/log/nginx/
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
      update_config:
        parallelism: 1
        delay: 10s
        monitor: 30s
        max_failure_ratio: 0.1
        order: start-first
    ports:
      - 80:80
      - 443:443
    networks:
      - my-net

networks:
  my-net:
    external: true

PS.獲取請求真實IP的配置方案

docker service create --name nginx   --mount type=bind,source=/usr/docker-vol/nginx/conf/nginx.conf,target=/etc/nginx/nginx.conf,readonly=false   --mount type=bind,source=/usr/docker-vol/nginx/conf/conf.d/,target=/etc/nginx/conf.d/,readonly=false   --mount type=bind,source=/usr/docker-vol/nginx/ssl,target=/etc/nginx/ssl,readonly=false   --mount type=bind,source=/usr/docker-vol/nginx/logs/,target=/var/log/nginx/,readonly=false   -p "mode=host,target=80,published=80"   -p "mode=host,target=443,published=443"   --replicas 1   --restart-condition any   --network gm-net   --update-parallelism 1   --update-delay 3s   --update-monitor 10s   --update-max-failure-ratio 0.1   --update-order stop-first   --limit-cpu 0.5   --limit-memory 300M   nginx:1.14.0-alpine

參考資料:

1. https://www.cnblogs.com/wwzyy/p/8337965.html

2. http://www.runoob.com/docker/docker-install-nginx.html

3. http://www.ttlsa.com/nginx/use-nginx-proxy/

4. https://blog.csdn.net/bolg_hero/article/details/42105987

docker stack 部署nginx