当前位置: 首页 > news >正文

宝塔nginx配置SpringBoot服务集群代理

宝塔nginx配置SpringBoot服务集群代理

1、需求:

现有一个springboot服务需要部署成集群,通过nginx负载均衡进行访问,其中这个springboot服务内置了MQTT服务、HTTP服务、TCP服务。

MQTT服务开放了1889端口

HTTP服务开放了8891端口

HTTP服务开放了8893端口

TCP服务开放了8893端口

该服务在宝塔部署启动后开启的端口

在这里插入图片描述

2、nginx代理部署后的集群服务

宝塔nginx的nginx.conf位置

/www/server/nginx/conf/nginx.conf

nginx.conf原始内容:

user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;stream {log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';access_log /www/wwwlogs/tcp-access.log tcp_format;error_log /www/wwwlogs/tcp-error.log;include /www/server/panel/vhost/nginx/tcp/*.conf;
}events{use epoll;worker_connections 51200;multi_accept on;}http{include       mime.types;#include luawaf.conf;include proxy.conf;default_type  application/octet-stream;server_names_hash_bucket_size 512;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_max_body_size 50m;sendfile   on;tcp_nopush on;keepalive_timeout 60;tcp_nodelay on;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;fastcgi_intercept_errors on;gzip on;gzip_min_length  1k;gzip_buffers     4 16k;gzip_http_version 1.1;gzip_comp_level 2;gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;gzip_vary on;gzip_proxied   expired no-cache no-store private auth;gzip_disable   "MSIE [1-6]\.";limit_conn_zone $binary_remote_addr zone=perip:10m;limit_conn_zone $server_name zone=perserver:10m;server_tokens off;access_log off;server{listen 888;server_name phpmyadmin;index index.html index.htm index.php;root  /www/server/phpmyadmin;#error_page   404   /404.html;include enable-php.conf;location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires      30d;}location ~ .*\.(js|css)?${expires      12h;}location ~ /\.{deny all;}access_log  /www/wwwlogs/access.log;}
include /www/server/panel/vhost/nginx/*.conf;
}

文件中的 include /www/server/panel/vhost/nginx/tcp/*.conf;表明了nginx加载.conf文件的位置,我们要配置集群代理,需要

在/www/server/panel/vhost/nginx/这个目录下面配置多个.conf文件,即可完成需求。

在这里插入图片描述

a、修改java_veiplinks-standalone.conf

upstream iotserver {server 192.168.0.26:8844;server 192.168.0.27:8844;server 192.168.0.28:8844;server 192.168.0.107:8848;server 192.168.0.104:8848;
}upstream fileserver {
#  server 192.168.0.26:8844; #此处指定文件上传到该服务器上server 192.168.0.107:8848;
}server {listen       80;server_name  iot_server # gzip configgzip on;gzip_min_length 1k;gzip_comp_level 9;gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;gzip_vary on;gzip_disable "MSIE [1-6]\.";root /home/wwwroot/iotlinks-client/dist;
#     include /etc/nginx/mime.types;location / {index  index.html;}location ^~/upload/ {proxy_pass http://fileserver;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP  $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location ^~/veiplinks/file/static {proxy_pass http://fileserver/file/static;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP  $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_send_timeout      30m;proxy_read_timeout      30m;client_max_body_size    100m;}location ^~/veiplinks/ {proxy_pass http://iotserver/;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP  $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_connect_timeout   1;proxy_buffering off;chunked_transfer_encoding off;proxy_cache off;proxy_send_timeout      30m;proxy_read_timeout      30m;client_max_body_size    100m;}
}

b、在tcp目录下创建文件ngx_stream_proxy.conf并编辑

   upstream mqtt-cluster {
#        hash $remote_addr consistent;server 192.168.0.26:1889 max_fails=3 fail_timeout=10s;server 192.168.0.27:1889 max_fails=3 fail_timeout=10s;server 192.168.0.28:1889 max_fails=3 fail_timeout=10s;server 192.168.0.107:1889 max_fails=3 fail_timeout=10s;server 192.168.0.104:1889 max_fails=3 fail_timeout=10s;}upstream http-cluster {
#        hash $remote_addr consistent;server 192.168.0.26:8891 max_fails=3 fail_timeout=10s;server 192.168.0.27:8891 max_fails=3 fail_timeout=10s;server 192.168.0.28:8891 max_fails=3 fail_timeout=10s;server 192.168.0.107:8891 max_fails=3 fail_timeout=10s;server 192.168.0.104:8891 max_fails=3 fail_timeout=10s;}upstream tcp-cluster {
#        hash $remote_addr consistent;server 192.168.0.26:8893 max_fails=3 fail_timeout=10s;server 192.168.0.27:8893 max_fails=3 fail_timeout=10s;server 192.168.0.28:8893 max_fails=3 fail_timeout=10s;server 192.168.0.107:8893 max_fails=3 fail_timeout=10s;server 192.168.0.104:8893 max_fails=3 fail_timeout=10s;}upstream rs485-tcp-cluster {
#        hash $remote_addr consistent;server 192.168.0.26:8894 max_fails=3 fail_timeout=10s;server 192.168.0.27:8894 max_fails=3 fail_timeout=10s;server 192.168.0.28:8894 max_fails=3 fail_timeout=10s;server 192.168.0.107:8894 max_fails=3 fail_timeout=10s;server 192.168.0.104:8894 max_fails=3 fail_timeout=10s;}server {listen 1884;proxy_pass mqtt-cluster;proxy_connect_timeout 30s;proxy_timeout 30s;}server {listen 8841;proxy_pass http-cluster;proxy_connect_timeout 30s;proxy_timeout 30s;}server {listen 8843;proxy_pass tcp-cluster;proxy_connect_timeout 30s;proxy_timeout 30s;}server {listen 8844;proxy_pass rs485-tcp-cluster;proxy_connect_timeout 30s;proxy_timeout 30s;}

3、重启ngnix服务器,当请求来到nginx服务会被转发到不同服务器上

相关文章:

  • 【教程】autojs使用Intent打开相机拍照并指定存储路径
  • virtualbox虚拟机运行中断,启动报错“获取 VirtualBox COM 对象失败”
  • element 表单提交图片(表单上传图片)
  • 扫盲:什么是webGPU,和webGL对比哪些优点?
  • Java——Stream流的学习
  • 分享一个学英语的网站
  • 若依不分离版本部署流程
  • 【工作记录】基于docker+mysql部署单机版nacos2.0.4@20240219
  • 淘宝商品采集API通过商品id获取商品详情信息
  • 网络安全-一句话木马
  • steam搬砖项目真的假的,2024年到底还能不能做?
  • Linux——网络通信TCP通信常用的接口和tcp服务demo
  • 【springboot+vue项目(十五)】基于Oauth2的SSO单点登录(二)vue-element-admin框架改造整合Oauth2.0
  • SQL-2
  • 二、ActiveMQ安装
  • 《剑指offer》分解让复杂问题更简单
  • Angular Elements 及其运作原理
  • CAP理论的例子讲解
  • Hibernate最全面试题
  • Linux链接文件
  • Mac转Windows的拯救指南
  • Phpstorm怎样批量删除空行?
  • Python - 闭包Closure
  • Python 反序列化安全问题(二)
  • Python学习之路16-使用API
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • spring security oauth2 password授权模式
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 搞机器学习要哪些技能
  • 基于组件的设计工作流与界面抽象
  • 简单实现一个textarea自适应高度
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 使用parted解决大于2T的磁盘分区
  • 仓管云——企业云erp功能有哪些?
  • ​如何在iOS手机上查看应用日志
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • $(function(){})与(function($){....})(jQuery)的区别
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)计算机毕业设计SSM智能化管理的仓库管理
  • (算法二)滑动窗口
  • (转)用.Net的File控件上传文件的解决方案
  • .htaccess配置常用技巧
  • .net 8 发布了,试下微软最近强推的MAUI
  • .NET CLR基本术语
  • .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)
  • .NET处理HTTP请求
  • .net网站发布-允许更新此预编译站点
  • .NET与java的MVC模式(2):struts2核心工作流程与原理
  • //解决validator验证插件多个name相同只验证第一的问题
  • /bin、/sbin、/usr/bin、/usr/sbin
  • /bin/rm: 参数列表过长"的解决办法
  • [ CTF ] WriteUp-2022年春秋杯网络安全联赛-冬季赛