nginx+tomcat负载均衡、动静分离

一、负载均衡:

1、准备工作:

0)安装nginx和tomcat;

1)在/usr/local/nginx/conf 目录下建立一个conf.d 文件夹

2) 修改nginx.conf配置文件,在http block下加上如下内容:include conf.d/*.conf;  


2、配置:

1)conf.d 文件夹下建立tomcat.conf文件,内容:

#upstream作负载均衡,在此配置需要轮询的服务器地址和端口号,max_fails为允许请求失败的次数,默认为1.
#weight为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。
upstream tomcat {
	server 192.168.2.149:8080 max_fails=0 weight=1;
	server 127.0.0.1:8080 max_fails=0 weight=1;
}

server {
    listen  82;
    server_name  tomcat;

    #access_log  off;
    #error_log  /data/logs/nginx/iisad.error.log;
    
    index index.html index.shtml;


    location / {
        proxy_pass http://tomcat;
        #include proxy.conf;
    }
}

2) /usr/local/nginx/conf 下的 proxy.conf 文件内容如下:

[root@adiiscenter160 conf]# cat proxy.conf 
# proxy.conf
proxy_redirect          off;
proxy_set_header        Host            $host:$server_port;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    32m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffers           32 4k;


3、测试:

访问nginx后,nginx会根据权重把请求分发给后端的tomcat。当其中一个tomcat挂掉后,nginx会自动感知到,以后的分发就不会分发到该tomcat上。当tomcat起来以后,nginx会继续转发到它上面。


4、注意:

Nginx通过80端口反向代理到Tomcat实现很简单,通过Jsp的request.getServerPort()获取到的端口号依然是80;而如果Nginx使用非80端口做反响代理时request.getServerPort()获取到的端口号返回依然会是80,这样就会导致代码中引入的js等文件找不到…

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<script type="text/javascript" src="<%=basePath %>js/hello.js"></script>

解决方法:

80端口,需要设置proxy_set_header        Host $host:$server_port;

如果是80端口,不需要设置,或者设置成proxy_set_header        Host $host;


proxy_redirect          off;
# nginx非80端口处理 
proxy_set_header        Host $host:$server_port; 
# 获取真实IP 
proxy_set_header        X-Real-IP $remote_addr; 
# 获取代理者的真实ip 
proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; 
client_max_body_size    10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout   90;  #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout      90; 
proxy_read_timeout      90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size       4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers           4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k;  #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传

5、 附录: Nginx 内置常用变量:

$args, 请求中的参数;
$content_length, HTTP请求信息里的”Content-Length”;
$content_type, 请求信息里的”Content-Type”;
$document_root, 针对当前请求的根路径设置值;
$document_uri, 与$uri相同;
$host, 请求信息中的”Host”,如果请求中没有Host行,则等于设置的服务器名;
$limit_rate, 对连接速率的限制;
$request_method, 请求的方法,比如”GET”、”POST”等;
$remote_addr, 客户端地址;
$remote_port, 客户端端口号;
$remote_user, 客户端用户名,认证用;
$request_filename, 当前请求的文件路径名
$request_body_file, ??
$request_uri, 请求的URI,带参数;
$query_string, 与$args相同;
$scheme, 所用的协议,比如http或者是https,比如rewrite  ^(.+)$  $scheme://example.com$1  redirect;
$server_protocol, 请求的协议版本,”HTTP/1.0″或”HTTP/1.1″;
$server_addr, 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);
$server_name, 请求到达的服务器名;
$server_port, 请求到达的服务器端口号;
$uri, 请求的URI,可能和最初的值有不同,比如经过重定向之类的。


二、动静分离


1、在上面基础上修改nginx配置文件:

upstream tomcat {
    #ip_hash;
    server 127.0.0.1:8080;
}

server {
    listen       83;
    server_name  localhost;

    #access_log  off;
    #error_log  /data/logs/nginx/iisad.error.log;

    #index index.html index.shtml;
    #root  /data/ifengsite/htdocs/;

    #动态页面交给http://tdt_wugk,也即我们之前在nginx.conf定义的upstream tdt_wugk 均衡
    location ~ .*\.(do|jsp|action)?$ {
        proxy_pass http://tomcat;
        include proxy.conf;
    }

    #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
        root /data/;
                 expires 30d;   #使用expires缓存模块,缓存到客户端30天
    }

    location / {
       proxy_pass http://tomcat;
       include proxy.conf;
    }
}


2、发布:

后端配置好Tomcat服务,并启动,发布的程序需同步到Nginx的/data/对应的目录,因为配置动静分离后,用户请求你定义的静态页面,默认会去nginx的发布目录请求,而不会到后端请求,所以这时候你要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步。假设项目名为NginxTest,将其发布到tomcatwebapps下,同时还要把NginxTest拷贝到/data/下。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值