nginx反向代理負載均衡與動靜頁面分離

科技小能手發表於2017-11-08

實驗所需的軟體包:

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(系統已經安裝過的)
 
實驗原理圖:
 
 


說明:nginx伺服器有2個網路卡ip分別是61.1.1.2192.168.1.2
Squid伺服器的IP192.168.1.3
Apache伺服器的IP192.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.comhosts檔案內容是
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;          //apacheIP以及開放埠
}
 
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 *
 
<VirtualHost *>
DocumentRoot “/usr/local/apache2/htdocs/”
ServerName web.abc.com
</VirtualHost>
 
安裝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,針對要訪問的主機使用主機頭,即通過主機頭來區分不同的主機;這也是配置透明代時必須要配置的.

本文轉自wiliiwin 51CTO部落格,原文連結:http://blog.51cto.com/wiliiwin/199186


相關文章