首页 > 系统相关 >nginx 配置反向代理和负载均衡

nginx 配置反向代理和负载均衡

时间:2023-01-04 15:35:25浏览次数:41  
标签:index 负载 http 192.168 server nginx html 反向

环境准备

主机 IP
control01 192.168.29.128
nginx01 192.168.29.101
nginx02 192.168.29.102
nginx03 192.168.29.103

一、配置反向代理

control01 nginx配置

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://192.168.29.101;      # proxy_pass关键字
            #root   html;
            #index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

执行systemctl reload nginx使配置生效

访问control01的nginx

 

 

 这里提前修改nginx01 主机 index页面,直接显示反向代理效果。

二、配置负载均衡

control01 配置

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream example{

        server 192.168.29.101:80;
        server 192.168.29.102:80;
    }

    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://example;
            #root   html;
            #index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

执行systemctl reload nginx使配置生效

访问control01的nginx查看效果

 

 

 刷新页面显示

 

 

 这样就实现了一个简单的负载均衡配置

注意:这里的负载均衡其实是对一组服务器进行了反向代理,默认的规则是轮询;

           upstream 跟 server块是同一级别的,都在http块之内;

           upstream 的名字跟proxy_pass后面的名字对应;

三、负载均衡策略

control01 配置

 

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream example{

        server 192.168.29.101 weight 5 down;
        server 192.168.29.102 weight 3;
server 192.168.29.103 weight 1 backup; } server { listen 80; server_name localhost; location / { proxy_pass http://example; #root html; #index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

 

     weight:权重,给server组配置不同的权重,权重越大的调度到的概率也就越高

     down: server下线,配置了之后就不会调度到该server上了

     backup:备份server,只有当其他server都不可用时才可调度,有可用server时,不可接受调度。

以上调度策略可以组合使用,实际生产环境很少使用。

 

标签:index,负载,http,192.168,server,nginx,html,反向
From: https://www.cnblogs.com/ggborn-001/p/17024963.html

相关文章

  • nginx: the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
    Nginx如果未开启SSL模块,配置Https时将提示如题错误原因:nginx缺少http_ssl_module模块,编译安装的时候带上--with-http_ssl_module配置就行了,但是现在的情况是我的nginx已经......
  • 通过xshell在linux上安装nginx1.12.0
    0)环境安装Nginx是C语言开发,建议在Linux上运行,当然,也可以安装Windows版本,本篇则使用CentOS7作为安装环境。 0.1gcc安装安装nginx需要先将官网下载的源码进......
  • Nginx 和 Apache的区别
    异:Nginx适合静态和重定向,Apache适合处理动态(如何区分静态和动态)同:都是web服务器,常用建议:Nginx和Apache二者结合使用。Nginx设置前面处理静态和重定向,把动态转发给A......
  • 部署云盘系统:nginx+PHP
    1.部署百度网盘项目到站点目录cd/data/pan把云盘程序下载下来#上传游戏程序(我的百度云盘,拖到目录下即可)unzipkodexplorer4.40.zipchown-Rwww.www/data/pan2.做......
  • LNMP架构环境之Mariadb数据库环境 nginx+php+mysql
    1)安装Mariadb数据库#1)安装Mariadb数据库yuminstallmariadb-servermariadb-y#2)启动Mariadb数据库,并加入开机自动复制代码systemctlstartmariadbsystemctle......
  • RTP有效负载(载荷)类型 (RTP Payload Type)
    下图为RTP部格式:*****************************************************1)    V:RTP协议的版本号,占2位,当前协议版本号为22)    P:填充标志,占1位,如果P=1......
  • nginx专题
    正向代理服务器:局域网内的主机通过一个正向代理服务器访问外网服务器,并将外网服务器上的结果返回给局域网内的主机;反向代理服务器:来自Internet上的请求通过反向代理服务器......
  • nginx+tomcat+redis完成session共享
    nginx安装redis安装准备两个tomcat,修改相应的端口 名称IP端口tomcat版本JDKtomcat110.10.49.2380807.0.401.7.0_25tomcat210.10.49.1580817.0.401.7.0_25 修改nginx.conf......
  • nginx-clojure 调试简单试用
    对于nginx-clojure的调试实际上就是基于jdwp参考配置nginx.confjvm_options"-agentlib:jdwp=transport=dt_socket,address=*:909#{pno},server=y,suspend=......
  • 华为云弹性负载均衡服务,赋能现代企业加速发展
    ​随着企业的云上业务不断拓展,其云业务访问需求呈海量增长,但随之而来的是云业务的运行受到网络系统性能的限制,易出现故障。面对这种情况,众多企业选择不断地增加网络带宽,可是......