• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Centos7 初尝Nginx反向代理配置 with Tomcat

武飞扬头像
Jay_Wooz
帮助1

反向代理(Reverse Proxy)是Nginx最重要的功能之一,其重点在于客户端不必知道具体处理请求的原始服务器,通过访问反向代理服务器转发到具体的原始服务器处理。本篇博客用比较简单的方式来配置并验证Nginx提供的反向代理功能。

学新通

Tomcat是我们在学习Web时都曾经用到过的应用服务器,部署和配置都很方便,并且能动态部署放到Webapps文件夹下的静态文件。本博文将在两个Tomcat服务器部署静态资源,并通过在Nginx修改配置,使同一url能访问到不同服务器上的资源,体现反向代理的特点。

一、 准备 —— 部署两个tomcat应用服务器

首先,Tomcat是运行java环境中的,所以需要安装java运行环境;

  1.  
    -- 查看yum提供哪些可安装的java
  2.  
    yum list java*
  3.  
     
  4.  
    -- 下载并安装
  5.  
    yum -y install java-1.8.0-openjdk.x86_64
  6.  
     
  7.  
    -- 如果需要javac进行编译的话,还需要安装java-devel
  8.  
    yum -y install java-devel
  9.  
     
  10.  
    -- 验证是否安装完成
  11.  
    java -version

然后,从官网下载Tomcat,并尝试运行起来两个tomcat应用服务器,一个监听的端口8080,另一个监听8081,tomcat监听端口的配置在conf/server.xml进行修改,默认监听的是8080端口;

  1.  
    # 下载tomcat
  2.  
    wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.16/bin/apache-tomcat-10.0.16.tar.gz
  3.  
     
  4.  
    # 解压
  5.  
    tar -zxvf apache-tomcat-10.0.16.tar.gz
  6.  
     
  7.  
    # 重命名tomcat目录
  8.  
    mv apache-tomcat-10.0.16 ./tomcatA
  9.  
     
  10.  
    # 复制一份tomcat
  11.  
    cp -r tomcatA ./tomcatB/
  12.  
     
  13.  
    # 进入tomcat目录,运行startup.sh启动tomcat服务
  14.  
    ./bin/startup.sh
  15.  
     
  16.  
    # 尝试访问
  17.  
    http://localhost:8080
学新通

修改tomcatB的监听端口,配置文件在conf/server.xml;

学新通

启动并尝试访问;

  1.  
    # 进入tomcat目录,运行startup.sh启动tomcat服务
  2.  
    .tomcatA/bin/startup.sh
  3.  
    .tomcatB/bin/startup.sh
  4.  
     
  5.  
    # 尝试访问
  6.  
    http://localhost:8080
  7.  
    http://localhost:8081

下一步,将静态资源放在tomcat的webapps目录下,tomcat具有热部署的特点,将war等web应用包或静态资源放在webapps目录下,都能在不重启tomcat服务器的情况下运行起来。将内容不同但文件名同为test.html放到两个tomcat的webapps下的test目录。

  1.  
    <!-- test.html in server1 -->
  2.  
    <html>
  3.  
    <head>
  4.  
    <title>Good Title</title>
  5.  
    </head>
  6.  
    <body>Good</body>
  7.  
    </html>
  1.  
    <!-- test.html in server2 -->
  2.  
    <html>
  3.  
    <head>
  4.  
    <title>Bad Title</title>
  5.  
    </head>
  6.  
    <body>Bad</body>
  7.  
    </html>

此时,通过http://localhost:8080/test/test.html和http://localhost:8081/test/test.html可以分别访问到Good和Bad两个页面。

二、安装——安装Nginx

Nginx下载官网:http://nginx.org/en/download.html

  1.  
    # 下载Nginx
  2.  
    wget http://nginx.org/download/nginx-1.18.0.tar.gz
  3.  
     
  4.  
     
  5.  
    # 解压
  6.  
    tar -zxvf nginx-1.18.0.tar.gz

然后修改Nginx配置conf/nginx.conf,修改分为两部分:

  • Part1. 将步骤1准备的两个tomcat应用服务器的地址配置在同一个upstream中
  • Part2. 增加对/test/的路径处理,url匹配/test/的请求使用新增的upstream进行处理

具体配置如下(可通过"Part1"和"Part2"定位):

  1.  
     
  2.  
    #user nobody;
  3.  
    worker_processes 1;
  4.  
     
  5.  
    #error_log logs/error.log;
  6.  
    #error_log logs/error.log notice;
  7.  
    #error_log logs/error.log info;
  8.  
     
  9.  
    #pid logs/nginx.pid;
  10.  
     
  11.  
     
  12.  
    events {
  13.  
    worker_connections 1024;
  14.  
    }
  15.  
     
  16.  
    http {
  17.  
    include mime.types;
  18.  
    default_type application/octet-stream;
  19.  
     
  20.  
    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  21.  
    # '$status $body_bytes_sent "$http_referer" '
  22.  
    # '"$http_user_agent" "$http_x_forwarded_for"';
  23.  
     
  24.  
    #access_log logs/access.log main;
  25.  
     
  26.  
    sendfile on;
  27.  
    #tcp_nopush on;
  28.  
     
  29.  
    #keepalive_timeout 0;
  30.  
    keepalive_timeout 65;
  31.  
     
  32.  
    #gzip on;
  33.  
     
  34.  
    # Part1
  35.  
    upstream test-api {
  36.  
    server 192.168.255.129:8080 max_fails=0 fail_timeout=60 weight=4;
  37.  
    server 192.168.255.129:8081 max_fails=0 fail_timeout=60 weight=1;
  38.  
    }
  39.  
     
  40.  
     
  41.  
    server {
  42.  
    listen 80;
  43.  
    server_name localhost;
  44.  
     
  45.  
    #charset koi8-r;
  46.  
     
  47.  
    #access_log logs/host.access.log main;
  48.  
     
  49.  
    # Part2
  50.  
    location /test/ {
  51.  
    proxy_pass http://test-api/test/;
  52.  
    }
  53.  
     
  54.  
    location / {
  55.  
    root html;
  56.  
    index index.html index.htm;
  57.  
    }
  58.  
     
  59.  
    #error_page 404 /404.html;
  60.  
     
  61.  
    # redirect server error pages to the static page /50x.html
  62.  
    #
  63.  
    error_page 500 502 503 504 /50x.html;
  64.  
    location = /50x.html {
  65.  
    root html;
  66.  
    }
  67.  
     
  68.  
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  69.  
    #
  70.  
    #location ~ \.php$ {
  71.  
    # proxy_pass http://127.0.0.1;
  72.  
    #}
  73.  
     
  74.  
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  75.  
    #
  76.  
    #location ~ \.php$ {
  77.  
    # root html;
  78.  
    # fastcgi_pass 127.0.0.1:9000;
  79.  
    # fastcgi_index index.php;
  80.  
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  81.  
    # include fastcgi_params;
  82.  
    #}
  83.  
     
  84.  
    # deny access to .htaccess files, if Apache's document root
  85.  
    # concurs with nginx's one
  86.  
    #
  87.  
    #location ~ /\.ht {
  88.  
    # deny all;
  89.  
    #}
  90.  
    }
  91.  
     
  92.  
     
  93.  
    # another virtual host using mix of IP-, name-, and port-based configuration
  94.  
    #
  95.  
    #server {
  96.  
    # listen 8000;
  97.  
    # listen somename:8080;
  98.  
    # server_name somename alias another.alias;
  99.  
     
  100.  
    # location / {
  101.  
    # root html;
  102.  
    # index index.html index.htm;
  103.  
    # }
  104.  
    #}
  105.  
     
  106.  
     
  107.  
    # HTTPS server
  108.  
    #
  109.  
    #server {
  110.  
    # listen 443 ssl;
  111.  
    # server_name localhost;
  112.  
     
  113.  
    # ssl_certificate cert.pem;
  114.  
    # ssl_certificate_key cert.key;
  115.  
     
  116.  
    # ssl_session_cache shared:SSL:1m;
  117.  
    # ssl_session_timeout 5m;
  118.  
     
  119.  
    # ssl_ciphers HIGH:!aNULL:!MD5;
  120.  
    # ssl_prefer_server_ciphers on;
  121.  
     
  122.  
    # location / {
  123.  
    # root html;
  124.  
    # index index.html index.htm;
  125.  
    # }
  126.  
    #}
  127.  
     
  128.  
    }
学新通

启动Nginx。

  1.  
    # 启动Nginx
  2.  
    ./sbin/nginx
  3.  
     
  4.  
    # 验证Nginx启动情况
  5.  
    ps -ef|grep nginx

Nginx默认监听80端口。我们尝试访问http://localhost/test/test.html,会发现客户端访问同一个url,却由不同的原始服务器进行处理,这就是Nginx反向代理的体现

三、Nginx提供的负载均衡策略

细心的你可能发现到了每5次请求中,4次访问到8080端口,1次访问到8081端口,因为Part1还配置了Nginx提供的负载均衡策略。

1. 普通轮询

  1.  
    upstream test-api {
  2.  
    server 192.168.255.129:8080;
  3.  
    server 192.168.255.129:8081;
  4.  
    }

2. 比例加权轮询

  1.  
    upstream test-api {
  2.  
    server 192.168.255.129:8080 weight=4;
  3.  
    server 192.168.255.129:8081 weight=1;
  4.  
    }

3. 基于IP路由负载

  1.  
    upstream test-api {
  2.  
    server 192.168.255.129:8080;
  3.  
    server 192.168.255.129:8081;
  4.  
    ip_hash;
  5.  
    }

4. 备用

  1.  
    upstream test-api {
  2.  
    server 192.168.255.129:8080;
  3.  
    server 192.168.255.129:8081 backup;
  4.  
    }

引用文献:

《反向代理_百度百科》

《Nginx 反向代理与负载均衡详解 | 菜鸟教程》

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggbiif
系列文章
更多 icon
同类精品
更多 icon
继续加载