您的位置:首页 > 运维架构 > 反向代理

nginx的反向代理搭建配置以及搭建过程中的一些思维发散

2015-01-16 17:35 393 查看
nginx的反向代理搭建配置以及搭建过程中的一些思维发散
2015-01-12 22:12:30
标签:nginx 反向代理搭建配置
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。/article/4540888.html
首先来介绍下nginx的反向代理。
代理服务器一般分为正向代理(通常直接称为代理服务器)和反向代理。画个图我们就好理解了。
正向代理:可以想象成是路由器,我们要通过它来上网的那种。(可以说是客户端的代理)

反向代理:客户端的请求过来之后交给反向代理服务器,然后反向代理服务器再交给后台真实的服务器。(这个是服务器端的代理)

我们今天说的是nginx的反向代理功能的实现。同时,反向代理还可以实现负载均衡的功能。可以自己思考下。由于实验比较简单,这边环境就简单处理。http-server1:192.168.10.156(Apache服务)http-server2:192.168.10.157(nginx服务)nginx-proxy:192.168.10.159

保证http服务器搭建完成,并能正常访问nginx-proxy安装(其他依赖条件自己安装)。版本:nginx-1.4.7.tar.gztar -zxvf nginx-1.4.7.tar.gz /home./configure --prefix=/usr/local/nginx \--sbin-path=/usr/local/bin \--conf-path=/etc \--error-log-path=/usr/local/nginx/error.log \--pid-path=/usr/local/nginx/ngnix.pid \--lock-path=/usr/local/nginx/nginx.lock \--with-http_ssl_module \--with-http_gzip_static_module \--with-http_perl_module \--http-log-path=/usr/local/nginx/access.log \--http-fastcgi-temp-path=/usr/local/nginx/html \--with-pcre --with-zlib=/usr --with-openssl=/usrmake && make install 编辑nginx的配置文件。vim /etc/nginx.conf#user nobody;
worker_processes 1;
events { worker_connections 1024;}

http { upstream loadbance { server 192.168.10.156; server 192.168.10.157;(如果其其他端口,后面请加端口号:X.X.X.X:8888) } server { listen 80; location / { proxy_pass http://loadbance; } } }

红色部分为搭建反向代理服务器所需要的简单配置。(注意,nginx行尾的分号)
upstream:反向代理的关键字loadbance:你可以理解为后台一组服务器的名称server: 后台真实的服务器地址,可以是IP或者FQDN(如果是FQDN,别忘记解析)listen:监听的端口,如果没有把原来nginx做web服务器的配置内容注销掉,建议改为其他端口proxy_pass:后面跟http://loadbance 此一定要和upstream后面(服务器组名称)的名称保持一致
OK,大功告成。访问进行测试。访问代理服务器地址:http://192.168.10.159,然后刷新






这就是nginx的反向代理。其实我们还可以延伸下。nginx的反向代理功能我认为就可以当做一个简单的负载均衡来使用,我们可以指定nginx的调度算法:nginx的官网上说(其实个人认为不止4中,你可以在服务器的ip地址后面加上服务器的权值,但是按官网上加上权值是不算一种。)NGINX supports four load balancing methods:1):The round-robin method 轮询2):The least_conn method 最少连接数a request is sent to the server with the least number of active connections with server weights taken into consideration。请求会送到活跃链接最少的服务器上(服务器的权值要考虑进来的)3):The ip_hash method The method guarantees that requests from the same address get to the same server unless it is not available。这种方法保证了来自同一个IP地址的请求会得到同一个服务器的响应,除非挂了。(其是通过客户端的ip(IPV4 or IPV6)地址来计算出此IP的hash值的)4);The generic hash method: the server to which a request is sent is determined from a user-defined key which may be a text, variable, or their combination. For example, the key may be a source IP and port,请求会发送到一个用户定义键值(可以是text,变量,或者混合的)的服务器上
再来看一个官网说的:If a method other than the default one is used, the corresponding directive (least_conn,ip_hash or hash) should be specified inside the upstream before the server directives.如果你要使用其他的调度算法(默认使用的round-robin),相应的指令必须在upstream内部指定,并且要在server指令之前。比如:最小数链接/ip_hash/hash $request_uri consistent;(如果是使用轮询的方式的话,就不用写了,因为默认算法就是轮询)upstream loadbance { least_conn; //或者是ip_hash和(hash $request_uri consistent) server 192.168.10.156 weight=3; //可以设置权值的哦 server 192.168.10.157;(如果其其他端口,后面请加端口号:X.X.X.X:8888) }
哎呀,我擦,这就扯远了。其实,nginx还有一个健康检查的功能,如果有台服务器挂了,请求不会分发到该服务器上。比如下面的写法:upstream loadbance { server 192.168.10.156 weight=3; server 192.168.10.157; server 192.168.10.158 backup;//当备机使用
server 192.168.10.155 down; //如果你要临时移除一台服务器进行升级或者其他操作,可以把其标记为down的状态,这样请求就不会到其上。
}
其实,我们还可以通过server内部的location指令来指定不同的路径分发到不同的服务器上去。来实现分流。比如,可以定义多个upstream。upstream1,upstream2.......然后location /mp3 { proxy_pass http://upstream1; }
location /flv { proxy_pass http://upstream2; }
等等等等。location后面的路径你可以使用精确路径,通配符,正则表达式扥等。OK,到此为止。要不然就刹不住了,东西太多了。

本文出自 “linux菜鸟” 博客,请务必保留此出处/article/4540888.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: