Nginx反向代理搭建配置及搭建过程一些思考


首先来介绍下Nginx的反向代理。代理服务器一般分为正向代理(通常直接称为代理服务器)和反向代理。

画个图我们就好理解了。

正向代理:可以想象成是路由器,我们要通过它来上网的那种。(可以说是客户端的代理)

Nginx反向代理搭建配置及搭建过程一些思考

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

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.gz

tar -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=/usr

make && 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的反向代理功能我认为就可以当做一个简单的负载均衡来使用,我们可以指定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,到此为止。要不然就刹不住了,东西太多了。

--------------------------------------分割线 --------------------------------------

CentOS 6.2实战部署Nginx+MySQL+PHP

使用Nginx搭建WEB服务器

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程

CentOS 6.3下Nginx性能调优

CentOS 6.3下配置Nginx加载ngx_pagespeed模块

CentOS 6.4安装配置Nginx+Pcre+php-fpm

Nginx安装配置使用详细笔记

Nginx日志过滤 使用ngx_log_if不记录特定日志

--------------------------------------分割线 --------------------------------------

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:

相关内容