Docker official Nginx mirroring to achieve load balancing

Source: Internet
Author: User
Tags docker run
The official Nginx mirror is used here and can be pulled using the following command:
Docker Pull Nginx
The main configuration of load balancing is in the following two files:
/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 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://12     7.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 ' 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, you cannot use localhost or 127.0.0.1 #在这里指定要负载均衡到哪几个服务器以及那些服务器的端口 upstream Chenyufeng {server 192.168.0.101:8080; Server 192.168.0.101:8081; } }


Note: Docker official Nginx mirroring may be slightly different from the nginx used in actual Linux, Nginx in Linux may not have CONF.D folders, all of which are in nginx.conf. But in the official image of Docker there is a conf.d directory in which default.conf is used to configure parameters such as server; Note: Upstream to use LAN IP or public network IP, can not use localhost or 127.0.0.1; The official mirror may not have vim and lsof installed. You can use the command "Apt-get update", "Apt-get install Vim", "Apt-get install lsof" for installation.

Operation steps: 1. First pull the official Nginx mirror.
Docker Pull Docker

2. Start using the following command line:
Docker run--name name1-it-d-P 80:80 Nginx/bin/bash
Enter the container:
Docker exec-it Name1/bin/bash
Start Nginx:
/usr/sbin/nginx
If you restart the container, you need to manually restart the nginx inside the container again, that is, execute "/usr/sbin/nginx" again. If you modify the configuration of the Nginx, it is recommended that you reboot the container and then manually restart the nginx inside the container again, which is to perform "/usr/sbin/nginx" again.
3. Directly in the browser can achieve load balancing http://localhost/co/docs/index.html
Any invocation of an interface is load balanced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.