免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 786 | 回复: 0
打印 上一主题 下一主题

nginx反向代理负载均衡与动静页面分离 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-21 11:53 |只看该作者 |倒序浏览
nginx反向代理负载均衡与动静页面分离
nginx反向代理负载均衡与动静页面分离
实验所需的软件包:
nginx-0.5.33.tar.gz
pcre-7.2.tar.gz
httpd-2.2.6.tar.gz
mysql-5.1.22-rc-linux-i686-icc-glibc23.tar.gz
php-5.2.4.tar.bz2
squid-2.5.i386.rpm(系统已经安装过的)
实验原理图:

file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/moz-screenshot.jpg

说明:nginx服务器有2个网卡ip分别是61.1.1.2和192.168.1.2
Squid服务器的IP是192.168.1.3
Apache服务器的IP是192.168.1.4
Nginx相对应的域名是:
www.abc.com
Apache相对应的域名是:web.abc.com
Squid相对应的域名是:squid.abc.com

由于此实验没有搭建DNS所以需要在各服务器的hosts文件里面写上解析语句并且需要修改主机名

Nginx服务器的主机名是
www.abc.com,hosts
文件内容是
web.abc.com    192.168.1.4
squid.abc.com  192.168.1.3

squid服务器的主机名是squid.abc.com,hosts文件内容是
www.abc.com
    192.168.1.2
web.abc.com    192.168.1.4

apache服务器的主机名是web.abc.com ,hosts文件内容是
www.abc.com
    192.168.1.2
squid.abc.com  192.168.1.3

在nginx服务器上安装相应的服务:

安装nginx
tar zxvf pcre-7.2.tar.gz
cd pcre-7.2/
./configure

make && make install

tar –zxvf  nginx-0.5.35.tar.gz
cd nginx-0.5.35
./configure \
--user=nobody  \
--group=nobody \
--prefix=/usr/local/nginx \
--with-http_stub_status_module

make && make install

cp /home/sysadmin/tools/nginx/fcgi.conf  /usr/local/nginx/conf/
(此处的fcgi.conf从其他服务器获得)
cp spawn-cgi /usr/local/php/bin/
cp /home/sysadmin/tools/nginx/nginx     /etc/init.d/
chmod 755 /etc/init.d/nginx

把nginx加入到chkconfig
chkconfig --add nginx
chkconfig --level 3 nginx on
mkdir –p /var/log/nginx

安装php
tar –jxvf php-5.2.4.tar.bz2
cd php-5.2.4
./configure --prefix=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-gd --with-jpeg-dir=/usr/lib --enable-gd-native-ttf \
--with-zlib-dir=/usr/lib --with-png-dir=/usr/lib \
--with-freetype-dir=/usr/include/freetype2 --with-ttf \
--enable-sockets --enable-ftp --enable-mbstring \
--enable-fastcgi --enable-force-cgi-redirect

make && make install

cp php.ini-dist /usr/local/php/lib/php.ini

修改nginx配置文件:
在nginx配置文件里添加下面的代码:
   
    upstream web.abc.com {         //apache服务器的域名
   server 192.168.1.4:80;          //apache的IP以及开放端口
}

upstream squid.abc.com {           //squid代理服务器的域名
   server 192.168.1.3:3128;        //squid服务器的IP以及开放端口
}

server
      {
             listen       80;
             server_name  www.abc.com *.abc.com ;
             proxy_redirect off;
        
location   ~*   .*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$  
        {
                proxy_pass http://squid.abc.com;
        }
location   ~*   ^/view/(.*)$
        {
                proxy_pass http://squid.abc.com;
        }
location / {
        proxy_pass http://web.abc.com;
}
}
}


在apache上安装需要的服务:

安装httpd服务:
tar –zxvf  httpd-2.2.6.tar.gz
cd httpd-2.2.6

    ./configure --prefix=/usr/local/apache2 --enable-module=so --enable-rewrite
make && make install

#使httpd服务能被chkconfig管理
cp support/apachectl  /etc/init.d/httpd

#我们需要做些修改,使chkconfig能够识别此服务。
vi /etc/init.d/httpd
加入:

# Startup script for the Apache Web Server
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve
# HTML files and CGI.
# processname: httpd     
# pidfile: /usr/local/apache2/log/httpd.pid
# config: /usr/local/apache2/conf/httpd.conf

chmod 755 /etc/init.d/httpd
chkconfig --add httpd
chkconfig --level 3 httpd on
mkdir –p /var/log/httpd/access_log
service httpd start

在配置文件中添加下面的代码:

NameVirtualHost *

DocumentRoot "/usr/local/apache2/htdocs/"
ServerName web.abc.com

安装php:
cd php-5.2.4
./configure \
--prefix=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-gd --with-jpeg-dir=/usr/lib --enable-gd-native-ttf \
--with-zlib-dir=/usr/lib --with-png-dir=/usr/lib \
--with-freetype-dir=/usr/include/freetype2 --with-ttf \
--enable-sockets --enable-ftp --enable-mbstring

make && make install

#在httpd配置文件里加入,使apache支持php
AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps

#拷贝php配置文件到指定位置
cp php.ini-dist /usr/local/php/lib/php.ini


安装squid服务:

由于系统默认安装 了squid服务,所以这里不进行安装了。

在squid的配置文件的代码如下:

http_port 192.168.1.3:3128     //代理服务器IP以及端口

cache_mem 64  MB               //代理的大小

cache_dir ufs /var/spool/squid 100 16 256    //缓存的存放位置

cache_access_log /var/log/squid/access.log

cache_log /var/log/squid/cache.log

cache_store_log /var/log/squid/store.log

pid_filename /var/run/squid.pid
auth_param basic children 5
auth_param basic realm Squid proxy-caching web server
auth_param basic credentialsttl 2 hours
auth_param basic casesensitive off

refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern .               0       20%     4320

acl A dstdomain
www.abc.com
         //允许访问的域名策略
acl B dstdomain web.abc.com         //允许访问的域名策略

acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443 563
acl CONNECT method CONNECT

http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports


http_access allow localhost
http_access allow A                      //允许访问域名
http_access allow B                       //允许访问的域名

http_access deny all

http_reply_access allow all

icp_access allow allcoredump_dir /var/spool/squid

访问的规则:
httpd_accel_host 192.168.1.4        
httpd_accel_port 80
httpd_accel_single_host on
httpd_accel_with_proxy on
httpd_accel_uses_host_header off
cache_peer 192.168.1.4  parent 80 0 no-query originserver


如果是多个web服务器,配置文件的规则需要修改成下面的代码:
httpd_accel_host virtual     //由于要访问的外网主机有许多台,virtual,即为虚拟的主机,virtual指定了虚拟主机模式,采用这种模式时,squid就取取消了缓存及ICP功能.
httpd_accel_port 80        //被加速主机的端口
httpd_accel_with_proxy   on    //选项定义为on后,squid既是Web请求的加速器,又是缓存代理服务器
httpd_accel_uses_host_header   on     //选项定义为on后,针对要访问的主机使用主机头,即通过主机头来区分不同的主机;这也是配置透明代时必须要配置的.


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/41660/showart_1656853.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP