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

Mac 环境下搭建Nginx + Tomcat集群

113次阅读
没有评论

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