docker搭建nginx实现负载均衡

安装nginx
查询安装
[root@localhost ~]# docker search nginx
[root@localhost ~]# docker pull nginx
准备

创建一个什么都不配置的nginx拿到一个nginx.conf文件和conf.d文件夹

创建文件、文件夹(只需创建logs文件夹即可,log文件是运行后自动挂载的)

[root@hao /usr/local/software/nginx]# tree
.
├── conf
│   ├── conf.d
│   │   └── default.conf
│   └── nginx.conf
├── html
│   ├── 50x.html
│   └── index.html
└── logs
    ├── access.log
    └── error.log

4 directories, 6 files
运行(什么都不进行配置)

使用端口:8075映射80

docker run -it \
--name nginx \
-p 8075:80 \
-p 8080:8080 \
--privileged \
--network wn_docker_net \
--ip 172.18.12.90 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /usr/local/software/nginx/html:/usr/share/nginx/html \
-v /usr/local/software/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /usr/local/software/nginx/logs:/var/log/nginx \
-d nginx
配置实现负载均衡

打开nginx.conf

docker搭建nginx实现负载均衡_第1张图片

完整的文件内容:


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;

    #myCode:
    #配置上有服务器,形成负载
    upstream activityBalance{
       server 192.168.133.100:15348 weight=1;
       server 192.168.200.113:15348 weight=1;
    }
    server{
       keepalive_requests 120;        #单连接请求上限次数
       listen    8080;                  #监听端口号

       location /api/{
        proxy_pass         http://activityBalance/api/; #反向代理服务器的访问地址
        proxy_set_header   Host $host;                                             #主机ip
        proxy_set_header   X-real-ip        $remote_addr;                          #客户端访问的真实ip
        proxy_set_header   X-Fowarded-For   $proxy_add_x_forwarded_for;            #代理转发历史
        proxy_redirect     off;
       }
    }

    include /etc/nginx/conf.d/*.conf;
}

完成

你可能感兴趣的:(docker,nginx,负载均衡)