nginx反向代理负载均衡与动静页面分离

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

实验所需的软件包:

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;           //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
c d 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
相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
21天前
|
应用服务中间件 nginx
【报错】在nginx下启动,登录成功后页面不跳转
【报错】在nginx下启动,登录成功后页面不跳转
|
24天前
|
运维 应用服务中间件 Linux
LNMP详解(十三)——Nginx子页面详解
LNMP详解(十三)——Nginx子页面详解
16 3
|
23天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
35 0
|
1月前
|
负载均衡 应用服务中间件 API
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
43 4
|
9天前
|
负载均衡 算法 网络协议
LVS、Nginx和HAProxy负载均衡器对比总结
LVS、Nginx和HAProxy负载均衡器对比总结
|
13天前
|
负载均衡 应用服务中间件 nginx
Nginx 负载均衡
Nginx 负载均衡
23 2
|
23天前
|
缓存 负载均衡 应用服务中间件
nginx的各种负载均衡策略与各种负载均衡策略如何配置
Nginx支持多种负载均衡策略,如轮询、加权轮询、IP哈希、最少连接、URL哈希和fair策略。轮询是默认策略,每个请求按顺序分发;加权轮询根据权重分配请求;IP哈希确保相同IP的请求始终发送到同一服务器;最少连接将请求发送给连接数最少的服务器;URL哈希(需额外工具或模块)和fair策略则依据URL和响应时间分配请求。配置变更需更新nginx.conf并重新加载或重启服务,具体配置应参照官方文档。
40 0
|
24天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
26 0
|
2月前
|
负载均衡 应用服务中间件 Linux
|
3月前
|
tengine Rust 负载均衡
反向代理学习笔记(一) Nginx与反向代理绪论
反向代理学习笔记(一) Nginx与反向代理绪论