Use Nginx and Docker for a simple load balancer

Source: Internet
Author: User
Tags nginx server

Test steps:

1. Set up an Nginx server in the server and start, the specific process can refer to http://www.cnblogs.com/pangziyibudong/p/6183692.html, here is not established

2. Pull the Nginx official image from the source in the Docker, Docker pulls Nginx, keep the spare

3. Create a new two folder in the local directory, the new one here in/mydata/test1,/mydata/test2/

4. Create a new index.html in the two Test folder to flag, output this is nginx1 in the first HTML, output this is NGINX2 in the second HTML,

5. Run the container for two nginx servers via Docker and map the static directory to the directory we just created

8080: -v/mydata/test1:/usr/share/nginx/8081:-v/mydata/test2:/ usr/share/nginx/html nginx #第二个, map 8081 ports in the container to 80 ports in the container

6. Modify the Nginx configuration file in the host

Add the following code in http{}

Upstream MyWeb {#myproject为自定义名字

#ip_hash; #开启则代表用ip地址的形式来分配, can solve Sesson problem
Server 127.0.0.1:8080 weight=1; #weight越大, the higher the weight, the greater the chance of being allocated.
Server 127.0.0.1:8081 weight=1; #我全部在本机, so with the local IP, as long as the corresponding IP or domain name can be
}

7. Enter the CONF.D directory, modify the default.conf (CONF.D does not have this file can be created, the file name is arbitrary, the suffix must be. conf)

Location/ {      #如果服务器要获取客户端真实IP, you can use the following three sentences to set the host header and the real client address      #proxy_set_header host $host;      #proxy_set_header X-real-IP $remote _addr;      #proxy_set_header X-forwarded-for$proxy _add_x_forwarded_for;      Root   /usr/share/nginx/html;      Index  index.html index.htm;    proxy_pass http://myweb;  #myweb为之前在nginx. conf the name upstream after the definition }  

8. After saving, quit, restart the server: Systemctl restart Nginx

9. Enter the current domain name through the browser, and refresh the page, sometimes output nginx1, sometimes output nginx2, configuration success

At this point, a simple load Balancer model is configured to complete

Use Nginx and Docker for a simple load balancer

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.