阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

123次阅读
没有评论

共计 4060 个字符,预计需要花费 11 分钟才能阅读完成。

首先来介绍下 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 http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

————————————– 分割线 ————————————–

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

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计4060字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中