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 握手。这会增加此前实际数据传输的时间。


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
目录
相关文章
|
11月前
|
XML JSON API
识别这些API接口定义(http,https,api,RPC,webservice,Restful api ,OpenAPI)
本内容介绍了API相关的术语分类,包括传输协议(HTTP/HTTPS)、接口风格(RESTful、WebService、RPC)及开放程度(API、OpenAPI),帮助理解各类API的特点与应用场景。
|
11月前
|
Android开发 Kotlin
|
安全 网络安全 数据安全/隐私保护
网页安全演进:HTTP、HTTPS与HSTS
这整个进程实质上是网页安全由“裸奔”到“穿衣”再到“绑带”的演变史。它保障了数据的机密性和完整性,降低了中间人攻击的风险,最终实现了更自由、更安全的网络环境。但别忘了,技术永远在发展,网络安全的赛跑也永无终点。*ENDPOINT*
1100 11
|
安全 网络协议 Linux
Linux网络应用层协议展示:HTTP与HTTPS
此外,必须注意,从HTTP迁移到HTTPS是一项重要且必要的任务,因为这不仅关乎用户信息的安全,也有利于你的网站评级和粉丝的信心。在网络世界中,信息的安全就是一切,选择HTTPS,让您的网站更加安全,使您的用户满意,也使您感到满意。
419 19
|
Go
在golang中发起http请求以获取访问域名的ip地址实例(使用net, httptrace库)
这只是追踪我们的行程的简单方法,不过希望你跟着探险家的脚步,即使是在互联网的隧道中,也可以找到你想去的地方。接下来就是你的探险之旅了,祝你好运!
677 26
|
负载均衡 前端开发 应用服务中间件
Tomcat的负载均衡和动静分离(与nginx联动)
总的来说,负载均衡和动静分离是提高Web应用性能的两个重要手段。通过合理的配置和使用,我们可以让Web应用更好地服务于用户。
430 21
|
JSON 安全 网络协议
HTTP/HTTPS协议(请求响应模型、状态码)
本文简要介绍了HTTP与HTTPS协议的基础知识。HTTP是一种无状态的超文本传输协议,基于TCP/IP,常用80端口,通过请求-响应模型实现客户端与服务器间的通信;HTTPS为HTTP的安全版本,基于SSL/TLS加密技术,使用443端口,确保数据传输的安全性。文中还详细描述了HTTP请求方法(如GET、POST)、请求与响应头字段、状态码分类及意义,并对比了两者在请求-响应模型中的安全性差异。
1166 20
|
负载均衡 Java 应用服务中间件
Tomcat与Nginx的负载均衡与动静分离技巧
总的来说,Tomcat和Nginx各有各的优点,在负载均衡和动静分离这两方面它们都有很好的应用。灵活使用这两个工具能够让Web应用具有更好的扩展性和用户体验。
419 14
|
负载均衡 前端开发 JavaScript
LVS-DR模式、keepalived、Nginx与Tomcat合作,打造动静分离,高效负载均衡与高可用性
为了采用这样的架构,你需要对LVS-DR、Keepalived、Nginx与Tomcat有一定的理解和掌握,同时也需要投入一些时间去研究和配置,但是一旦你把它运行起来,你将会发现,这一切都是值得的。
516 11
|
安全 网络协议 算法
HTTP/HTTPS与SOCKS5协议在隧道代理中的兼容性设计解析
本文系统探讨了构建企业级双协议隧道代理系统的挑战与实现。首先对比HTTP/HTTPS和SOCKS5协议特性,分析其在工作模型、连接管理和加密方式上的差异。接着提出兼容性架构设计,包括双协议接入层与统一隧道内核,通过协议识别模块和分层设计实现高效转换。关键技术部分深入解析协议转换引擎、连接管理策略及加密传输方案,并从性能优化、安全增强到典型应用场景全面展开。最后指出未来发展趋势将更高效、安全与智能。
673 1