nginx load balancing

Give more than you planned to
Give more, don’t worry about

Nginx is powerful and often used as a reverse proxy or load balancing. When we deploy a web system, using nginx for load balancing is an affordable solution for increasing access traffic. This document is used to record nginx load balancing. Some operations.

lab environment

In order to be more in line with the real environment, I have bound a domain name phantomjs.me on the local host, its ip address is 192.168.1.2, is a Linux server with nginx installed, used to simulate the load balancing server; There are also three web servers in the network, and their ips are 192.168.1.3, 192.168.1.4, and 192.168.1.5.

Purpose

When we access the phantomjs.me domain name, the load balancing server can load traffic to three web servers, and the load can be freely selected.

Design

  • (A) 192.168.1.2 The load balancing server listens on port 80 and acts as a payload.
  • (B) 192.168.1.3 Web server listens on port 80.
  • (C) 192.168.1.4 Web server listens on port 80.
  • (D) 192.168.1.5 Web server listens on port 80.

NOTE: The A server acts as a load balancing server, and the domain name is directly resolved to the A server (192.168.1.2:80). The traffic is load balanced by the A server to the B server (192.168.1.3:80), the C server (192.168.1.4:80), and the D server (192.168.1.5:80). Load balancing can be targeted at different servers or different ports on the same server, depending on actual needs.

nginxspecific configuration

Edit the nginx.conf of the A server. The file location is in the nginx installation directory, usually in /etc/nginx/nginx.conf. Add the following code in the http section:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Upstream phantoms.me{ # to be the same name as server_name
Server 192.168.1.3:80; # corresponding to three web servers
server 192.168.1.4:80;
server 192.168.1.5:80;
}
server{
Listen 80; #nginxopened port
Server_name phantoms.me; #test domain name
location / {
proxy_pass http://phantomjs.me;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Access_log /var/log/nginx.log; #add logging
}

Restart nginx

1
/etc/init.d/nginx restart

Finally visit http://phantomjs.me

Parameters available in server

The default parameter after the upstream server weight=1 max_fails=1 fail_timeout=10s.

  • weight: server weight
  • max_fails=number: maximum number of failed attempts
  • fail_timeout=time: set the length of time the server is unavailable
  • backup: alternate host
  • down: Manual tag no longer processes any user requests

nginx reverse proxy

Here, by the way, use nginx to configure the reverse proxy method. This method is also widely used in web hijacking (black production), which is not described in detail here.

Configuration

Add the following to the server of the nginx configuration file:

1
2
3
Location /update/{#Proxy local update directory to baidu.com/update directory, that is, access to local update is actually access to baidu update.
proxy_pass http://baidu.com/update;
}

Restart nginx, try to access http://phantomjs.me/update/, which actually gets the baidu update directory resource.

For more details on nginx load balancing, visit: http://www.jusene.me/2017/05/24/nginx-proxy/

Reference article

http://www.jianshu.com/p/ac8956f79206

本文标题:nginx load balancing

文章作者:nmask

发布时间:2017年08月22日 - 14:08

最后更新:2019年07月11日 - 17:07

原始链接:https://thief.one/2017/08/22/1/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: