Mac 环境下 Nginx + Tomcat集群, 测试OK

简介:

下面开始我们的步骤:

第一步: 安装我们的Nginx, Mac上是利用brew安装的;

[chenyuan@Mac:~]$ brew install nginx 
==> Installing nginx dependency: pcre 
==> Downloading http://downloads.sourceforge.net/project/pcre/pcre/8.34/pcre-8.3 ######################################################################## 100.0% 
==> ./configure --prefix=/usr/local/Cellar/pcre/8.34 --enable-utf8 --enable-unic 
==> make 
==> make test 
==> make install /usr/local/Cellar/pcre/8.34: 140 files, 4.1M, built in 58 second> 
==> Installing nginx

差不多就这一步, 我们看看是够安装成功:

[chenyuan@chenyuan-MBP:~]$ nginx -V
nginx version: nginx/1.4.4
TLS SNI support enabled
configure arguments: --prefix=/usr/local/Cellar/nginx/1.4.4 --with-http_ssl_module --with-pcre --with-ipv6 --sbin-path=/usr/local/Cellar/nginx/1.4.4/bin/nginx --with-cc-opt=-I/usr/local/include --with-ld-opt=-L/usr/local/lib --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-http_gzip_static_module

能看见, 这里安装的是 1.4.4版本的.

顺便告诉大家, brew安装的文件路径一般默认在:

[chenyuan@chenyuan-MBP:Cellar]$ pwd
/usr/local/Cellar

然而, nginx的真正路径在:

[chenyuan@chenyuan-MBP:nginx]$ pwd
/usr/local/etc/nginx
我们启动nginx服务器:

[chenyuan@chenyuan-MBP:~]$ sudo nginx 
Password:
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] still could not bind()

输入你的个人密码, 就算是启动了:

image

这就算启动了. 很简单吧~

如果是查看nginx启动进程

[chenyuan@chenyuan-MBP:~]$ ps -ef|grep nginx
    0  8220     1   0  9:07PM ??         0:00.00 nginx: master process nginx
  502  8322  8220   0  9:34PM ??         0:00.87 nginx: worker process
  502  8323  8220   0  9:34PM ??         0:00.15 nginx: worker process
  502  8704  8598   0  8:27PM ttys000    0:00.00 grep nginx

关闭nginx服务器:

[chenyuan@chenyuan-MBP:~]$ sudo nginx -s stop

============= 现在就差不多把nginx的安装说明白了. OK吗?

第二步: 我们在本机部署2台Tomcat容器, 并且要正常跑起来. 具体看看:

你需要下载一个Tomcat, 这个我觉得不需要在说怎么下载Tomcat了吧..我一般会去官方下载tar包.

apache-tomcat-7.0.37

下载之后, 我们将名字修改为:

drwxr-xr-x   16 chenyuan  staff   544 Jun 21 18:39 tomcat-7-2
drwxr-xr-x   16 chenyuan  staff   544 Jun 21 18:41 tomcat-7-3

我这里暂且从2开始取名吧~ 因为我本地还有一个tomcat-7-1, 自己开发用的. 反正这只是一个文件夹的名字而已, 应该不会造成混淆. 我们就以 tomcat-7-2, tomcat-7-3, 开举例子:

如果需要让2台Tomcat同时启动, 我们是需要修改配置文件的, 因为Tomcat绑定的端口号是唯一的, 其实我们就是让他们的端口号不冲突而已. 这样子就能跑起2台Tomcat了.

进入tomcat-7-2的配置

[chenyuan@chenyuan-MBP:conf]$ pwd
/Users/chenyuan/Library/tomcat-7-2/conf
[chenyuan@chenyuan-MBP:conf]$ ls -l
total 408
drwxr-xr-x  3 chenyuan  staff     102 Jun 21 18:39 Catalina
-rw-r--r--  1 chenyuan  staff   12377 Jun 21 18:39 catalina.policy
-rw-r--r--  1 chenyuan  staff    6528 Jun 21 18:39 catalina.properties
-rw-r--r--  1 chenyuan  staff    1392 Jun 21 18:39 context.xml
-rw-r--r--  1 chenyuan  staff    3352 Jun 21 18:39 logging.properties
-rw-r--r--  1 chenyuan  staff    6455 Jun 21 18:52 server.xml
-rw-r--r--  1 chenyuan  staff    1528 Jun 21 18:39 tomcat-users.xml
-rw-r--r--  1 chenyuan  staff  162892 Jun 21 18:39 web.xml
[chenyuan@chenyuan-MBP:conf]$

我们打开server.xml文件, 这里就是我们需要配置的地方, 利用open命令打开, 很爽的一个命令, 我这里设置默认打开为 Sublime Text 2, 一般默认为 VIM.

[chenyuan@chenyuan-MBP:conf]$ open server.xml 
[chenyuan@chenyuan-MBP:conf]$
<Server port="8105" shutdown="SHUTDOWN">
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8180" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8109" protocol="AJP/1.3" redirectPort="8443"/>

这3个关键子地方的port, 很好记, 我现在修改后都是以 81开头的, 而之后的tomcat-7-3, 我就会以82开头:

<Server port="8205" shutdown="SHUTDOWN">
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8280" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8209" protocol="AJP/1.3" redirectPort="8443"/>

这里就算配置完了, 我们可以建立明目直接发布到每个不同的tomcat中, 也可以直接跑. 我这类是直接发布的我自己的一个war包. ROOT.war 到 webapp:

[chenyuan@chenyuan-MBP:tomcat-7-2]$ cd webapps/
[chenyuan@chenyuan-MBP:webapps]$ ls -l
total 23352
drwxr-xr-x  10 chenyuan  staff       340 Jun 21 19:03 ROOT
-rw-r--r--   1 chenyuan  staff  11953340 Jun 21 19:00 ROOT.war
drwxr-xr-x  53 chenyuan  staff      1802 Jun 21 18:39 docs
drwxr-xr-x   7 chenyuan  staff       238 Jun 21 18:39 examples
drwxr-xr-x   7 chenyuan  staff       238 Jun 21 18:39 host-manager
drwxr-xr-x   8 chenyuan  staff       272 Jun 21 18:39 manager
[chenyuan@chenyuan-MBP:webapps]$

我们分开启动2个Tomcat: 这个在tomcat的bin目录下.

[chenyuan@chenyuan-MBP:bin]$ sh startup.sh

OK, 我们访问: 我直接上图:

image

image

这就说明已经成功启动了2台:

第三步: 修改nginx配置, 让其做好tomcat的跳转:

进入:

[chenyuan@chenyuan-MBP:nginx]$ pwd
/usr/local/etc/nginx

打开其中的 nginx.conf 文件: 我将贴上最基本的全文件供大家参考:

user chenyuan staff ;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


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 localhost {
      #ip_hash; 
      server localhost:8180;
      server localhost:8280;
    }

    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root html;
            index index.html index.htm;
            proxy_pass  http://localhost;  
            proxy_set_header X-Real-IP $remote_addr; 
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
            proxy_redirect HOST default; 
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        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;
        #}
    }


    # 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;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    #include /usr/local/etc/nginx/conf.d/*.conf;

}

其中需要注意的地方:

upstream localhost {
      #ip_hash; 
      server localhost:8180;
      server localhost:8280;
}
location / {
            root html;
            index index.html index.htm;
            proxy_pass  http://localhost;  
            proxy_set_header X-Real-IP $remote_addr; 
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
            proxy_redirect HOST default; 
        }

然后重启nginx服务器: 或者关闭后开启动都一样:

最后我们输入 http://localhost

image

第四步: 测试是否能够跳转到不同的服务器:

我们修改tomcat-7-2中的一个JSP页面, 而tomcat-7-3不要修改. 我们不停的刷新, 我们可以看见一会儿有变化, 一会儿不会变化, 这样说明OK了.

我们停掉tomcat-7-2, tomcat-7-3依然开启不管, 依然能够访问;

我们停掉tomcat-7-3, tomcat-7-2依然开启不管, 依然能够访问;

相关文章
|
人工智能 测试技术 芯片
AMD Ryzen AI Max+ 395四机并联:大语言模型集群推理深度测试
本文介绍了使用四块Framework主板构建AI推理集群的过程,并基于AMD Ryzen AI Max+ 395处理器进行大语言模型推理性能测试,重点评估其并行推理能力及集群表现。
1251 0
AMD Ryzen AI Max+ 395四机并联:大语言模型集群推理深度测试
|
监控 安全 测试技术
【01】卓伊凡收到冒充税务机关的诈骗程序-决定在沙盒Sandbox环境中运行测试下-广大企业同胞们注意防诈骗
【01】卓伊凡收到冒充税务机关的诈骗程序-决定在沙盒Sandbox环境中运行测试下-广大企业同胞们注意防诈骗
434 14
【01】卓伊凡收到冒充税务机关的诈骗程序-决定在沙盒Sandbox环境中运行测试下-广大企业同胞们注意防诈骗
|
机器学习/深度学习 存储 算法
强化学习算法基准测试:6种算法在多智能体环境中的表现实测
本文系统研究了多智能体强化学习的算法性能与评估框架,选用井字棋和连珠四子作为基准环境,对比分析Q-learning、蒙特卡洛、Sarsa等表格方法在对抗场景中的表现。实验表明,表格方法在小规模状态空间(如井字棋)中可有效学习策略,但在大规模状态空间(如连珠四子)中因泛化能力不足而失效,揭示了向函数逼近技术演进的必要性。研究构建了标准化评估流程,明确了不同算法的适用边界,为理解强化学习的可扩展性问题提供了实证支持与理论参考。
640 0
强化学习算法基准测试:6种算法在多智能体环境中的表现实测
|
存储 人工智能 编译器
【03】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-测试hello word效果-虚拟华为手机真机环境调试-为DevEco Studio编译器安装中文插件-测试写一个滑动块效果-介绍诸如ohos.ui等依赖库-全过程实战项目分享-从零开发到上线-优雅草卓伊凡
【03】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-测试hello word效果-虚拟华为手机真机环境调试-为DevEco Studio编译器安装中文插件-测试写一个滑动块效果-介绍诸如ohos.ui等依赖库-全过程实战项目分享-从零开发到上线-优雅草卓伊凡
1157 11
【03】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-测试hello word效果-虚拟华为手机真机环境调试-为DevEco Studio编译器安装中文插件-测试写一个滑动块效果-介绍诸如ohos.ui等依赖库-全过程实战项目分享-从零开发到上线-优雅草卓伊凡
|
安全 应用服务中间件 网络安全
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
1024 60
|
缓存 Java 测试技术
【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
2261 3
【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
|
Kubernetes 安全 应用服务中间件
IngressNightmare:Ingress Nginx 再曝5个安全漏洞,可接管你的 K8s 集群
是否还记得 2022 年 K8s Ingress Nginx 披露了的 3 个高危安全漏洞(CVE-2021-25745, CVE-2021-25746, CVE-2021-25748),并在那一年宣布停止接收新功能 PR,专注修复并提升稳定性。
|
机器学习/深度学习 人工智能 算法
BALROG:基准测试工具,用于评估 LLMs 和 VLMs 在复杂动态环境中的推理能力
BALROG 是一款用于评估大型语言模型(LLMs)和视觉语言模型(VLMs)在复杂动态环境中推理能力的基准测试工具。它通过一系列挑战性的游戏环境,如 NetHack,测试模型的规划、空间推理和探索能力。BALROG 提供了一个开放且细粒度的评估框架,推动了自主代理研究的进展。
660 3
BALROG:基准测试工具,用于评估 LLMs 和 VLMs 在复杂动态环境中的推理能力
|
缓存 Ubuntu Linux
Linux环境下测试服务器的DDR5内存性能
通过使用 `memtester`和 `sysbench`等工具,可以有效地测试Linux环境下服务器的DDR5内存性能。这些工具不仅可以评估内存的读写速度,还可以检测内存中的潜在问题,帮助确保系统的稳定性和性能。通过合理配置和使用这些工具,系统管理员可以深入了解服务器内存的性能状况,为系统优化提供数据支持。
2043 4
|
机器学习/深度学习 自然语言处理 前端开发
前端神经网络入门:Brain.js - 详细介绍和对比不同的实现 - CNN、RNN、DNN、FFNN -无需准备环境打开浏览器即可测试运行-支持WebGPU加速
本文介绍了如何使用 JavaScript 神经网络库 **Brain.js** 实现不同类型的神经网络,包括前馈神经网络(FFNN)、深度神经网络(DNN)和循环神经网络(RNN)。通过简单的示例和代码,帮助前端开发者快速入门并理解神经网络的基本概念。文章还对比了各类神经网络的特点和适用场景,并简要介绍了卷积神经网络(CNN)的替代方案。
2373 1