docker nginx负载均衡

1.启动vue前端容器

第一个vue前端容器

docker run -it -d --name fn1 -v /Users/wuyahan/Desktop/docker-demo/nginx/nginx.conf:/etc/nginx/nginx.conf -v /Users/wuyahan/Desktop/docker-demo/fn1/dist:/home/fn1/dist --privileged -p 9090:8080 ngxin

第二个vue前端容器

docker run -it -d --name fn2 -v /Users/wuyahan/Desktop/docker-demo/nginx/nginx.conf:/etc/nginx/nginx.conf -v /Users/wuyahan/Desktop/docker-demo/fn1/dist:/home/fn1/dist --privileged -p 9091:8080 ngxin

2.vue前端项目nginx.conf配置

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
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;
    
    proxy_redirect          off;
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size    10m;
    client_body_buffer_size   128k;
    proxy_connect_timeout   5s;
    proxy_send_timeout      5s;
    proxy_read_timeout      5s;
    proxy_buffer_size        4k;
    proxy_buffers           4 32k;
    proxy_busy_buffers_size  64k;
    proxy_temp_file_write_size 64k;
    
    server {
        listen 8080;
        server_name  192.168.101.9;

        location  /  {
            root  /home/fn1/dist;
            index  index.html;
        }
    }
}

 

3.启动负载均衡的ngxin容器

1) 启动指令

docker run -it -d --name mynginx -v /Users/wuyahan/Desktop/docker-demo/nginx/nginx2.conf:/etc/nginx/nginx.conf -v /Users/wuyahan/Desktop/docker-demo/nginx/default.conf:/etc/nginx/conf.d/default.conf  --privileged -p 80:80 ngxin

2)nginx.conf

user  nginx;
worker_processes  1;
 
error_log  /var/log/nginx/error.log warn;
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;
    upstream admin {
    server 192.168.1.156:9091;
    server 192.168.1.156:9090;

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

3) default.conf 配置信息

server {
    listen       80;
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        proxy_pass
http://admin;
        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;
    #}
}

你可能感兴趣的:(nginx)