nginx篇三

来源:互联网 发布:手机检测软件 编辑:程序博客网 时间:2024/05/17 22:56

nginx篇三

nginx可以作为反向代理服务器,并且可以实现负载均衡而且是应用层的负载,下面用一个简单的例子来介绍下。

现在有一个client,IP地址为192.168.5.100。代理服务器网卡一的IP地址为192.168.5.1负责接收客户端的请求,网卡二的IP地址为172.16.5.1负责调度上游的服务器。上游的两台服务器的IP地址172.16.5.2(负责news部分),172.16.5.3(负责sports部分)。开始说明,

拓扑图如下:


1、  client端可以用自己的物理机进行模拟,配置好IP地址,网关,dns就可以了。

2、  NGinx的配置

①DNS的搭建

yum –y install bind

②修改配置文件

options {

//      listen-on port 53 { 127.0.0.1; };

//      listen-on-v6 port 53 { ::1; };

        directory       "/var/named";

        dump-file       "/var/named/data/cache_dump.db";

        statistics-file"/var/named/data/named_stats.txt";

        memstatistics-file"/var/named/data/named_mem_stats.txt";

//      allow-query     { localhost; };

 

zone"hello.com" IN {

        type master;

        file "hello.com.zone";

};

 

③建立区域文件数据库

vi /var/named/hello.com.zone

@       IN      SOA    ns.hello.com.     root.hello.com.(

        20151103

        28800

        14400

        3600000

        86400

 

)

@        IN      NS     ns.hello.com.

ns        IN     A       192.168.5.1

www      IN      A      192.168.5.1

   DNS搭建好之后,启动DNS服务,并且在Nginx中进行配置文件的修改

3、  Nginx配置文件的修改

vi /etc/nginx/nginx.conf

server {

    listen 80;

           server_namewww.hello.com;                 

}

location / {

           root/web/www/;

           indexindex.html index.htm;

}

location /news {

           proxy_pass http://172.16.5.2/news;

           proxy_set_headerX-Real-IP  $remote_addr;

}

location /sports {

           proxy_pass http://172.16.5.3/sports;

           proxy_set_headerX-Real-IP  $remote_addr;

}

Nginx中的大体的配置就是这些,但是我们要注意的是把Linux主机的路由的转发的功能打开。

echo 1 >/proc/sys/net/ipv4/ip_forward

重新加载配置文件

/usr/sbin/nginx –s reload

4、  两台Apache服务器的配置很简单,只要建立好相应的news和sports目录,然后把httpd服务打开,另外改下配置文件中,日志的记录格式就可以了。

vi /etc/httpd/conf/httpd.conf

LogFormat "%h %l %u %t \"%r\" %>s %b\"%{Referer}i\" \"%{User-Agent}i\"" combined改为:

LogFormat "%{X-Real-IP}i\ %l %u %t \"%r\"%>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined这样客户端访问的时候访问日志中记录的就是客户端的真是的IP地址,而不是代理服务器的IP地址。

然后重新加载Apache的配置,就可以在浏览器里面进行测试了。本测试实现的是单台服务器作为一个节点。下面的博客将介绍不同功能服务器集群实现负载均衡和反向代理。

0 0
原创粉丝点击