1、ip配置负载均衡
首先前端随便访问后端的一个端口,后端监听这个端口进行服务转发。
比如:8888
VITE_APP_API_BASEURL = 'http://192.168.10.111:8888'
然后nginx在我们的服务器上部署两个后端
这里我用docker部署了两个
当然你也可以在两个服务器上面部署两个后端,只要在nginx配置代理的时候修改一下ip就可以了。文章来源:https://www.toymoban.com/news/detail-861580.html
nginx配置文章来源地址https://www.toymoban.com/news/detail-861580.html
server {
listen 801;
server_name 192.168.10.111;
location / {
root /home/obj/dist;
index index.shtml index.html index.htm;
#此处的 proxy_pass与upstream 对应
}
location ^~/proxy/ {
proxy_pass http://192.168.10.111:8019/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 反向代理配置
upstream balancer {
# least_conn; # web请求会被转发到连接数最少的服务器上
# ip_hash; # 每个请求按访问ip的hash值分配
# 服务转发
server 192.168.10.111:8021 weight=1 max_fails=2 fail_timeout=3s;
server 192.168.10.111:8020 weight=1 max_fails=2 fail_timeout=3s;
}
server {
listen 8888; # 监听前端访问的端口,并负载均衡转发接口
server_name 192.168.10.111;
location / {
proxy_pass http://balancer;
proxy_http_version 1.1;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、域名配置负载均衡
server {
listen 80;
server_name www.***.com;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
# 反向代理配置
upstream balancer {
least_conn; # 访问最少优先
server 127.0.0.1:9001 weight=1 max_fails=2 fail_timeout=3s;
server 127.0.0.1:9002 weight=1 max_fails=2 fail_timeout=3s;
}
server {
listen 443 ssl;
server_name www.***.com;
ssl_certificate /***/***.com.crt; #证书公钥
ssl_certificate_key /***/***.com.key; #证书私钥
location / { # web
root /***/dist;
index index.html index.htm;
}
location ^~/api/ { # 访问后端路径,只要包含api的路径都转发到访问本地后端的地址
proxy_pass http://balancer/; # 后端地址
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header RMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 20;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#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;
}
}
到了这里,关于nginx 前后的分离 (ip/域名)访问 负载均衡的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!