会话保持和动静分离

前言:

    前两篇博客说到了keepalived_nginx 和keepalived_tengine,实现discuz的负载均衡和高可用,但是咱们没有考虑到动态静态资源分离,和会话保持的问题,实际上每一个部分都应该作为单独一台服务器出现,但是由于还没有找到工作(有工作请联系我Q2137626),自己的机器性能也很有限,所以此处我们在HostDB上新增memcache服务作为会话保持服务器,用前端HostVS主机作为静态资源处理服务器;


    前段tengine在接受到客户端静态资源请求时,不向后代理,而是自身直接响应。

后端HostDB上搭建memcache作为会话保持服务器,但是此处我们我们不能使用discuz,因为它并没有是使用php自带的session机制。


HostDB 

创建phpwind数据库和用户

> create database phpwind;
Query OK, 1 row affected (0.00 sec)
> grant all on phpwind.* to phpwind@'%' identified by 'magedu';
Query OK, 0 rows affected (0.00 sec)
> flush privileges;
Query OK, 0 rows affected (0.00 sec)


使用nfs提供phpiwind代码文件

# mkdir /nfshare/phpwind
# unzip /tmp/phpwind_UTF8_8.7.1.zip -d /nfshare/phpwind/


安装并启用memcache

# yum install -y memcached
# systemctl restart memcached  <--在CentOS6中需要/etc/init.d/memcached start
正在启动 memcached:                                       [确定]
[root@34400575 ~]# ss -tnl | grep 11211
LISTEN     0      128                      :::11211                   :::*     
LISTEN     0      128                       *:11211                    *:*



HostRS1

挂在nfs并将discuz修改为phpwind应用

# mount -t nfs 10.0.0.202:/nfshare /htdocs
# chown -R apache:apache /htdocs/phpwind/
# vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
    ServerName admin.ws.com
    DocumentRoot "/htdocs/phpwind/upload/"
    <Directory "/htdocs/phpwind/upload/">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>


给php安装memcache扩展模块,并配置php的session存储在memcache中,随后重启httpd服务使其生效

# yum install -y php-memcache
# vim /etc/php.ini
session.save_handler = memcache
session.save_path = "tcp://10.0.0.202:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
# vim /etc/httpd/conf.d/php.conf
php_value session.save_handler "memcache"
php_value session.save_path "tcp://172.18.71.202:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

# systemctl restart httpd
# ss -tnl | grep 80
LISTEN     0      128         :::80                      :::*


使用http://10.0.0.101/install.php,安装phpwind应用   



HostRS2 做相同的配置


测试:

    使用admin用户登录/退出论坛,重复几次,然后登陆memcache查看get_hits缓存命中次数

# yum install -y telnet
# telnet 10.0.0.202 11211
Trying 10.0.0.202...
Connected to 172.18.71.202.
Escape character is '^]'.
stats
STAT pid 2472
STAT uptime 429
STAT time 1463791798
STAT version 1.4.4
STAT pointer_size 64
STAT rusage_user 0.005999
STAT rusage_system 0.016997
STAT curr_connections 12
STAT total_connections 13
STAT connection_structures 13
STAT cmd_get 12
STAT cmd_set 34
STAT cmd_flush 0
STAT get_hits 10
STAT get_misses 2
STAT delete_misses 0
STAT delete_hits 2
STAT incr_misses 3
STAT incr_hits 9
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 4235
STAT bytes_written 3899
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 363
STAT curr_items 2
STAT total_items 25
STAT evictions 0
END



动静分离:


HostVS

# vim /usr/local/nginx/conf/nginx.conf
http{
    ...
    # 上游服务器, 即后端RS服务器.
    upstream backend {
        server 10.0.0.101 weight=1;
        server 10.0.0.102 weight=1;
        # sorry_server
        server 10.0.0.111:8080 backup;
        server 10.0.0.112:8080 backup;

        # 健康检查
        check interval=3000 rise=1 fall=3 timeout=1000 type=http;
        check_http_send "HEAD / HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
    }

    server {
        listen 80;
        server_name localhost;
        charset utf-8;
        
        # 当nginx将php代码送至后端RS处理时请求头中的Host值会是backend.
        # php代码在RS上处理时,其内部代码会去请求图片/层叠样式表等静态资源以填充页面.
        # 而php代码去请求静态资源时使用的是如http://backend/xxxx.gif这样的url,自然是取不到的.
        # 所以我们要在nginx向后代理遇到Host为backend时,将其转换为127.0.0.1.
        
        set $my_host $http_host;
        if ($http_host = "backend") {
            set $my_host "127.0.0.1";
        }

        # php页面请求发往后端RS进行处理
        location ~* \.php$ {
            proxy_pass   http://backend;
            proxy_set_header Host $my_host;
        }
        
        
         # 其余内容本机处理
        location / {
            root /htdocs/phpwind/upload;
            index index.php index.html;
        }
    }


    server {
        listen 8080;
        server_name localhost;
        charset utf-8;

        root   /usr/local/nginx/html;
        index  index.html index.htm;

        # sorry_server仅提供主页面, 访问其它资源也转至主页面.
        location ~ .* {
            error_page  404  /;
        }
    }


Host VS2 做与上面一样的配置


测试验证:

    将前段HostVS1&&和HostVS2代码文件中的php文件删除,而后将后端HostRS1 &&2中的静态文件删除,由于我们是通过nfs挂载方式拿到的代码文件,如果直接删除,全局都会受到影响。所以我们需要将代码文件复制到各个节点。


HostVS1 && HostVS2

# cp -r /htdocs/phpwind/ /tmp/
# umount /htdocs/
# cp -r /tmp/phpwind/ /htdocs/
# find /htdocs/phpwind/upload/ -name "*.php" | xargs rm -f


HostRS1 && HostRS2

# cp -r /htdocs/phpwind/ /tmp/
# umount /htdocs/
# cp -r /tmp/phpwind/ /htdocs/
# 当然还有其它静态资源文件, 这里为了测试只需删除部分即可.
# find /htdocs/phpwind/upload/ -name "*.css" -or -name "*.jpg" | xargs rm -f

这样操作以后, 再来通过浏览器访问页面, 发现仍然正常. 说明确实可行, 动态资源和静态资源被分离了.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章