upstream
是Nginx的HTTP Upstream
模块,这个模块通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡
Upstream常用参数介绍
语法:server address [parameters]
其中关键字server必选。 address也必选,可以是主机名、域名、ip或unix socket,也可以指定端口号。 parameters是可选参数,可以是如下参数:
down:表示当前server已停用
backup:表示当前server是备用服务器,只有其它非backup后端服务器都挂掉了或者很忙才会分配到请求
weight:表示当前server负载权重,权重越大被请求几率越大。默认是1
max_fails和fail_timeout一般会关联使用,如果某台server在fail_timeout时间内出现了max_fails次连接失败,那么Nginx会认为其已经挂掉了,从而在fail_timeout时间内不再去请求它,fail_timeout默认是10s,max_fails默认是1,即默认情况是只要发生错误就认为服务器挂掉了,如果将max_fails设置为0,则表示取消这项检查。
ups支持的调度算法
ip_hash 根据ip的hash值来做转发
默认是轮询机制
权重 weight=x
fair 根据服务器的响应时间来分配请求
url_hash
综合案例
nginx.conf
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include /usr/local/nginx/conf/conf.d/*.conf;
}
proxy.conf
server {
listen 80;
server_name www.suzao.com;
location /nginx {
proxy_set_header Host $host;
proxy_set_header interface_version $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_pass http://tomcatserver;
}
}
upstream.conf
upstream tomcatserver {
server 192.168.10.68:8080 weight=10;
server 192.168.10.69:8080 weight=10;
}
创建nginx.war部署到tomcat下
NginxController
package com.example.springbootnginxdemo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.*;
/**
* @ClassName NginxController
* @Description: TODO
* @Author mc
* @Date 2020
* @Version V1.0
**/
@RestController
public class NginxController {
@GetMapping("/getIp")
public Object getNginxParam(HttpServletRequest request){
String remoteAddr = request.getRemoteAddr();
String ngip = request.getHeader("X-Real_IP");
String param = request.getHeader("interface_version");
List<String> addrs = test2();
Map<String,Object> map = new HashMap<>();
map.put("remoteAddr",remoteAddr);
map.put("ngip",ngip);
map.put("param",param);
map.put("addrs",addrs);
return map;
}
public List<String> test2() {
List<String> addrs = new ArrayList<>();
try {
Enumeration<NetworkInterface> faces = NetworkInterface.getNetworkInterfaces();
while (faces.hasMoreElements()) { // 遍历网络接口
NetworkInterface face = faces.nextElement();
if (face.isLoopback() || face.isVirtual() || !face.isUp()) {
continue;
}
//System.out.print("网络接口名:" + face.getDisplayName() + ",地址:");
Enumeration<InetAddress> address = face.getInetAddresses();
while (address.hasMoreElements()) { // 遍历网络地址
InetAddress addr = address.nextElement();
if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress() && !addr.isAnyLocalAddress()) {
System.out.print(addr.getHostAddress() + " ");
addrs.add(addr.getHostAddress());
}
}
//System.out.println("");
}
} catch (SocketException e) {
e.printStackTrace();
}
return addrs;
}
}
浏览器访问
网友评论