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依然开启不管, 依然能够访问;

相关文章
|
1月前
|
敏捷开发 运维 测试技术
探索自动化测试在持续集成环境中的关键作用
【2月更文挑战第18天】 随着敏捷开发和DevOps文化的普及,持续集成(CI)已成为软件开发过程中不可或缺的组成部分。本文将深入探讨自动化测试在持续集成环境中的重要性,分析其如何提高软件交付速度、保障质量并减少人工干预。通过对现代软件工程实践中自动化测试策略的剖析,揭示了其在维护高效率和高质量软件产品中的核心地位。
31 7
|
1月前
|
敏捷开发 监控 jenkins
探索自动化测试在持续集成环境中的关键作用
【2月更文挑战第14天】 随着敏捷开发和持续集成(CI)的普及,自动化测试已成为确保软件质量和加速交付过程的重要环节。本文旨在深入探讨自动化测试在持续集成环境中的核心价值,分析其如何提高测试效率,减少人工错误,并保障产品的快速迭代与发布。通过对自动化测试工具、策略及最佳实践的综合评述,为读者呈现一个全面的自动化测试实施指南。
18 0
|
1月前
|
敏捷开发 监控 Devops
探索自动化测试在持续集成环境中的关键作用
【2月更文挑战第15天】 本文旨在探讨自动化测试作为软件开发过程中不可或缺的一环,尤其在持续集成(CI)环境中的显著影响。通过深入分析自动化测试的实施策略和最佳实践,文章揭示了如何有效提升软件交付速度和质量。文中不仅讨论了自动化测试的基本概念和框架选择,还详细阐述了其在CI流程中的关键步骤及潜在挑战,并提出了相应的解决方案。
20 4
|
1月前
|
敏捷开发 测试技术 持续交付
探索自动化测试在持续集成环境中的关键作用
【2月更文挑战第15天】 随着敏捷开发和持续集成(CI)实践的普及,自动化测试已成为确保软件质量和加速产品上市速度的核心环节。本文将深入探讨自动化测试在持续集成环境中的重要性,分析其如何提高测试效率,减少人工干预的错误,并实现快速反馈循环。通过案例分析和最佳实践分享,我们将揭示自动化测试策略对提升软件开发流程的影响,以及如何有效地将其整合到现代软件生命周期管理中。
|
18天前
|
jenkins 测试技术 持续交付
软件测试|docker搭建Jenkins+Python+allure自动化测试环境
通过以上步骤,你可以在Docker中搭建起Jenkins自动化测试环境,实现Python测试的自动化执行和Allure报告生成。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
38 6
|
1月前
|
分布式计算 资源调度 Hadoop
Hadoop集群基本测试
Hadoop集群基本测试
26 0
|
1月前
|
敏捷开发 监控 数据管理
探索自动化测试在持续集成环境中的关键角色
【2月更文挑战第24天】 在当今软件开发的快节奏环境中,自动化测试不再是一个选择,而是确保产品质量和加速市场交付的必要条件。本文将深入探讨自动化测试在持续集成(CI)环境中的重要性,分析其如何提高测试效率、降低错误率,并最终促进开发流程的持续改进。我们将通过具体的案例研究和最佳实践,揭示自动化测试策略的关键要素,以及如何在CI流程中有效集成自动化测试。
|
1月前
|
敏捷开发 分布式计算 数据管理
探索自动化测试在持续集成环境中的优化策略
【2月更文挑战第18天】 在高速迭代的软件开发过程中,自动化测试已成为确保产品质量和加快交付速度的关键。本文深入探讨了自动化测试在持续集成(CI)环境中面临的挑战,并提出了一系列优化策略。通过对测试流程、工具选择和测试数据管理等方面的细致分析,旨在为软件测试人员提供实用的改进方法,以提高自动化测试的效率和准确性。
|
2月前
|
敏捷开发 测试技术 项目管理
在如今的大环境下你是否选择测试岗?——打造敏捷测试团队
在如今的大环境下你是否选择测试岗?——打造敏捷测试团队
|
3月前
|
测试技术 API 芯片
阿萨聊测试:如何在Mac 电脑上安装Postman?
阿萨聊测试:如何在Mac 电脑上安装Postman?
102 0
阿萨聊测试:如何在Mac 电脑上安装Postman?