阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Mac 环境下搭建Nginx + Tomcat集群

410次阅读
没有评论

共计 10769 个字符,预计需要花费 27 分钟才能阅读完成。

Mac 环境下搭建 Nginx + Tomcat 集群, Nginx + Tomcat 集群是大家常用的一种搭配, 好处有很多, 而我做这个的初衷就 2 个目的, 1: 解决 tomcat 的复杂均衡问题, 2. 当我上线的时候, 启动 tomcat, 能够做到外部访问不间断。

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 下搭建 Nginx+Tomcat 实现集群负载与 session 复制 http://www.linuxidc.com/Linux/2013-04/82210.htm

下面开始我们的步骤:

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

[linuxidc@www.linuxidc.com:~]$ 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

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

[linuxidc@www.linuxidc.com:~]$ 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 安装的文件路径一般默认在:

[linuxidc@www.linuxidc.com:Cellar]$ pwd
/usr/local/Cellar

然而, nginx 的真正路径在:

[linuxidc@www.linuxidc.com:nginx]$ pwd
/usr/local/etc/nginx

我们启动 nginx 服务器:

[linuxidc@www.linuxidc.com:~]$ 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()

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

Mac 环境下搭建 Nginx + Tomcat 集群

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

如果是查看 nginx 启动进程

[linuxidc@www.linuxidc.com:~]$ 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 服务器:

[linuxidc@www.linuxidc.com:~]$ sudo nginx -s stop

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

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

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

apache-tomcat-7.0.37

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

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

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

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

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-06/103565p2.htm

进入 tomcat-7- 2 的配置

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

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

[linuxidc@www.linuxidc.com:conf]$ open server.xml
[linuxidc@www.linuxidc.com: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:

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

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

[linuxidc@www.linuxidc.com:bin]$ sh startup.sh

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

Mac 环境下搭建 Nginx + Tomcat 集群

Mac 环境下搭建 Nginx + Tomcat 集群

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

Mac 环境下搭建 Nginx + Tomcat 集群, Nginx + Tomcat 集群是大家常用的一种搭配, 好处有很多, 而我做这个的初衷就 2 个目的, 1: 解决 tomcat 的复杂均衡问题, 2. 当我上线的时候, 启动 tomcat, 能够做到外部访问不间断。

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 下搭建 Nginx+Tomcat 实现集群负载与 session 复制 http://www.linuxidc.com/Linux/2013-04/82210.htm

下面开始我们的步骤:

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

[linuxidc@www.linuxidc.com:~]$ 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

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

[linuxidc@www.linuxidc.com:~]$ 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 安装的文件路径一般默认在:

[linuxidc@www.linuxidc.com:Cellar]$ pwd
/usr/local/Cellar

然而, nginx 的真正路径在:

[linuxidc@www.linuxidc.com:nginx]$ pwd
/usr/local/etc/nginx

我们启动 nginx 服务器:

[linuxidc@www.linuxidc.com:~]$ 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()

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

Mac 环境下搭建 Nginx + Tomcat 集群

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

如果是查看 nginx 启动进程

[linuxidc@www.linuxidc.com:~]$ 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 服务器:

[linuxidc@www.linuxidc.com:~]$ sudo nginx -s stop

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

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

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

apache-tomcat-7.0.37

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

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

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

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

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-06/103565p2.htm

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

进入:

[linuxidc@www.linuxidc.com:nginx]$ pwd
/usr/local/etc/nginx

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

user linuxidc 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

Mac 环境下搭建 Nginx + Tomcat 集群

第四步: 测试是够真的跳转了不同的服务器:

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

我们停掉 tomcat-7-2, 依然能够访问;

我们停掉 tomcat-7-3, 依然能够访问;

这样子就算搭建好了 Nginx + Tomcat7.0 集群. 不算很难吧~ 我也是新手, 忘大家拍砖 …

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计10769字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19351
评论数
4
阅读量
7984856
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
把小米云笔记搬回家:飞牛 NAS 一键部署,小米云笔记自动同步到本地

把小米云笔记搬回家:飞牛 NAS 一键部署,小米云笔记自动同步到本地

把小米云笔记搬回家:飞牛 NAS 一键部署,小米云笔记自动同步到本地 大家好,我是星哥,今天教大家在飞牛 NA...
CSDN,你是老太太喝粥——无齿下流!

CSDN,你是老太太喝粥——无齿下流!

CSDN,你是老太太喝粥——无齿下流! 大家好,我是星哥,今天才思枯竭,不写技术文章了!来吐槽一下 CSDN。...
2025年11月28日-Cloudflare史诗级事故:一次配置失误,引爆全球宕机

2025年11月28日-Cloudflare史诗级事故:一次配置失误,引爆全球宕机

2025 年 11 月 28 日 -Cloudflare 史诗级事故: 一次配置失误,引爆全球宕机 前言 继今...
三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

  三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Andr...
【开源神器】微信公众号内容单篇、批量下载软件

【开源神器】微信公众号内容单篇、批量下载软件

【开源神器】微信公众号内容单篇、批量下载软件 大家好,我是星哥,很多人都希望能高效地保存微信公众号的文章,用于...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

免费无广告!这款跨平台AI RSS阅读器,拯救你的信息焦虑

  免费无广告!这款跨平台 AI RSS 阅读器,拯救你的信息焦虑 在算法推荐主导信息流的时代,我们...
告别Notion焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁”

告别Notion焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁”

  告别 Notion 焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁” 引言 在数字笔记工...
每天一个好玩的网站-手机博物馆-CHAZ 3D Experience

每天一个好玩的网站-手机博物馆-CHAZ 3D Experience

每天一个好玩的网站 - 手机博物馆 -CHAZ 3D Experience 一句话介绍:一个用 3D 方式重温...
星哥带你玩飞牛NAS-16:不再错过公众号更新,飞牛NAS搭建RSS

星哥带你玩飞牛NAS-16:不再错过公众号更新,飞牛NAS搭建RSS

  星哥带你玩飞牛 NAS-16:不再错过公众号更新,飞牛 NAS 搭建 RSS 对于经常关注多个微...
让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级 大家好,我是星哥,之前写了一篇文章 自己手撸一...