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

RHEL6.4 搭建Nginx反向代理服务器

130次阅读
没有评论

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

实验需求:使用 nginx 搭建反向代理服务器,把用户的请求分发给后端的 web 服务器组 192.168.100.1 和 192.168.100.2

 内网 web 服务器 192.168.100.1          内网接口 eth0(192.168.1.254)

      ———– nginx 反向代理服务器 ———— 公网客户端 1.1.1.1

 内网 web 服务器 192.168.100.2          公网接口 eth1(1.1.1.254)

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/2012-08/69151.htm

一. 部署内网的网站服务器 192.168.100.1

可以使用 apache 或 nginx 等软件搭建,此实验采用 nginx 搭建

1. 安装软件并编辑配置文件

# vim /usr/local/nginx/conf/nginx.conf

http {

  ……

    server {

        listen      80;         

        server_name  www.linuxidc.com

 

        location / {

            root  html;

            index  index.html index.htm;

        }

 

    }

  ……

2. 制作测试网页文件

# echo 192.168.100.1 > /usr/local/nginx/html/index.html

3. 启动服务

# cd /usr/local/nginx/sbin

# ./nginx

二. 部署内网的网站服务器 192.168.100.2

1. 安装软件并编辑配置文件

# vim /usr/local/nginx/conf/nginx.conf

http {

  ……

    server {

        listen      80;

        server_name  www.linuxidc.com           

        location / {

            root  html;

            index  index.html index.htm;

        }

 

    }

  ……

2. 制作测试网页文件

# echo 192.168.100.2 > /usr/local/nginx/html/index.html  // 为了测试效果,2 台服务器上创建不同的网页

3. 启动服务

# cd /usr/local/nginx/sbin

# ./nginx

三. 配置 nginx 反向代理服务器

1. 安装软件并编辑配置文件

# vim /usr/local/nginx/conf/nginx.conf

……

http {

    ……

    upstream sergrp {// 定义源服务器组

        server 192.168.100.1:80;

        server 192.168.100.2:80;

    }

    server {

        listen      80;

        server_name  www.linuxidc.com;  //web 主机名

 

        location / {

            root  html;

            index  index.html index.htm;

            proxy_pass http://sergrp;      // 调用服务器组

    ……

2. 释放 80 端口并启动服务

# service httpd stop              // 本服务器若已启动 web 服务则关闭,或将其开启在别的端口

# chkconfig httpd off

# cd /usr/local/nginx/sbin

# ./nginx

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-05/102219p2.htm

三. 客户端 1.1.1.1 测试

# vim /etc/hosts          // 通过 DNS 解析或在 hosts 文件里指定

1.1.1.254 www.linuxidc.com

# elinks -dump http://www.linuxidc.com

  192.168.100.2

# elinks -dump http://www.linuxidc.com

  192.168.100.1

# elinks -dump http://www.linuxidc.com

  192.168.100.2

# elinks -dump http://www.linuxidc.com

  192.168.100.1

测试结果:nginx 默认采用轮询的分发策略,默认不缓存数据。

四.nginx 代理服务器分发请求的方式

轮询(默认的):每个请求按时间顺序逐一分配到不同的后端服务器,如果后断服务器 down 掉能自动剔除

Weight:指定轮询几率, 权重和访问比率成正比 通常用于后端服务器性能不同的情况默认值为 1

ip_hash:每个请求按访问 ip 的 hash 结果分配,这样可以让每个访客固定访问一个后端服务器,可以解决 session 的问题

Fair:按后端服务器的响应时间来分配请求,响应时间短的优先分配(nignx 默认不支持此方式,需要安装第三方软件)

五. 设置后端服务器的状态

down : 表示当前的 server 暂时不参与负载

max_fails:允许请求失败的次数,默认为 1,当超过最大次数时,返回 proxy_next_upstream 模块定义的错误

fail_timeout:max_fails 次失败后,暂停提供服务的时间

backup:其他所有的非 backup 机器 down 或者忙的时候 请求会发给 backup 机器 所有这台机器压力会最轻

六. 设置服务器组状态

upstream  sergrp  {

        #ip_hash;

        #server 192.168.100.1:80  weight=2;

        server 192.168.100.2:80  down;

        server 192.168.100.3:80;

        server 192.168.100.4:80  backup;

        server 192.168.100.5:80  max_fails=2  fail_timeout=30;

}

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

实验需求:使用 nginx 搭建反向代理服务器,把用户的请求分发给后端的 web 服务器组 192.168.100.1 和 192.168.100.2

 内网 web 服务器 192.168.100.1          内网接口 eth0(192.168.1.254)

      ———– nginx 反向代理服务器 ———— 公网客户端 1.1.1.1

 内网 web 服务器 192.168.100.2          公网接口 eth1(1.1.1.254)

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/2012-08/69151.htm

一. 部署内网的网站服务器 192.168.100.1

可以使用 apache 或 nginx 等软件搭建,此实验采用 nginx 搭建

1. 安装软件并编辑配置文件

# vim /usr/local/nginx/conf/nginx.conf

http {

  ……

    server {

        listen      80;         

        server_name  www.linuxidc.com

 

        location / {

            root  html;

            index  index.html index.htm;

        }

 

    }

  ……

2. 制作测试网页文件

# echo 192.168.100.1 > /usr/local/nginx/html/index.html

3. 启动服务

# cd /usr/local/nginx/sbin

# ./nginx

二. 部署内网的网站服务器 192.168.100.2

1. 安装软件并编辑配置文件

# vim /usr/local/nginx/conf/nginx.conf

http {

  ……

    server {

        listen      80;

        server_name  www.linuxidc.com           

        location / {

            root  html;

            index  index.html index.htm;

        }

 

    }

  ……

2. 制作测试网页文件

# echo 192.168.100.2 > /usr/local/nginx/html/index.html  // 为了测试效果,2 台服务器上创建不同的网页

3. 启动服务

# cd /usr/local/nginx/sbin

# ./nginx

三. 配置 nginx 反向代理服务器

1. 安装软件并编辑配置文件

# vim /usr/local/nginx/conf/nginx.conf

……

http {

    ……

    upstream sergrp {// 定义源服务器组

        server 192.168.100.1:80;

        server 192.168.100.2:80;

    }

    server {

        listen      80;

        server_name  www.linuxidc.com;  //web 主机名

 

        location / {

            root  html;

            index  index.html index.htm;

            proxy_pass http://sergrp;      // 调用服务器组

    ……

2. 释放 80 端口并启动服务

# service httpd stop              // 本服务器若已启动 web 服务则关闭,或将其开启在别的端口

# chkconfig httpd off

# cd /usr/local/nginx/sbin

# ./nginx

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2014-05/102219p2.htm

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