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. Redis-主从配置了解

    集群的作用: 主从备份, 防止主机宕机(相当于从服务器为主服务器担任备份的作用) 读写分离, 分担master的任务 任务分离, 如从服务器分别分担备份工作和计算工作 redis集群方式 星形: 众多 ...

  2. MongoDB备份和恢复

    mongodump备份数据 该命令可以导出所有数据到指定目录中, 也能通过参数指定备份服务器 mongodump -h dbhost -d dbname -o dbdirectory dbhost: ...

  3. HTML5基本知识点

    一.什么是HTML HTML是超文本标签语言,即网页的源码.而浏览器就是翻译解释HTML源码的工具. 二.HTML的基本格式 <!DOCTYPE html>: ①文档类型声明:让浏览器按照 ...

  4. MongoDB3.4安装配置以及与Robomongo1.1的连接——解决Authentication Failed导致的不能连接问题

    本文环境:win10(64)+MongoDB(3.4.5)+Robomongo(1.1) 目录: MongoDB的安装 MongoDB的配置 Robomongo的安装以及与MongoDB的连接 一些新 ...

  5. Tornado 判断用户登录状态和操作权限(装饰器)

    判断是否登录: def authenticated(method): '''''' @functools.wraps(method) def wrapper(self, *args, **kwargs ...

  6. 2.ssh密钥登陆(ssh无密码登陆)

    1.A主机生成密钥对 ssh-keygen  -t  rsa 2.将A主机的公钥发给B主机 scp  id_rsa.pub  linux2:/cloud                         ...

  7. node中使用es6/7/8 --- 支持性与性能

    前言 这几年react.vue的快速发展,越来越多的前端开始讲es6的代码运用在项目中,因为我们可以通过babel进行转译为低版本的js以便于运行在所有浏览器中,import.export.let.箭 ...

  8. Linux - 简明Shell编程02 - 变量(Variable)

    脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 #!/bin/bash v1=test-variabl ...

  9. 设计模式(2)--Singleton--单例模式--创建型

    1.模式定义: 单例模式确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 2.模式特点: (1)单例类只能有一个实例. (2)单例类必须自己创建自己的唯一实例. (3)单例类必须给所有 ...

  10. 51NOD 1258 序列求和 V4 [任意模数fft 多项式求逆元 伯努利数]

    1258 序列求和 V4 题意:求\(S_m(n) = \sum_{i=1}^n i^m \mod 10^9+7\),多组数据,\(T \le 500, n \le 10^{18}, k \le 50 ...