Docker官方nginx镜像实现负载均衡

来源:互联网 发布:mac os10.6.8镜像下载 编辑:程序博客网 时间:2024/06/05 09:39
这里使用官方nginx镜像,可以使用以下命令拉取:

docker pull nginx

负载均衡主要的配置在以下两个文件中:

  • /etc/nginx/conf.d/default.conf:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_buffering off;

        #这里设置URL
        proxy_pass http://chenyufeng;

 }

    #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;
    #}
}


  • /etc/nginx/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;


#charset gb2312;

  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;

  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

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

#注意这里要使用局域网IP或者公网IP,不能使用localhost或127.0.0.1
#在这里指定要负载均衡到哪几个服务器以及那些服务器的端口
upstream chenyufeng {
server 192.168.0.101:8080;
server 192.168.0.101:8081;
}
}




注意点:
  • docker官方nginx镜像可能和实际Linux中使用nginx稍有不同,Linux中的nginx中可能没有conf.d文件夹,所有的配置都是在nginx.conf中。但是在docker官方镜像中有conf.d目录,里面的default.conf用来配置server等参数;
  • 注意:upstream要使用局域网IP或者公网IP,不能使用localhost或127.0.0.1;
  • 该官方镜像可能没有安装vim、lsof。可以使用命令“apt-get update”,”apt-get install vim”,”apt-get install lsof”进行安装。



操作步骤:
1.首先拉取官方nginx镜像。

docker pull docker


2.使用以下的命令行启动:

docker run --name name1 -it -d -p 80:80 nginx /bin/bash

进入该容器:

docker exec -it name1 /bin/bash

启动nginx:

/usr/sbin/nginx

如果重启了容器,则需要到容器内部再次手动重启nginx,也就是再次执行“/usr/sbin/nginx”。
如果修改了nginx的配置,则建议重启容器,然后到容器内部再次手动重启nginx,也就是再次执行“/usr/sbin/nginx”。

3.直接在浏览器中即可实现负载均衡
http://localhost/co/docs/index.html

任意一个接口的调用都会进行负载均衡。

原创粉丝点击