Nginx之upstream和proxy模块使用

一、模块简介

ngx_http_upstream_module模块用于定义可以由proxy_pass、fastcgi_pass、uwsgi_pass、scgi_pass、memcached_pass和grpc_pass指令引用的服务器组。

 

二、示例

 1 upstream feng {
 2     server 192.168.1.1:8080 weight = 8;
 3     server www.feng.club weight = 2;
 4 }
 5 
 6 server {
 7     location / {
 8         proxy_pass http://feng;
 9     }
10 }

注意:upstream定义在http下!!!

 

三、upstream指令

1.分组的方法

upstream的作用是定义一组服务器,服务器可以在不同的监听端口,还可以混合使用侦听TCP和UNIX套接字的服务器。

官方文档示例:

1 upstream backend {
2     server backend1.example.com weight=5;
3     server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;
4     server unix:/tmp/backend3;
5 
6     server backup1.example.com  backup;
7 }

定义服务器address和其他parameters服务器。

该地址可以指定为域名或IP地址(IP:端口,如果不加端口,默认为80),也可以指定为 unix:”前缀后指定的UNIX域套接字路径。

2.负载均衡的算法

①WRR(WeightedRound-Robin)

在示例配置中,每10个请求将如下分配:向192.168.1.1:8080发送8个请求,向www.fengcc.club发送2个请求。如果在与upstream定义的服务器通讯时发生错误,那么nginx会把请求传递给下一个服务器。

配置权重的方法是定义关键字"weight"

weight = number
注:如果不定义,默认值为1

 

②RR(Round-Robin)

Nginx默认使用的负载均衡算法是加权轮询算法,依次将请求分配到各个后台服务器中,适用于后台机器性能一致的情况。 

挂掉的机器可以自动从服务列表中剔除。

示例:
upstream rr {
    192.168.1.1;
    192.168.1.2;
    192.168.1.3;
    192.168.1.4;
}

 

③ip_hash

在该方法中,请求将基于客户端IP地址在服务器之间分配。客户端IPv4地址的前三个八位位组或整个IPv6地址用作哈希密钥。该方法确保了来自同一客户端的请求将始终传递到同一服务器,除非该服务器不可用。

如果客户请求的此太服务器不可用,则该请求将传递到另一台服务器。

在一定情况下,用户永远请求的是同一台服务器。

示例:
upstream rr {
    ip_hash;
    192.168.1.1;
    192.168.1.2;
    192.168.1.3;
}

 

四、proxy模块介绍

ngx_http_proxy_module模块允许将请求传递到另一台服务器。

 

五、示例

1 location  / {
2     proxy_pass http://feng;
3     proxy_set_header Host $host;
4     proxy_set_header X-Real-IP $remote_addr;
5 }

 

六、proxy_pass的使用

1.proxy_pass的语法

“proxy_pass URL” 设置代理服务器的协议和地址。

作为协议,可以指定为“http”或“https”。

作为URL,可以指定为域名或IP地址+端口。

1 proxy_pass http://www.feng.club;
2 
3 proxy_pass http://192.168.1.1:8080;
4 
5 proxy_pass http://localhost:8080/url/;
6 
7 proxy_pass http:// unix:/tmp/backend.socket:/ uri /;

2.URI请求传递给服务器的两种方法

proxy_pass使用URI指定伪指令,那么当请求传递到服务器时, 与该位置匹配的请求URI 的一部分 将被伪指令中指定的URI代替:

1 location / {
2     proxy_pass http://127.0.0.1/remote/;
3 }

proxy_pass指定为不带URI,在处理更改的URI时传递完整的规范化请求URI。

location /some/path/ {
    proxy_pass http://127.0.0.1;
}

  

3.无法确定请求URI中要替换的部分

location /name/ {
    proxy_pass http://127.0.0.1$request_uri;
}

在这种情况下,如果在指令中指定了URI,它将照原样传递到服务器,从而替换原始请求URI。

 

 

七、proxy_set_header的使用

允许在传递给代理服务器的请求标头中重新定义或附加字段 。该可以是文本,变量,以及它们的组合 默认情况下,仅重新定义两个字段:

1 proxy_set_header Host       $proxy_host;
2 proxy_set_header Connection close;

如果启用了缓存,则标题栏中的字段“ If-Modified-Since”,“ If-Unmodified-Since”,“ If-None-Match”,“ If-Match”,“ Range”和“ If-Range”原始请求不会传递到代理服务器。

 

八、简单示例:把自己的域名代理到淘宝(访问自己的域名跳转到淘宝)

Nginx配置如下:

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    gzip  on;
    upstream taobao {
        server www.taobao.com:443;
    }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/nginx/html;
        }

        ssl_certificate      XXXXX.crt;
        ssl_certificate_key  XXXXX.key;


        server_name  XXXX你的域名;


        location / {
            proxy_pass https://taobao;
            proxy_set_header Host www.taobao.com;
        }
}

 

 

 

 

 

 

 

posted @ 2019-12-01 23:00  better_feng  阅读(9555)  评论(0编辑  收藏  举报