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(系統已經安裝過的)
實驗原理圖:
說明:nginx伺服器有2個網路卡ip分別是61.1.1.2和192.168.1.2
Squid伺服器的IP是192.168.1.3
Apache伺服器的IP是192.168.1.4
Apache相對應的域名是:web.abc.com
Squid相對應的域名是:squid.abc.com
由於此實驗沒有搭建DNS所以需要在各伺服器的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 *
<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 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
相關文章
- Nginx 動靜分離與負載均衡的實現Nginx負載
- Nginx—tomcat負載均衡動靜分離群集NginxTomcat負載
- nginx面試題-nginx負載均衡與正反向代理Nginx面試題負載
- Nginx作為動靜分離、快取與負載均衡初探Nginx快取負載
- Nginx使用篇:實現負載均衡、限流與動靜分離Nginx負載
- 介紹下Nginx 反向代理與負載均衡Nginx負載
- 《Nginx系列》之青銅入門篇 反向代理 負載均衡 動靜分離就是這麼簡單Nginx負載
- Nginx + Tomcat 動靜分離實現負載均衡NginxTomcat負載
- 【Nginx】Nginx反向代理和負載均衡部署Nginx負載
- nginx詳解反向代理負載均衡Nginx負載
- Nginx負載均衡反向代理伺服器Nginx負載伺服器
- [原創]Nginx反向代理及負載均衡Nginx負載
- Nginx伺服器的使用與反向代理負載均衡Nginx伺服器負載
- 秒懂負載均衡與反向代理負載
- Nginx入門(2)反向代理和負載均衡Nginx負載
- Nginx反向代理負載均衡的容器化部署Nginx負載
- docker下nginx反向代理和負載均衡配置DockerNginx負載
- nginx+tomcat反向代理負載均衡配置NginxTomcat負載
- nginx配置web服務|反向代理|負載均衡NginxWeb負載
- nginx反向代理目錄及動靜分離公羊seoNginx
- Nginx+Tomcat負載均衡,動靜分離群集部署解析NginxTomcat負載
- 代理與反向代理、負載均衡和快取負載快取
- nginx反向代理和負載均衡策略實戰案例Nginx負載
- nginx+tomcat實現反向代理+負載均衡NginxTomcat負載
- 帶你瞭解Nginx+Tomcat負載均衡,動靜分離群集NginxTomcat負載
- Nginx+Tomcat實現負載均衡、動靜分離叢集部署NginxTomcat負載
- Nginx+Tomcat+Keepalived+Memcache 負載均衡動靜分離技術NginxTomcat負載
- Nginx 高階篇(一)反向代理實現動靜分離Nginx
- 誰說前端不需要懂-Nginx反向代理與負載均衡前端Nginx負載
- 圖解Nginx,系統架構演變 + Nginx反向代理與負載均衡圖解Nginx架構負載
- Tomcat+Nginx實現動靜分離和負載均衡架構部署TomcatNginx負載架構
- 伺服器群集—Nginx+Tomcat+keepalived負載均衡、動靜分離群集伺服器NginxTomcat負載
- Linux下Nginx+Tomcat負載均衡和動靜分離配置要點LinuxNginxTomcat負載
- Nginx代理功能與負載均衡詳解Nginx負載
- centos7下配置nginx反向代理負載均衡叢集CentOSNginx負載
- Nginx反向代理+負載均衡簡單實現(https方式)Nginx負載HTTP
- Nginx搭建反向代理負載均衡和web快取伺服器Nginx負載Web快取伺服器
- 循序漸進nginx(二):反向代理、負載均衡、快取服務、靜態資源訪問Nginx負載快取