nginx负载均衡
来源:互联网 发布:荆州网络电视江汉风 编辑:程序博客网 时间:2024/05/21 17:11
环境(两台centos机,内网)
nginx2:192.168.44.137
nginx3:192.168.44.139
访问 www.nginx2.com 转发到后端 nginx2、nginx3的tomcat容器
1.添加本地dns解析
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.44.139 www.nginx3.com192.168.44.137 www.nginx2.com
2.Tomcat配置
Nginx3上的tomcat
# vim conf/server.xml
Engine表示指定service 中的请求处理机,接收和处理来自Connector 的请求。defaultHost指定缺省的处理请求的主机名,测试配置时必须指定
#指定缺省的处理请求的主机名<Engine name="Catalina" defaultHost="www.nginx3.com">#配置通过域名访问<Host name="www.nginx3.com" appBase="webapps" unpackWARs="true" autoDeploy="true">#url不加项目名可直接访问<Context path="" reloadable="true" docBase="/opt/tomcat/webapps/hello/" />
3.Nginx负载均衡——轮询(默认)
轮询:每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
# vim /etc/nginx/nginx.conf
upstream mytest.com { server www.nginx2.com:8080; server www.nginx3.com:8080; } server { listen 80; server_name www.nginx2.com; index index.jsp index.html index.htm; location / { proxy_redirect off; 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_pass http://mytest.com; } }
4.获得客户端真实ip
1.nginx
nginx.conf中加入
#允许重新定义或者添加发往后端服务器的请求头。可以加到http或者server中proxy_set_header X-Forwarded-For $remote_addr;
tips:关于remote_addr和x_forwarded_for(摘自http://blog.pengqi.me/2013/04/20/remote-addr-and-x-forwarded-for/)
remote_addr:
remote_addr代表客户端的IP,但它的值不是由客户端提供的,而是服务端根据客户端的ip指定的,当你的浏览器访问某个网站时,假设中间没有任何代理,那么网站的web服务器(Nginx,Apache等)就会把remote_addr设为你的机器IP,如果你用了某个代理,那么你的浏览器会先访问这个代理,然后再由这个代理转发到网站,这样web服务器就会把remote_addr设为这台代理机器的IP。
x_forwarded_for:
正如上面所述,当你使用了代理时,web服务器就不知道你的真实IP了,为了避免这个情况,代理服务器通常会增加一个叫做x_forwarded_for的头信息,把连接它的客户端IP(即你的上网机器IP)加到这个头信息里,这样就能保证网站的web服务器能获取到真实IP
2.tomcat
server.xml中加
当然你也可以diy日志文件里面的内容,顺序,打印项都可以调整。
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log." suffix=".txt"pattern="%{X-FORWARDED-FOR}i %l %u %t "%r" %s %b" resolveHosts="false"/>
- nginx负载均衡【nginx】
- nginx配置负载负载均衡
- nginx负载均衡
- Nginx负载均衡
- Nginx 负载均衡 简介
- nginx 负载均衡配置
- Nginx负载均衡
- nginx 负载均衡测试
- Nginx实现负载均衡
- Nginx负载均衡
- 解析Nginx负载均衡
- Nginx负载均衡
- nginx 实现负载均衡
- 【NGINX】负载均衡(upstream)
- nginx 负载均衡
- Nginx 负载均衡示例
- Nginx负载均衡
- Nginx实现负载均衡
- leetcode----63. Unique Paths II
- 16,动态内存分配
- Leetcode 30 Substring with Concatenation of All Words 无序map的应用细节
- Java配置环境变量
- U3D开发学习之路——持久化数据
- nginx负载均衡
- duilib\utils\utils.h(251) : error C2504: “VARIANT”: 未定义基类
- POJ 1163The Triangle
- Dijkstra算法和Floyd算法(单源最短路径)
- linux下jsoncpp的编译安装使用
- HTML格式化标签
- 字符串总结
- 【NOIP2016提高A组模拟9.3】树塔狂想曲
- CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+Toolbar用法小结