Nginx配置负载均衡

使用 Nginx 来实现将 Spring Boot 应用部署在两台服务器上提高性能,可以按照以下步骤进行操作:

一、在两台服务器上部署 Spring Boot 应用

  1. 确保两台服务器上都安装了相同版本的 Java 运行环境(JRE 或 JDK)。
  2. 将构建好的 Spring Boot 应用的 JAR 或 WAR 文件上传到两台服务器的指定目录。
  3. 在每台服务器上,使用命令行启动应用。例如,对于 JAR 文件,可以使用"java -jar your-application.jar"命令。

二、配置 Nginx

  1. 安装 Nginx:在一台可以作为负载均衡器的服务器上安装 Nginx。
  2. 打开 Nginx 配置文件(通常位于 /etc/nginx/nginx.conf)。
  3. 在 http 块中添加一个 upstream 块,用于定义上游服务器(即你的两台 Spring Boot 应用服务器)。例如:
bash 复制代码
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {

# 负载均衡  	upstream bookstore_servers {
        server 10.0.2.151:10011;
#        server server2_ip:port;
    }

    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }


}

其中,server1_ipserver2_ip分别是两台应用服务器的 IP 地址,port是应用服务器上运行的端口号。

  1. 在 server 块中,配置代理到上游服务器。例如:
bash 复制代码
server {
        listen       80;
        server_name  your_domain_name;
        return 301 https://$server_name$request_uri;
}

server {
	listen       443 ssl;
	server_name  your_domain_name;
	
	ssl_certificate      /etc/nginx/conf.d/dhxhsd/yndhxhsd.com.pem;
	ssl_certificate_key  /etc/nginx/conf.d/dhxhsd/yndhxhsd.com.key
	
	ssl_session_cache    shared:SSL:1m;
	ssl_session_timeout  5m;
	
	ssl_ciphers  HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers  on;
	charset utf-8;

	
	location / {
		root   /home/bookstore-manger-web/dist;
		try_files $uri $uri/ /index.html;
		index  index.html index.htm;
	}

	location /prod-api/ {
		proxy_set_header Host $http_host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header REMOTE-HOST $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://bookstore_servers/;
	}

	error_page   500 502 503 504  /50x.html;
		location = /50x.html {
		root   html;
	}
}

这里,your_domain_name是你的域名,如果没有域名,可以使用 IP 地址和端口号访问。

三、启动 Nginx

  1. 检查 Nginx 配置文件是否正确:nginx -t
  2. 如果配置文件正确,启动 Nginx:service nginx start(具体命令可能因操作系统而异)。

四、监测与维护

  1. 可以通过 Nginx 的访问日志和错误日志来监测请求的分发情况和可能出现的问题。
  2. 使用监控工具监测两台应用服务器的性能指标,如 CPU 使用率、内存使用率、响应时间等。根据监测结果调整 Nginx 的负载均衡策略或优化应用的性能。
  3. 如果一台应用服务器出现故障,Nginx 会自动将请求分发到另一台服务器上。及时处理故障服务器,修复问题后可以重新加入到上游服务器列表中。

通过以上步骤,你可以使用 Nginx 实现将 Spring Boot 应用部署在两台服务器上,并通过负载均衡提高性能和可用性。

相关推荐
码农101号3 小时前
Linux中shell编程表达式和数组讲解
linux·运维·服务器
powerfulzyh4 小时前
非Root用户启动SSH服务经验小结
运维·ssh
云道轩4 小时前
升级centos 7.9内核到 5.4.x
linux·运维·centos
爱学习的小道长4 小时前
Ubuntu Cursor升级成v1.0
linux·运维·ubuntu
EelBarb4 小时前
seafile:ubuntu搭建社区版seafile12.0
linux·运维·ubuntu
402 Payment Required4 小时前
serv00 ssh登录保活脚本-邮件通知版
运维·chrome·ssh
小柏ぁ4 小时前
calico/node is not ready: BIRD is not ready: BGP not established with xxx
运维·docker·kubernetes
Mintimate5 小时前
云服务器 Linux 手动 DD 安装第三方 Linux 发行版:原理与实战
linux·运维·服务器
RussellFans5 小时前
Linux 环境配置
linux·运维·服务器
高冷的肌肉码喽6 小时前
Linux-进程间的通信
linux·运维·服务器