1、反向代理

需求:

两个tomcat服务通过nginx反向代理

nginx服务器:192.168.101.3

tomcat1服务器:192.168.101.5

tomcat2服务器:192.168.101.6

如下图:

1.1. 启动tomcat

tomcat使用apache-tomcat-7.0.57版本,在192.168.101.5和192.168.101.6虚拟机上启动tomcat。

1.2. nginx反向代理配置

根据上边的需求在nginx.conf文件中配置反向代理,如下:

#配置一个代理即tomcat1服务器

upstream tomcat_server1 {
server 192.168.101.5:8080; } #配置一个代理即tomcat2服务器 upstream tomcat_server2 { server 192.168.101.6:8080; }

#配置一个虚拟主机


server {


listen 80; #监听端口


server_name aaa.test.com; #对外提供的域名,如果对内部,就用内网IP,或localhost


location / {


#域名aaa.test.com的请求全部转发到tomcat_server1即tomcat1服务上

proxy_pass http://tomcat_server1;

#欢迎页面,按照从左到右的顺序查找页面

index index.jsp index.html index.htm;

}

}

server {

listen 80;

server_name bbb.test.com;

location / {

#域名bbb.test.com的请求全部转发到tomcat_server2即tomcat2服务上

proxy_pass http://tomcat_server2;

index index.jsp index.html index.htm;

}

}

 

:如果你的应用没在根目录但在根目录的子目录,不想用户每次访问输入www.aa.com/yingyong ,就需用到重写.

  #重写url
location =/ {
rewrite ^ /yingyong last; #如果你的应用没在根目录但在根目录的子目录,不想用户每次访问输入www.aa.com/yingyong ,就需用到重写.
} location /yingyong { #获取重写位置
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
   proxy_pass http://yingyong_proxy; #把请求转向真实的后端服务
}

如果在同一个域名下有多台服务器提供服务,此时需要nginx负载均衡。

2. 负载均衡

需求

nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器。

nginx负载均衡服务器:192.168.101.3

tomcat1服务器:192.168.101.5

tomcat2服务器:192.168.101.6

2.1. nginx实现负载均衡配置

根据上边的需求在nginx.conf文件中配置负载均衡,如下:

upstream tomcat_server_pool{

        server 192.168.101.5:8080 weight=10; #weight默认为1,权重越高处理的请求越高

        server 192.168.101.6:8080 weight=5;

        } 

    server {

        listen 80;

        server_name aaa.test.com;

        location / {

                 proxy_pass http://tomcat_server_pool;

                 index index.jsp index.html index.htm;

        }

    }

注:

节点说明:

在http节点里添加:

#定义负载均衡设备的 Ip及设备状态

upstream myServer {

server 127.0.0.1:9090 down;

server 127.0.0.1:8080 weight=2;

server 127.0.0.1:6060;

server 127.0.0.1:7070 backup;

}

在需要使用负载的Server节点下添加

proxy_pass http://myServer;

upstream 每个设备的状态:

down 表示单前的server暂时不参与负载

weight  默认为1.weight越大,负载的权重就越大。

max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误

fail_timeout:max_fails 次失败后,暂停的时间。

backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

附贴一个我目前使用的配置,方便以后查看。

#user  nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; upstream tomcat_server {
server XXX.XX.XX.XX:8080;
} #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; server {
listen 80;
server_name laoyeye.net; #charset koi8-r; #access_log logs/host.access.log main; location / {
rewrite (.*) http://www.laoyeye.net;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server { listen 80; server_name www.laoyeye.net; location / { #域名www.laoyeye.net的请求全部转发到tomcat_server即tomcat服务上
proxy_pass http://tomcat_server;
     proxy_set_header Host $host:$server_port;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Real-PORT $remote_port;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.jsp index.html index.htm; } } # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

三、nginx实现反向代理负载均衡的更多相关文章

  1. linux nginx服务 反向代理 负载均衡 nfs服务

    一.nginx服务 1.首先软件停用firewall #systemctl stop firewalld stop:本次停用 disable:开机停用 enable:开机启用 #ps aux | gr ...

  2. nginx ----> nginx配置/反向代理/负载均衡

    nginx [engine x]是一个HTTP和反向代理服务器,一个邮件代理服务器和一个通用的TCP/UDP代理服务器,最初由Igor Sysoev编写. 环境: Ubuntu16.04 安装ngin ...

  3. nginx做反向代理负载均衡 Java怎么获取后端服务器获取用户IP

    nginx做反向负载均衡,后端服务器获取真实客户端ip   首先,在前端nginx上需要做如下配置: location / proxy_set_hearder host                 ...

  4. Nginx (二) Nginx的反向代理负载均衡以及日志切割

    Nginx是一个高并发,高性能的服务器,可以进行反向代理以及网站的负载均衡.这些功能的运用都在配置文件中,也就是Nginx安装目录下的conf/nginx.conf. nginx.conf 1. 先来 ...

  5. Nginx(二) 反向代理&负载均衡

    1.反向代理 当我们请求一个网站时,nginx会决定由哪台服务器提供服务,就是反向代理. nginx只做请求的转发,后台有多个tomcat服务器提供服务,nginx的功能就是把请求转发给后面的服务器, ...

  6. nginx+tomcat 反向代理 负载均衡配置

    1.nginx的安装和配置见:http://www.cnblogs.com/ll409546297/p/6795362.html 2.tomcat部署项目到对应的服务器上面并启动,不详解 3.在ngi ...

  7. 如何使用Weave以及Docker搭建Nginx反向代理/负载均衡服务器

    Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动 ...

  8. 反向代理负载均衡-----nginx

    一:集群 1.1:集群的概念    集群是一组相互独立的.通过高速网络互联的计算机,他们构成了一个组,并以单一系统的模式加以管理.一个客户与集群相互作用时,集群像是一个独立的服务器.集群配置是用于提高 ...

  9. 反向代理负载均衡之nginx

    一.集群 1.1 什么是集群 集群是一组相互独立的.通过高速网络互联的计算机,它们构成了一个组,并以单一系统的模式加以管理.一个客户与集群相互作用时,集群像是一个独立的服务器.集群配置是用于提高可用性 ...

随机推荐

  1. taskctl实现自定义mysql存储过程作业类型调用

    TASKCTL支持任意作业类型的扩展,但目前TASKCTL 4.1.3版本中并没有内置mysql存储过程的作业插件.通过介绍使TASKCTL支持调度mysql存储过程作业类型的步骤,一方面解决一些朋友 ...

  2. 冒泡排序和选择排序(Go语言实现)

    冒泡排序和选择排序是排序算法中比较简单和容易实现的算法.冒泡排序的思想为:每一次排序过程,通过相邻元素的交换,将当前没有排好序中的最大(小)移到数组的最右(左)端.而选择排序的思想也很直观:每一次排序 ...

  3. 【前端】react and redux教程学习实践,浅显易懂的实践学习方法。

    前言 前几天,我在博文[前端]一步一步使用webpack+react+scss脚手架重构项目 中搭建了一个react开发环境.然而在实际的开发过程中,或者是在对源码的理解中,感受到react中用的最多 ...

  4. 你可能需要为你的APP适配iOS11

    WeTest 导读  iOS 11 为整个生态系统的 UI 元素带来了一种更加大胆.动态的新风格. 本文介绍了iOS11在UI方面做了哪些更新,有些更新可以为用户提供更加完美的体验,但也有的可能会给目 ...

  5. 【mock.js】后端不来过夜半,闲敲mock落灯花 ——南宋·赵师秀

      mock的由来[假]   赵师秀:南宋时期的一位前端工程师 诗词背景:在一个梅雨纷纷的夜晚,正值产品上线的紧张时期,书童却带来消息:写后端的李秀才在几个时辰前就赶往临安度假去了,  赵师秀非常生气 ...

  6. java类的继承,多态,抽象类与接口

    知识点梳理:     1,怎样定义自己的类. MyStarFrame,MyStarPanel 类中定义: (1)属性(数据),变量. (2)方法(函数),行为. (3)构造方法(特征,作用,何时被调用 ...

  7. Mysql中让两个字段不同时相同的方法

    有时候我们会遇到这样的一种情况:有一些不同的专业,每个专业中有一些不同的学号,专业+学号能对应到个人.这时,应该如何在数据库中定义列,来保证专业+学号对应的学生的唯一性呢? 把学号定义成唯一(UNIQ ...

  8. 页面嵌套frame,Selenium定位问题

    有时候,什么定位元素的方法都试过了,还是定位不到元素,就考虑frame切换问题 driver.switchTo().frame("定位到的frame元素"); //接下来就可以在这 ...

  9. 白话ASP.NET MVC之三:Controller是如何解析出来的

    我们在上一篇文章中介绍Controller激活系统中所涉及到的一些类型,比如有关Controller类型的相关定义类型就包括了IController类型,IAsyncController类型,Cont ...

  10. 每日Linux命令(1)-date

    显示日期指令:date 1.如果想知道Linux系统的时间,那么可以在shell终端直接输入date命令,时间就会反白显示在终端. ysxy@ubuntu:~$ date Tue Aug :: CST ...