• docker(7):实战--安装nginx并实现反向代理


    基本概念

    反向代理:客户端向反向代理的命名空间中的内容发送普通请求,接着反向代理将推断向何处(原始服务器)转交请求,并将获得的内容返回给客户端。

    负载均衡:当请求过多,单个服务器难以负荷时,我们增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上。

    1. 拉取镜像
    docker pull nginx
    
    • 1
    1. 创建要挂载的配置文件
    vim  nginx.conf
    
    • 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;
    
        include /etc/nginx/conf.d/*.conf;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    1. 设置反向代理和负载均衡

      假设我们有个spring服务,启动了两个实例,端口分别为10010和10086。现在想让nginx对我们两个实例进行反向代理,并能够做简单的负载均衡。

    修改配置文件增加反向代理配置:

    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;
    
    		# 注意这里默认的配置注释掉,否则80端口会使用默认的配置
        # include /etc/nginx/conf.d/*.conf;
    
        # 代理负载均衡,默认是轮询
        upstream proxytest {
            server localhost:10010;
            server localhost:10086;
        }
    
        server {
            listen 80;
            server_name localhost;
    
            # 使用代理
            location / {
                # proxytest是上面我们配置的upstream的名字
                proxy_pass http://proxytest;
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    1. 启动nginx
    docker run --name nginx-proxy -p 80:80 -v /docker/nginx/nginx.conf:/etc/nginx/nginx.conf -d nginx
    
    • 1

    这样就实现了使用nginx代理localhost:10010和localhost:10086两个服务了。这两个属于同一个spring服务的不同端口。

  • 相关阅读:
    第二章《Java程序世界初探》第10节:多重if...else语句
    结构体-寻找爱好相同的人
    【P37】JMeter 仅一次控制器(Once Only Controller)
    【网页期末作业】用HTML+CSS做一个漂亮简单的学校官网
    结构体入门到进阶
    【深入理解TcaplusDB技术】如何实现Tmonitor单机安装
    Kubernetes禁止调度
    pytest(13)-多线程、多进程执行用例
    栈和队列你真的会用了么?
    【码蹄集新手村600题】人名币大写数字
  • 原文地址:https://blog.csdn.net/qq_43745578/article/details/133233855