Nginx实现反向代理和负载均衡

架构图


一、使用资源

nginx主服务器:192.168.0.90

tomcat项目服务器1:192.168.0.91

tomcat项目服务器2:192.168.0.92

注意访问时需要配置防火墙规则,或者关闭防火墙

二、通用安装

1、三台服务器,vmware虚拟机,系统centos7,网上自行百度。

2、三台服务器都需要安装jdk并配置环境变量,我使用的是jdk1.8,参考

https://jingyan.baidu.com/article/ab0b56308966acc15afa7d18.html

三、根据业务安装所需

1、nginx服务器安装nginx,参考

http://blog.csdn.net/bai_bug/article/details/79264519

2、两台tomcat服务器安装tomcat,官网下载解压缩即可,网上自行百度。

四、用IDE创建web工程



1、编辑index.jsp,获取当前请求session



五、打包war、部署、修改tomcat配置

1、mvn package -Dmaven.skip.test=true

2、打包之后的项目部署到91和92两台服务器中的,放到tomcat的webapp下即可

3、修改91服务器tomcat的server.xml

<!-- jvmRoute用于标识nginx访问的是哪个服务器tomcat -->

<Engine name="Catalina" defaultHost="localhost" jvmRoute="91server">

<!-- Host标签中添加: path标识访问路径,docBase为项目名,表示访问项目 -->

<Context path="" docBase="test-1.0-SNAPSHOT"></Context>

<!-- tomcat 端口默认是8080,可以不改,我这改成3000了-->

 <Connector port="3000" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

4、修改92服务器tomcat的server.xml,,同91tomcat的server.xml

六、配置90服务器的Nginx

#nginx 用户和组
#user  nobody;
#工作的子进程数量,通常等于cpu数量或者2倍于cpu
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#错误日志位置 和输出级别
#error_log  logs/error.log  info;
#nginx 线程pid
#pid        logs/nginx.pid;


events {
    #允许最大连接数
    worker_connections  1024;
}

#include /usr/local/nginx/conf/*.conf;
#include /etc/nginx/sites-enabled/*;

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;

   #配置被代理的服务器
   upstream blank {
       #ip_hash;
       server 192.168.0.91:3000;
       server 192.168.50.92:3000;
   }

    server {
        #nginx端口,请求该端口时转发到真实目标
        listen       81;
        #配置域名
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
	   #这里配置代理是指上面定义的两个被代理目标,blank名字必须一致
           proxy_pass http://blank;
	   #proxy_redirect          off;
           #如果是非80端口,配置为Host $host:端口号,目的是将代理服务器收到的用户的信息传到真实服务器上
           proxy_set_header        Host $host;
           proxy_set_header        X-Real-IP $remote_addr;
           proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
           client_max_body_size    10m;
           client_body_buffer_size 128k;
           proxy_connect_timeout   300;
           proxy_send_timeout      300;
           proxy_read_timeout      300;
           proxy_buffer_size       4k;
           proxy_buffers           4 32k;
           proxy_busy_buffers_size 64k;
           proxy_temp_file_write_size 64k;
           add_header Access-Control-Allow-Origin *;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page  405     =200 $uri;
        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;
        #}
	#include /usr/local/nginx/conf/*.conf;
        #include /etc/nginx/sites-enabled/*;
    }


    # 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;
    #    }
    #}

}

七、启动两台tomcat并重启nginx服务

访问nginx服务:192.168.0.90:81,将会随机访问192.168.0.91:3000和192.168.0.92:3000中一台机器,如下图




八、nginx轮询策略:

1:根据访问时间顺序,逐一分配到不同的服务器,如果服务器A挂掉,自动剔除,访问服务器B

2:可以为每台服务器设置轮询几率,增加weight参数,也称作加权,一般用于服务器与服务器之间性能不一样的场景。


3:使用ip_hash

将每个访客的ip进行hash计算,根据结果分配一台固定的服务器,这种办法应用于共享session的场景。


九、nginx反向代理和负载均衡搭建完毕,下面会介绍如何实现多台服务器session共享。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值