Linux nginx服务之反向代理

                   Linux nginx服务之反向代理

Nginx服务之反向代理:

GSLBGlobal Service LB 全局服务负载均衡:

SLBService LB

应用程序发布:

灰度模型:

 

ngx_http_proxy_module模块:

 1proxy_pass URL;

Context: location, if in location, limit_except

 

注意:proxy_pass后面的路径不带uri时,其会将locationuri传递给后端主机;

server {

server_name HOSTNAME;

location /uri/ {

proxy_pass http://hos[:port];

}

  }

http://HOSTNAME/uri –> http://host/uri 

blob.png

 proxy_pass后面的路径是一个uri时,其会将locationuri替换为proxy_passuri

server {

server_name HOSTNAME;

location /uri/ {

proxy_pass  http://host/new_uri/;

}

}

http://HOSTNAME/uri/ –> http://host/new_uri/

blob.png

如果location定义其uri时使用了正则表达式的模式,则proxy_pass之后必须不能使用uri; 用户请求时传递的uri将直接附加代理到的服务的之后;

    server {

server_name HOSTNAME;

location ~|~* /uri/ {

proxy_pass http://host;

}

  }

http://HOSTNAME/uri/ –> http://host/uri/

blob.png

2proxy_set_header field value;

设定发往后端主机的请求报文的请求首部的值;Context: http, server, location

   proxy_set_header X-Real-IP  $remote_addr;

   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 示例:

   让后台realserver主机记录客户端访问真正的地址,而不是nginx反向代理服务器的IP

   nginx反向代理服务器上设置:

blob.png

blob.png

 在后台realserver httpd服务器上设置LogFormat 

blob.png

blob.png

3proxy_cache_path :只能用于http配置段中;

定义可用于proxy功能的缓存;Context: http

proxy_cache_path path[levels=levels]  keys_zone=name:size  [inactive=time] [max_size=size]  

4proxy_cache zone | off;

指明要调用的缓存,或关闭缓存机制;Context: http, server, location

 

5 proxy_cache_key string;

缓存中用于“键”的内容;

默认值:proxy_cache_key $scheme$proxy_host$request_uri;

 

6proxy_cache_valid [code …] time;

定义对特定响应码的响应内容的缓存时长;

 示例:

   nginx主配置文件中定义可用于proxy功能的缓存:

blob.png

nginx默认配置文件中指明要调用的缓存或关闭缓存:

blob.png

blob.png

7、proxy_cache_use_stale

  proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off …;

  当后端服务器出现故障时,后端服务器是何种故障才提供缓存中的内容响应给客户端。

8、proxy_cache_methods GET | HEAD | POST …;

   为那些请求方法去检查缓存:默认为GETHEADPOST没有什么意义

If the client request method is listed in this directive then the response will be cached. GETand HEADmethods are always added to the list, though it is recommended to specify them explicitly. 

9、proxy_hide_header field;

   指明要隐藏的head

By default, nginx does not pass the header fields Date, Server, X-Pad, and X-Accel-…from the response of a proxied server to a client. The proxy_hide_header directive sets additional fields that will not be passed.

10、proxy_connect_timeout time;

    设置代理连接时长,默认为60s,不能超过75s

Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.

 

ngx_http_headers_module模块

The ngx_http_headers_module module allows adding the Expiresand Cache-Controlheader fields, and arbitrary fields, to a response header.

向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值;

1、add_header name value [always];

   向响应报文中添加首部信息;

   添加自定义首部;

add_header X-Via  $server_addr;  显示代理服务器的IP地址;

add_header X-Accel $server_name; 显示代理服务器的主机名;

blob.png

blob.png

指明此次访问的网页是由哪个ip地址的服务器代理的。

blob.png

blob.png

2expires [modified] time;

expires epoch | max | off;

用于定义ExpireCache-Control首部的值;

ngx_http_upstream_module模块

   定义realserver服务组

   The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass directives.

1upstream name { … }

 定义后端服务器组,会引入一个新的上下文;Context: http

upstream httpdsrvs {

server …

server…

     }

 

2server address [parameters];

 upstream上下文中server成员,以及相关的参数;Context: upstream

      address的表示格式:

unix:/PATH/TO/SOME_SOCK_FILE

IP[:PORT]

HOSTNAME[:PORT]

blob.png

blob.png

nginx的默认配置文件中定义反向代理的后端服务器组的组名websrvs

blob.png

blob.png

 parameters(参数):

weight=number

权重,默认为1

max_fails=number

失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用;

            可以对后台服务器的健康状态做检测

fail_timeout=time

设置将服务器标记为不可用状态的超时时长;

max_conns

当前的服务器的最大并发连接数;

backup

将服务器标记为“备用”,即所有服务器均不可用时此服务器才启用;

down

标记为“不可用”;

blob.png

3least_conn;

最少连接调度算法,当server拥有不同的权重时其为wlc;

4 ip_hash;

源地址hash调度方法;

 

5、consistent_hash

   一致性hash调度;

 我们举个例子来说明一致性哈希的好处。

 假设后端集群包含三台缓存服务器,ABC

 请求r1r2落在A上。

 请求r3r4落在B上。

 请求r5r6落在C上。

 使用一致性哈希时,当缓存服务器B宕机时,r1/r2会仍然落在A上,r5/r6会仍然落在C 上,

 也就是说这两台服务器上的缓存都不会失效。r3/r4会被重新分配给A或者C,并产生回源。

 使用其它算法,当缓存服务器B宕机时,r1/r2不再落在A上,r5/r6不再落在C上了。

 也就是说ABC上的缓存都失效了,所有的请求都要回源。

blob.png

blob.png

5hash key [consistent];

基于指定的keyhash表来实现对请求的调度,此处的key可以直接文本、变量或二者的组合;

作用:将请求分类,同一类请求将发往同一个real server

If the consistent parameter is specified the ketama consistent hashing method will be used instead.

示例:

hash $request_uri consistent; 将来自同一uri的请求始终发往同一个real server

hash $remote_addr;将来自同一个客户端的IP地址请求始终发往同一个real server

blob.png

6、keepalive connections;

   定义nginx代理服务器和后端服务器的保持连接。

为每个worker进程保留的空闲的长连接数量;

 

nginx的其它的二次发行版:

  tengine

  OpenResty

ngx_stream_core_module模块 放置在main配置段中 core:核心 stream:流动

   模拟反代基于tcpudp的服务连接,即工作于传输层的反代或调度器;

   http同等级的配置段,stream要放置在main配置段中。

 

1stream { … }

定义stream相关的服务;Context:main

stream {

upstream sshsrvs {

server 192.168.22.2:22;

server 192.168.22.3:22;

least_conn;

     }

 

  server {

  listen 10.1.0.6:22022;

  proxy_pass sshsrvs;

}

}

blob.png

blob.png

注意:自定义stream基于tcpudp反向代理时,需要把nginx的主配置文件中的http配置段注释掉。

blob.png

blob.png

blob.png

2listen

listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];

 

memcached缓存服务器:

   缓存服务器:

 缓存:cache,无持久存储功能;

 bypass缓存

 k/v cache,仅支持存储流式化数据;

LiveJournal旗下的Danga Interactive研发,

    特性:

k/v cache:仅可序列化数据;存储项:k/v

        k:存放于内存的hash表中;(k是哈希的文件名)

        v:存放于内存中的数据;(v是数据)

智能性一半依赖于客户端(调用memcachedAPI开发程序),一半依赖于服务端

分布式缓存:互不通信的分布式集群;

分布式系统请求路由方法:取模法,一致性哈希算法;

算法复杂度:O(1)

           O1)就是说n很大的时候,复杂度基本就不增长了基本就是个常量c

清理过期缓存项:

缓存耗尽:LRU

缓存项过期:惰性清理机制

 

安装配置:

CentOS 7 base仓库直接提供:

监听的端口:11211/tcp, 11211/udp ,以tcp使用的最多。

    主程序:/usr/bin/memcached

配置文件:/etc/sysconfig/memcached  memcached的环境配置文件。

Unit Filememcached.service

         启动memcached

协议格式:memcached协议

文本格式

二进制格式:编码效率更高;

    

命令:

统计类:stats, stats items, stats slabs, stats sizes

存储类:set, add, replace, append, prepend

            set:表示key有值就修改,没有值就新创建。

            add:表示新创建key值。

            replace

            append:在已有key值之后附加新值。

            prepend:在已有key值的前面追加新值。

命令格式:<command name> <key> <flags> <exptime> <bytes>  

<cas unique>

检索类:get, delete, incr/decr

            get:获取key值数据。

            delete:删除数据。

            incr:把已有的值加1

            decr:把已有的值减1

blob.png

清空:flush_all:清空整个缓存。

blob.png

示例:

telnet> add KEY <flags> <expiretime> <bytes> \r

telnet> VALUE

blob.png

blob.png

blob.png

blob.png

blob.png

memcached程序的常用选项:

-m <num>Use <num> MB memory max to use for object storage; the default is 64 megabytes.

-c <num>Use <num> max simultaneous connections; the default is 1024.

-u <username>:以指定的用户身份来运行进程;

-l <ip_addr>:监听的IP地址,默认为本机所有地址;

-p <num>:监听的TCP端口, the default is port 11211.

-U <num>Listen on UDP port <num>, the default is port 11211, 0 is off.

-M:内存耗尽时,不执行LRU清理缓存,而是拒绝存入新的缓存项,直到有多余的空间可用时为止;

-f <factor>:增长因子;默认是1.25

-t <threads>:启动的用于响应用户请求的线程数;

 

查看memcached的增长因子过程:

blob.png

memcached默认没有认证机制,可借用于SASL进行认证;

SASLSimple Authentication Secure Layer

API:

php-pecl-memcache 可能需要依赖epel yum源进行安装。

php-pecl-memcached 可能需要依赖epel yum源进行安装。

python-memcached

libmemcached

libmemcached-devel

命令行工具:

   memcached-tool  SERVER:PORT  COMMAND

blob.png

实验测试:

  使用nginx反向代理,后端httpd realserver组:

  实验环境:

  测试客户机:

  client客户机:IP 192.168.3.7

  nginx反向代理服务器:

     配置两块网卡,网卡1ip192.168.3.5 网卡2192.168.22.1

  后台realserver配置:

  realserver 1httpd服务器:serverIP192.168.22.2  gateway192.168.22.1

     realserver1)中配置http服务器 upstream server 1

  realserver 2httpd服务器:serverIP192.168.22.3  gateway192.168.22.1

     realserver2)中配置http服务器 upstream server 2

 

1)realserver1)上搭建http服务:

blob.png

blob.png

2)realserver2)上搭建http服务和nginx服务器,nginx服务器监听8080端口:

blob.png

blob.png

  realserver2)上 配置nginx服务器监听8080端口:

配置默认主页为:

blob.png

blob.png

blob.png

blob.png

3)配置nginx反向代理服务器:

nginx的主配置文件中的http配置段中配置upstream 后端服务器组:

blob.png

 nginx的默认配置文件中定义反向代理的后端服务器组的组名websrvs

blob.png

blob.png

nginx配合完成之后使用nginx -t 检查一下语法是否正确;

如果语法没有错误就可以使用 nginx -s reload systemctl  restart nginx.service 重载或重启服务。

4)client 192.168.3.7 客户端 主机上测试请求后端realserver httpd服务器组:

blob.png

5)关闭 realserver1)和realserver2)上的httpd服务进行测试backup主机服务器:

 或是在/etc/nginx/nginx.conf的主配置文件中在upstream配置段中将realserver12标记为down

blob.png

blob.png

原创文章,作者:zhengyibo,如若转载,请注明出处:http://www.178linux.com/59610

(0)
zhengyibozhengyibo
上一篇 2016-11-18 18:29
下一篇 2016-11-18 19:01

相关推荐

  • Linux iptables配置应用

                    Linux iptables配置应用 概述:    Linux的防火墙体系主要工作在网络层,只对tcp/ip数据包实施过滤和限制,属于典型的包过滤防火墙(或称为网络层防火墙)。Linux的防火墙体系基于内核编码实现,因…

    系统运维 2016-11-18
  • Google 开源技术protobuf

    1.  Protobuf简介        protobuf是google提供的一个开源序列化框架,类似于XML,JSON这样的数据表示语言,其最大的特点是基于二进制,因此比传统的XML表示高效短小得多。虽然是二进制数据格式,但并没有因此变得复杂,开发人员通过按照一定的语法定义结构化的消息格式,然后送给命令行…

    系统运维 2015-04-04
  • 使用Storm实现实时大数据分析

    摘要:随着数据体积的越来越大,实时处理成为了许多机构需要面对的首要挑战。Shruthi Kumar和Siddharth Patankar在Dr.Dobb’s上结合了汽车超速监视,为我们演示了使用Storm进行实时大数据分析。CSDN在此编译、整理。 简单和明了,Storm让大数据分析变得轻松加愉快。 当今世界,公司的日常运营经常会生成TB级别的数据。数据来源…

    Linux干货 2015-04-04
  • 小巧精悍——常见文本处理工具用法及技巧总结

    小巧精悍——常见文本处理工具用法及技巧总结 Linux最重要的哲学思想就是:一切皆文件。可见文件及文件操作在Linux当中是多么的重要。在Linux系统中我们也会经常用到各种文本文件处理的操作,熟练使用这些小巧精悍的文本处理工具,在关键时刻往往展现非凡的神奇功效。常见文本处理小工具:cat、hexdump、nl、less、head、tail、cut、tr、r…

    Linux干货 2016-11-05
  • php5.4zend-opcache安装

    公司在做高并发压测,fastcgi,apc,ocache,opcache, 几经考虑,最终考虑用opcache,因为作为 zend-opcache合并为php5.5的分支版本,xcache的前途堪虑, 在环境的安装上竟然折腾了半个下午没有搞定,这个作下问题记录。 先说安装, http://php.net/manual/zh/opcache.installat…

    2015-04-20
  • 设计模式 ( 十七) 状态模式State(对象行为型)

    设计模式 ( 十七) 状态模式State(对象行为型) 1.概述 在软件开发过程中,应用程序可能会根据不同的情况作出不同的处理。最直接的解决方案是将这些所有可能发生的情况全都考虑到。然后使用if… ellse语句来做状态判断来进行不同情况的处理。但是对复杂状态的判断就显得“力不从心了”。随着增加新的状态或者修改一个状体(if else(或swit…

    Linux干货 2015-07-27

评论列表(1条)

  • Cornelia
    Cornelia 2017-04-24 06:10

    Sehr geehrte Damen und Herren,bitte tragen Sie mich in den Hydrogeit-Newsletter ein. Leider führt die Eigeninitiative unter Verwendung des vorgesehenen Formulars zu einem Dat.rbankfehleneBesten Dank!Mit freundlichen GrüßenHolger Krings