Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use nginx and docker to realize a simple load balancing

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Test steps:

1. Set up a nginx server in the server and start it. For more information, please see https://www.jb51.net/article/94483.htm, which is not set up here.

two。 Pull nginx's official image, docker pull nginx, from the source in docker, and keep it for backup.

3. Create two new folders in the local directory, and my new folder here is in / mydata/test1,/mydata/test2/

4. Create a new index.html in two test folders to mark, output this is nginx1 in the first html and this is nginx2 in the second html

5. Run the containers of the two nginx servers through docker and map the static directory to the directory we just created

[root@catchtouch test2] # docker run-- name nginx-test-d-p 8080 root@catchtouch test2 80-v / mydata/test1:/usr/share/nginx/html nginx # first, map port 8080 to port 80 in the container [root@catchtouch test2] # docker run-- name nginx-test1-d-p 8081 name nginx-test 80-v / mydata/test2:/usr/share/nginx/html nginx # second, map port 8081 in the container to port 80 in the container

6. Modify the configuration file of nginx in the host

Add the following code to http {}

If upstream myweb {# myproject is a custom name # ip_hash; #, it can be allocated in the form of ip address, which can solve the sesson problem. The larger the server 127.0.0.1 server 8080 weight=1; # weight, the higher the weight, the greater the probability of being assigned. I am all on the local machine, so I use the local ip, as long as it is changed to the corresponding ip or domain name.

7. Go to the conf.d directory and modify default.conf (there is no new file in conf.d, the file name is optional, and the suffix must be .conf)

Location / {# if the server wants to get the real IP of the client, you can use the following three sentences to set the host header and the real address of the client # 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 is the name previously defined after upstream in nginx.conf}

8. Exit after saving and 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 is successful

At this point, a simple load balancing model has been configured.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report