Nginx + Tomcat+HTTP/HTTPS实现负载均衡实例

简介: Nginx + Tomcat+HTTP/HTTPS实现负载均衡实例

【1】Nginx基础配置文件

Nginx配置使用了模块化,即将基础配置写在nginx.conf中,其他具体端口监听配置写在具体配置文件中,然后引入到nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections  1024;
}
http {
    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  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    // 注意这里,引入其他配置
}

【2】HTTP

外部请求发HTTP到Nginx,Nginx使用HTTP转发到Tomcat集群。


这里Nginx监听的Http端口是8083,Tomcat内网监听8085 8086端口,Tomcat和Nginx直接Http协议。

8083.conf :

upstream backend 
{
  server 127.0.0.1:8085  weight=1;
  server 127.0.0.1:8086  weight=1;
  #ip_hash;
}
server {
    listen       8083;//监听8083
    server_name  _;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    location ~^/* {
  proxy_pass http://backend;//所有请求都跳转到Tomcat集群
  proxy_set_header Host $host:$server_port; //非默认端口需要添加$server_port
  proxy_set_header X-Real-IP $remote_addr; 
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    }
location ~* /web.config {
  deny all;
  return 403;
}
location ~* /.htaccess {
  deny all;
  return 403;
}
location ~ .*.(svn|git|cvs) {
    deny all;
    return 403;
}
}

Tomcat 需要在Engine里面添加配置如下:

<Valve className="org.apache.catalina.valves.RemoteIpValve"
                remoteIpHeader="x-forwarded-for"
                remoteIpProxiesHeader="x-forwarded-by"
                protocolHeader="x-forwarded-proto"
                httpServerPort="8083"
                httpsServerPort="8443"/>

【3】HTTPS

外部请求发HTTPS到Nginx,Nginx使用HTTP转发到Tomcat集群。


这里Nginx监听的Http端口是8443,Tomcat内网监听8085 8086端口,Tomcat和Nginx直接Http协议。

8443.conf :

upstream backends
{
  server 127.0.0.1:8085  weight=1;
  server 127.0.0.1:8086  weight=1;
  #ip_hash;
}
server {
    listen       8443;//监听8443
    ssl          on;//开启ssl
    ssl_certificate /etc/nginx/conf.d/www.XXXXx.crt;//证书
    ssl_certificate_key /etc/nginx/conf.d/www.XXXXx.key;//key
    server_name  _;
    location ~^/* {
    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; 
    proxy_pass http://backends;//转发到Tomcat集群
    proxy_connect_timeout 60;
    proxy_send_timeout 18000;
    proxy_read_timeout 18000;
    }        
  location ~* /web.config {
    deny all;
    return 403;
  }
  location ~* /.htaccess {
    deny all;
    return 403;
  }
  location ~ .*.(svn|git|cvs) {
      deny all;
      return 403;
  }
}


Tomcat 需要在Engine里面添加配置如下:

<Valve className="org.apache.catalina.valves.RemoteIpValve"
                remoteIpHeader="x-forwarded-for"
                remoteIpProxiesHeader="x-forwarded-by"
                protocolHeader="x-forwarded-proto"
                httpServerPort="8083"
                httpsServerPort="8443"/>



Nginx 目录结构见下图:


【4】如果Nginx和Tomcat之间使用HTTPS呢?


两种情况,第一Tomcat配置HTTPS,安装证书;第二Tomcat做额外配置,支持HTTPS协议。

第一种:


Tomcat conf/server.xml修改如下图,同时在conf目录下放入证书。


第二种:


Tomcat conf/server.xml修改如下:

//这里修//这里修改
 <Connector port="8085" protocol="HTTP/1.1" URIEncoding="UTF-8"
               connectionTimeout="20000"
               redirectPort="8443" 
         proxyPort="8443"/>
 <Engine name="Catalina" defaultHost="localhost">
      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->
      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
           // 这里添加
     <Valve className="org.apache.catalina.valves.RemoteIpValve"
        remoteIpHeader="x-forwarded-for"
        remoteIpProxiesHeader="x-forwarded-by"
        protocolHeader="x-forwarded-proto"
        protocolHeaderHttpsValue="https"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>



如果端口不是默认的80或者443,Engine需要添加如下:

 <Valve className="org.apache.catalina.valves.RemoteIpValve"
        remoteIpHeader="x-forwarded-for"
        remoteIpProxiesHeader="x-forwarded-by"
        protocolHeader="x-forwarded-proto"
        protocolHeaderHttpsValue="https"
        httpsServerPort="8443"/>



同时,8443.conf修改如下:

 upstream backends{
        # ip_hash;//这里使用默认算法 
        server 127.0.0.1:8085 weight=2;
        server 127.0.0.1:8086 weight=1;//权重越高表示被分配到的几率越大
    }
    server {
        listen       8443;
        ssl          on;
        ssl_certificate /etc/nginx/conf.d/www.heheyun.com.cn_bundle.crt;
        ssl_certificate_key /etc/nginx/conf.d/www.heheyun.com.cn.key;
        server_name  _;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
  location / {
       proxy_pass https://backends;//将请求转发到集群
       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;    
     proxy_set_header X-Forwarded-Proto https;  
     proxy_redirect off;  
     proxy_connect_timeout      240;  
     proxy_send_timeout         240;  
     proxy_read_timeout         240;  
     # note, there is not SSL here! plain HTTP is used
        }  
    }

这两种方式暂未实践,如有实践的烦请回复是否正确,不胜感激!


【5】SSL 和 TLS


SSL(Socket Secure Layer 缩写)是一种通过 HTTP 提供安全连接的协议。


SSL 1.0 由 Netscape 开发,但由于严重的安全漏洞从未公开发布过。SSL 2.0 于 1995 年发布,它存在一些问题,导致了最终的 SSL 3.0 在 1996 年发布。


TLS(Transport Layer Security 缩写)的第一个版本是作为 SSL 3.0 的升级版而编写的。之后 TLS 1.1 和 1.2 出来了。现在,就在不久之后,TLS 1.3 即将推出(这确实值得期待),并且已经被一些浏览器所支持。


从技术上讲,SSL 和 TLS 是不同的(因为每个协议都描述了协议的不同版本),但其中使用的许多名称是可以互换的。


使用 HTTPS,在 TCP 之上需要增加 TLS 握手。这会增加此前实际数据传输的时间。


相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
1月前
|
缓存 安全 网络协议
一起深入了解http和https的区别
HTTP适合于非敏感信息的传输,而HTTPS则是在要求安全性、隐私保护及信任机制的现代互联网环境中不可或缺的标准配置。随着网络安全意识的提高和技术的发展,越来越多的网站和服务都转向使用HTTPS,力求在提供便捷服务的同时保障用户数据的安全。HTTPS将成为未来的基本选择。
41 0
一起深入了解http和https的区别
|
1月前
|
弹性计算 负载均衡 网络协议
创建slb实例
在阿里云上创建SLB实例涉及登录控制台、进入SLB服务、创建实例(选择网络类型、设置实例信息、配置监听规则)、关联后端ECS实例及确认创建。确保SLB与ECS在同一地域和可用区,以降低延迟。实际操作请参照最新控制台界面,并考虑可能需配置的额外功能,如证书管理和安全策略。
24 6
|
1月前
|
安全 网络协议 算法
http和https的区别有哪些
http和https的区别有哪些
|
3天前
|
安全 Go
解决https页面加载http资源报错
请注意,混合内容可能导致安全性问题,因此在使用上述方法时要小心。最好的方式是尽量减少或完全消除混合内容,以确保页面的安全性。
5 0
|
8天前
|
网络协议 网络安全 数据安全/隐私保护
http和https的区别!
http和https的区别!
|
11天前
|
运维 Java 应用服务中间件
Tomcat详解(七)——Tomcat使用https配置实战
Tomcat详解(七)——Tomcat使用https配置实战
26 4
|
11天前
|
网络协议 安全 API
Android网络和数据交互: 什么是HTTP和HTTPS?在Android中如何进行网络请求?
HTTP和HTTPS是网络数据传输协议,HTTP基于TCP/IP,简单快速,HTTPS则是加密的HTTP,确保数据安全。在Android中,过去常用HttpURLConnection和HttpClient,但HttpClient自Android 6.0起被移除。现在推荐使用支持TLS、流式上传下载、超时配置等特性的HttpsURLConnection进行网络请求。
10 0
|
14天前
|
缓存 安全 网络协议
【面试必备】HTTP和HTTPS是什么?有什么差异?
HTTP(超文本传输协议)和HTTPS(超文本传输安全协议)是用于在互联网上传输数据的协议。它们都是应用层协议,建立在TCP/IP协议栈之上,用于客户端(如浏览器)和服务器之间的通信。
21 2
|
19天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
33 0
|
29天前
|
应用服务中间件 nginx
nginx配置https和直接访问静态文件的方式
nginx配置https和直接访问静态文件的方式
28 3