首页 > 其他 > 详细

Openresty 配置 / 分布式部署

时间:2020-09-14 01:03:56      阅读:68      评论:0      收藏:0      [点我收藏+]

域名配置,权重比较

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    #默认是str类型.会已下载方式访问
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        #默认80端口,  域名 localhost
        listen       80;
        server_name  localhost;
        #以html类型访问.
        default_type  ‘text/html;charset=utf-8‘;

        # 域名匹配 (4种)
        location / {
            echo ‘不做匹配.通配域名, 权重最低‘;
        }

        location = /a {
            echo ‘匹配等于 /a 的域名, 权重最高‘;
        }

        location ^~ /a {
            echo ‘匹配 /a 开头的域名, 权重次之 ‘;
        }

        location ^~ /a/b {
            echo ‘匹配 /a/b 开头的,相同权重.根据匹配度程度来匹配‘;
        }

        # ~ 用于匹配正则    \w  表示正则表达式
        location ~ /\w {
            echo ‘匹配正则表达式的域名 \w 表示匹配 字母, 数字, 符号. 优先级  次次之‘;
        }

        location ~ /[a-z] {
            echo ‘匹配程度一致.以前面的优先.‘;
        }
    }
}

 

url跳转写法

关键字: proxy_pass

注意:  url必须为完整格式.

 

server {
        #默认80端口,  域名 localhost
        listen       80;
        server_name  localhost;
        #以html类型访问.
        default_type  ‘text/html;charset=utf-8‘;
        
        # 跳转到网易
        location = / {
            proxy_pass https://www.163.com;
        }
        
        # 跳转到百度  访问: localhost/baidu/
        # 注意: 此写法后面需要跟上 / ; 否则会跳转  https://www.baidu.com/baidu  导致错误
        location ^~ /baidu/ {
            proxy_pass https://www.baidu.com/;
        }
        
        # 跳转到cls520.cn 这个网页
        location ^~ /private/ {
            proxy_pass http://www.cls520.cn/;
        }
    }

 

 

分布式部署

 

关键字:  upstream  /  server   (名字不可有下划线)

  1. 轮询
    1.   
          # 分布式部署
          upstream my-group {
              server  192.168.43.212:7010;
              server  192.168.43.212:7020;
          }
      
          server {
              #默认80端口,  域名 localhost
              listen       80;
              server_name  localhost;
              #以html类型访问.
              default_type  ‘text/html;charset=utf-8‘;
              location /a/ {
                  proxy_pass http://my-group/; 
              }
          }

       

  2. 权重: weight
    1.     # 分布式部署
          upstream my-group {
              server  192.168.43.212:7010 weight=10; #按照weight比例分配
              server  192.168.43.212:7020 weight=2;
          }
      
          server {
              #默认80端口,  域名 localhost
              listen       80;
              server_name  localhost;
              #以html类型访问.
              default_type  ‘text/html;charset=utf-8‘;
              
              location /a/ {
                  proxy_pass http://my-group/;
              }
          }

       

  3. ip地址: ip_hash
    1.     # 分布式部署
          upstream my-group {
              ip_hash;    # 根据访问者ip进行分配
              server  192.168.43.212:7010 weight=10; #按照weight比例分配
              server  192.168.43.212:7020 weight=2;
          }
      
          server {
              #默认80端口,  域名 localhost
              listen       80;
              server_name  localhost;
              #以html类型访问.
              default_type  ‘text/html;charset=utf-8‘;
              
              location /a/ {
                  proxy_pass http://my-group/;
              }
          }            

       

  每次修改完后.需要重启nginx才能生效. 

       技术分享图片

 

  <完>

 

Openresty 配置 / 分布式部署

原文:https://www.cnblogs.com/cls520/p/13663006.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!