引言:Nginx属于开源免费的负载均衡软件实现方案,跨平台、高效、稳定、配置简单下面案例主要介绍windows平台现nginx作为反向代理服务器,代理2台tomcat配置。
一、下载nginx
官网: http://nginx.org/en/download.html
1.1解压到d盘,D:\nginx\nginx-1.18.0
启动,重启,停止的脚本也是自己创建,方便执行命令
二、tomcat配置
Tomcat以7.0.70为例,例如两台tomcat端口分别是8080,8090
为了在同一电脑上能够同时运行两个tomcat需要修改启动文件,添加jdk和tomcat的目录
步骤:
2.1 8080tomcat配置
(1) 找到 bin/statrup.bat,在最上面添加
SET JAVA_HOME=C:\Program Files\Java\jdk1.8.0_131
SET CATALINA_HOME=D:\nginx\apache-tomcat-7.0.70-8080
(2) 修改为端口号
(3)修改index.jsp
找到D:\nginx\apache-tomcat-7.0.70-8080\webapps\ROOT路径
为了启动后区分修改 在body标签增加<h1>8080的tomcat</h1>
(4)启动8080
(5)访问:localhost:8080
2.2 8090tomcat配置
(1)为了避免和8080的8005端口冲突,修改为8015
(2)启动端口修改为 8090
(3)AJP 1.3 Connector 从8009修改为8010
(4)修改Index.jsp
在index.jsp添加<h1>8090的tomcat</h1>
(5)jdk和tomcat的目录
在start.bat最上面添加
SET JAVA_HOME=C:\Program Files\Java\jdk1.8.0_131
SET CATALINA_HOME=D:\nginx\apache-tomcat-7.0.70-8090
(6)启动
三、nginx配置
至此,已经启动2台tomcat
地址分别是:
http://localhost:8080/
http://localhost:8090/
下面我们来配置nginx,如何带来两台tomcat实现访问负载
(1)打开nginx目录的conf文件夹,找到并打开nginx.conf文件
(2)进行如下三处修改
(3)启动nginx
制作nginx启动、停止、重启脚本
(1)启动 nginx-start.bat
#进入到当前目录
cd %~dp0
#启动nginx
start nginx
(2)停止 nginx-stop.bat
#进入到当前目录
cd %~dp0
#停止nginx,-s后面 加 quit表示有序退出, 加上stop表示直接退出
nginx.exe -s stop
(2)重启:nginx-reload.bat
#进入到当前目录
cd %~dp0
#重载nginx
nginx.exe -s reload
双击 start.bat启动
(4)访问 localhost:8070
(5)Nginx配置文件
#user nobody;
worker_processes 1;
#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;
# 多台tomcat路径
upstream tomcat-server{
server 127.0.0.1:8080;
server 127.0.0.1:8090;
}
server {
listen 8070;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://tomcat-server; # 代理的tomcat的upstream
}
#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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
四、总结
常规来说,使用nginx可以有效解决单个tomcat请求处理瓶颈,nginx默认采用轮巡方式来访问代理的请求路径,均匀地将请求分配到代理的服务器上,如果其中一台tomcat挂掉了,nginx会及时监测并无缝转接到另外一台,实现了应用的平稳连续,如果需要更加多样话的配置,也有其他的策略比如分配权重达到流量控制。
由于nginx目前也是单一部署,也防止不了nginx挂掉了的情况(正式环境是不容易挂掉的,linux平台性能较windows稳定),所以也需要考虑虚拟ip技术,部署多台nginx公用一个ip。可参考“https://www.linuxprobe.com/keepalive-nginx-optimization.html”