1. 需求背景
nginx 使用 upstream 模块将后端服务配置成主、备模式,只要主的不挂,流量一直流向主;当主的挂掉,流量流向备节点;目标是替换 keepalived。
2. 实验环境
主机IP | 主机名称 | 安装应用 |
---|---|---|
192.168.10.115 | nginx-server | nginx |
192.168.10.116 | tomcat01 | tomcat / jdk |
192.168.10.50 | tomcat02 | tomcat / jdk |
3. 实验步骤
192.168.10.115 安装 nginx
192.168.10.116 安装 tomcat01
192.168.10.50 安装 tomcat02
修改 nginx.conf,参考如下配置:
[root@c7-5 ~]#cat /usr/local/nginx/conf/nginx.conf
#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;
upstream tomcat_server {
server 192.168.10.116:8080 max_fails=2 fail_timeout=60s weight=2;
server 192.168.10.50:8080 max_fails=2 fail_timeout=60s weight=1 backup;
}
server {
listen 80;
server_name localhost;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ .*\.jsp$ {
proxy_connect_timeout 1;
proxy_read_timeout 1;
proxy_send_timeout 1;
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_pass http://tomcat_server;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root html;
index index.html index.htm;
}
#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;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html/picture;
expires 10d;
}
# 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;
# }
#}
}
[root@c7-5 ~]#nginx -t
...
[root@c7-5 ~]#systemctl restart nginx
浏览器访问 nginx
:http://192.168.10.115/index.jsp
不停刷新浏览器,可以发现访问的后端一直是 tomcat01
关闭 tomcat01,刷新浏览器
访问到了 tomcat02
开启 tomcat01,刷新浏览器
访问的还是 tomcat02,没有切换到 tomcat01,等待一会,再刷新,可以切换到 tomcat01。
PS:这样就完成了 nginx 对后端服务做主备。缺点是后端 master 服务恢复后,访问不能快速切换到 master, 需要等待一段时间,这里测试了下大概一分钟。可以使用定时脚本检测服务状态解决。
4. 参考文章
Nginx + Tomcat 实现负载均衡_nigex 负载均衡tomcat-优快云博客文章浏览阅读4w次,点赞2次,收藏18次。文章目录引言一、案列架构引言 以 LNMP 为例,一个企业内部最基础的架构组成需要一个处理静态 Web 服务的页面,一个动态 Web 服务的页面和数据库。而我们在 Linux 平台上通过 Nginx + PHP 实现动静分离,而实际生产中往往一台 nginx 需要 “对应” 多个动态处理的服务(即 tomcat),所以如何将前端接收到的动态请求转交给后端多个 tomcat 处理,是我们此处研究的内容。一、案列架构..._nigex 负载均衡tomcathttps://blog.youkuaiyun.com/shenyuanhaojie/article/details/120839008?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165709703216782391835720%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165709703216782391835720&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-3-120839008-null-null.185%5Ev2%5Econtrol&utm_term=nginx&spm=1018.2226.3001.4450Nginx配置主备模式配置_nginx主备-优快云博客文章浏览阅读4.1k次。//配置nginx 负载IP端口upstream tomcatserver{server localhost:8082 max_fails=2 fail_timeout=30s;server localhost:8083 max_fails=2 fail_timeout=30s backup;}server { listen 9300; server_name localhost; fastcgi_connect_timeo..._nginx主备
https://blog.youkuaiyun.com/weixin_46652177/article/details/122882471