世界杯直播平台网站优化二

作者: print("") 分类: linux,nginx 发布时间: 2018-06-17 01:28

世界杯直播平台网站优化二

我以为昨天优化到位了。结果4k在线人数就傻逼了。真的傻逼了。日

动不动 数据库连接数量就11w 或者20w 要不是内存是190G的内存就GG了

后面直接做了一个负载均衡。这个是入口服务器  配置如下

另外一个是从服务器

这个CMS 都是动态去获取数据库的所以对数据库特别大。所以后端加了memecached 缓存

但是缓存的效果不佳。我就想着。在前段加一层缓存  使用squid 缓存 看看效果

设置最大打开文件数量

ulimit -HSn 65535

为了重启能自动设置我就直接在rc.local 里面添加 

[root@localhost database]# cat /etc/rc.local 
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
ulimit -HSn 65535

touch /var/lock/subsys/local

安装squid =。= 我直接yum 安装就行了

[root@localhost database]# yum install squid
Loaded plugins: fastestmirror
Setting up Install Process
Determining fastest mirrors
epel/metalink                                                                                                                            | 7.4 kB     00:00     
 * base: centos.uhost.hk
 * epel: ftp.cuhk.edu.hk
 * extras: centos.uhost.hk
 * updates: centos.uhost.hk
base                                                                                                                                     | 3.7 kB     00:00     
epel                                                                                                                                     | 4.7 kB     00:00     
http://ftp.cuhk.edu.hk/pub/linux/fedora-epel/6/x86_64/repodata/repomd.xml: [Errno -1] repomd.xml does not match metalink for epel
Trying other mirror.
epel                                                                                                                                     | 3.2 kB     00:00     
epel/primary                                                                                                                             | 3.2 MB     00:00     
epel                                                                                                                                                12514/12514
extras                                                                                                                                   | 3.4 kB     00:00     
updates                                                                                                                                  | 3.4 kB     00:00     
Resolving Dependencies
--> Running transaction check
---> Package squid.x86_64 7:3.1.23-24.el6 will be installed
--> Processing Dependency: perl(DBI) for package: 7:squid-3.1.23-24.el6.x86_64
--> Running transaction check
---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================================
 Package                               Arch                                Version                                      Repository                         Size
================================================================================================================================================================
Installing:
 squid                                 x86_64                              7:3.1.23-24.el6                              base                              1.8 M
Installing for dependencies:
 perl-DBI                              x86_64                              1.609-4.el6                                  base                              705 k

Transaction Summary
================================================================================================================================================================
Install       2 Package(s)

Total download size: 2.5 M
Installed size: 8.0 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): perl-DBI-1.609-4.el6.x86_64.rpm                                                                                                   | 705 kB     00:00     

(2/2): squid-3.1.23-24.el6.x86_64.rpm                                                                                                    | 1.8 MB     00:00     
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                           4.6 MB/s | 2.5 MB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : perl-DBI-1.609-4.el6.x86_64                                                                                                                  1/2 
  Installing : 7:squid-3.1.23-24.el6.x86_64                                                                                                                 2/2 
  Verifying  : 7:squid-3.1.23-24.el6.x86_64                                                                                                                 1/2 
  Verifying  : perl-DBI-1.609-4.el6.x86_64                                                                                                                  2/2 

Installed:
  squid.x86_64 7:3.1.23-24.el6                                                                                                                                  

Dependency Installed:
  perl-DBI.x86_64 0:1.609-4.el6                                                                                                                                 

Complete!

唔、清空配置

mkdir /opt/squid_cache  # 创建缓存目录
mkdir /opt/squid_cache  # 创建缓存目录
chown -R squid.squid /opt/squid_cache  # 更改属主、属组
 echo "">/etc/squid/squid.conf  # 清空默认配置文件

配置文件如下:

[root@localhost database]# cat /etc/squid/squid.conf
cache_effective_user squid
cache_effective_group squid
http_port 0.0.0.0:8999 accel vhost
dns_nameservers 223.5.5.5
visible_hostname study.localhost.localdomain
cache_mgr 1249648969@qq.com
cache_mem 3000 MB
cache_dir ufs /opt/squid_cache 4096 16 256
maximum_object_size 4 MB
cache_access_log /var/log/squid/access.log
cache_log /var/log/squid/cache.log
cache_store_log /var/log/squid/store.log
logfile_rotate 7
cache_swap_high 90
cache_swap_low 80
cache_peer 45.114.168.242 parent 8989 0 originserver name=sina
cache_peer 160.124.255.102 parent 8989 0 originserver name=baidu

#cache_peer_domain sina 45.114.168.242
#cache_peer_domain baidu 45.114.168.242
http_access allow all

初始化squid’

[root@localhost database]# squid -z
2018/06/17 00:54:10| Creating Swap Directories
2018/06/17 00:54:10| /opt/squid_cache exists
2018/06/17 00:54:10| Making directories in /opt/squid_cache/00
2018/06/17 00:54:10| Making directories in /opt/squid_cache/01
2018/06/17 00:54:10| Making directories in /opt/squid_cache/02
2018/06/17 00:54:10| Making directories in /opt/squid_cache/03
2018/06/17 00:54:10| Making directories in /opt/squid_cache/04
2018/06/17 00:54:10| Making directories in /opt/squid_cache/05
2018/06/17 00:54:10| Making directories in /opt/squid_cache/06
2018/06/17 00:54:10| Making directories in /opt/squid_cache/07
2018/06/17 00:54:10| Making directories in /opt/squid_cache/08
2018/06/17 00:54:10| Making directories in /opt/squid_cache/09
2018/06/17 00:54:10| Making directories in /opt/squid_cache/0A
2018/06/17 00:54:10| Making directories in /opt/squid_cache/0B
2018/06/17 00:54:10| Making directories in /opt/squid_cache/0C
2018/06/17 00:54:10| Making directories in /opt/squid_cache/0D
2018/06/17 00:54:10| Making directories in /opt/squid_cache/0E
2018/06/17 00:54:10| Making directories in /opt/squid_cache/0F

启动squid

[root@localhost database]#  service squid start
Starting squid: .                                          [  OK  ]
[root@localhost database]# chkconfig --add squid
[root@localhost database]# chkconfig --level 35 squid on
[root@localhost database]# netstat -anpt | grep squid
tcp        0      0 0.0.0.0:8999                0.0.0.0:*                   LISTEN      43296/(squid)       

两台机器都这么操作。

然后在nginx 配置文件中修改一下端口就行了

upstream www.zqzb.cc
{
server 45.114.168.242:8999  weight=2;
server 160.124.255.102:8999  weight=2;
#ip_hash;
}

server
{
    listen  80;
    server_name  www.zqzb.cc;
 
    location / {
        proxy_pass        http://www.zqzb.cc;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

看看缓存效果吧

[root@localhost database]# more /var/log/squid/access.log 
1529168424.788    181 27.37.28.6 TCP_MISS/200 17723 GET http://160.124.255.102:8999/ - FIRST_UP_PARENT/sina text/html
1529168425.022      1 27.37.28.6 TCP_MISS/200 3979 GET http://160.124.255.102:8999/js/index.js? - FIRST_UP_PARENT/sina application/javascript
1529168425.026      0 27.37.28.6 TCP_MISS/200 491 GET http://160.124.255.102:8999/d/js/acmsd/thea1.js - FIRST_UP_PARENT/sina application/javascript
1529168425.194    179 27.37.28.6 TCP_MISS/200 11421 GET http://160.124.255.102:8999/css/flags.css - FIRST_UP_PARENT/sina text/css
1529168425.217    180 27.37.28.6 TCP_MISS/200 26911 GET http://160.124.255.102:8999/images/logo.png - FIRST_UP_PARENT/sina image/png
1529168425.220      1 27.37.28.6 TCP_MISS/200 2239 GET http://160.124.255.102:8999/js/time.js - FIRST_UP_PARENT/sina application/javascript
1529168425.386    554 27.37.28.6 TCP_MISS/200 59454 GET http://160.124.255.102:8999/css/style.css? - FIRST_UP_PARENT/sina text/css
1529168425.415      5 27.37.28.6 TCP_MISS/200 935 GET http://160.124.255.102:8999/js/ready.js - FIRST_UP_PARENT/sina application/javascript
1529168425.579      1 27.37.28.6 TCP_MISS/200 4532 GET http://160.124.255.102:8999/js/navbar.js - FIRST_UP_PARENT/sina application/javascript
1529168425.600      1 27.37.28.6 TCP_MISS/200 5894 GET http://160.124.255.102:8999/js/cmd.js - FIRST_UP_PARENT/sina application/javascript
1529168425.762      1 27.37.28.6 TCP_MISS/200 4639 GET http://160.124.255.102:8999/js/panel.js - FIRST_UP_PARENT/sina application/javascript
1529168425.768      1 27.37.28.6 TCP_MISS/200 3694 GET http://160.124.255.102:8999/js/commonzb.js - FIRST_UP_PARENT/sina application/javascript
1529168425.768      1 27.37.28.6 TCP_MISS/200 5635 GET http://160.124.255.102:8999/js/dialog.js - FIRST_UP_PARENT/sina application/javascript
1529168425.770      1 27.37.28.6 TCP_MISS/200 12193 GET http://160.124.255.102:8999/js/trans.js - FIRST_UP_PARENT/sina application/javascript
1529168425.832      0 27.37.28.6 TCP_MISS/200 1534 GET http://160.124.255.102:8999/images/tx.png - FIRST_UP_PARENT/sina image/png
1529168425.942      1 27.37.28.6 TCP_MISS/200 5600 GET http://160.124.255.102:8999/images/tub.png - FIRST_UP_PARENT/sina image/png
1529168425.946      0 27.37.28.6 TCP_MISS/200 1935 GET http://160.124.255.102:8999/css/zuqiu.gif - FIRST_UP_PARENT/sina image/gif
1529168425.950      0 27.37.28.6 TCP_MISS/200 827 GET http://160.124.255.102:8999/images/flags/fi.gif - FIRST_UP_PARENT/sina image/gif
1529168425.950      0 27.37.28.6 TCP_MISS/200 647 GET http://160.124.255.102:8999/css/live.gif - FIRST_UP_PARENT/sina image/gif
1529168426.021      1 27.37.28.6 TCP_MISS/200 822 GET http://160.124.255.102:8999/images/flags/jp.gif - FIRST_UP_PARENT/sina image/gif
1529168426.123      0 27.37.28.6 TCP_MISS/200 818 GET http://160.124.255.102:8999/images/flags/ec.gif - FIRST_UP_PARENT/sina image/gif
1529168426.133      0 27.37.28.6 TCP_MISS/200 823 GET http://160.124.255.102:8999/images/flags/br.gif - FIRST_UP_PARENT/sina image/gif
1529168426.139      0 27.37.28.6 TCP_MISS/200 1995 GET http://160.124.255.102:8999/css/lanqiu.gif - FIRST_UP_PARENT/sina image/gif
1529168426.141      0 27.37.28.6 TCP_MISS/200 827 GET http://160.124.255.102:8999/images/flags/ie.gif - FIRST_UP_PARENT/sina image/gif
1529168426.143      0 27.37.28.6 TCP_MISS/200 1004 GET http://160.124.255.102:8999/images/flags/sjb.png - FIRST_UP_PARENT/sina image/png
1529168426.209      1 27.37.28.6 TCP_MISS/200 5368 GET http://160.124.255.102:8999/images/r_fk.png - FIRST_UP_PARENT/sina image/pn

## TCP_MISS 代表没有被 Squid 缓存,而是从原始服务器取出的并返回给用户的数据

[root@localhost database]# cat /var/log/squid/access.log | grep TCP_MEM_HIT
1529168870.704      0 160.124.255.102 TCP_MEM_HIT/200 984 GET http://www.zqzb.cc/t.html? - NONE/- text/html

1529168871.133      0 160.124.255.102 TCP_MEM_HIT/200 4685 GET http://www.zqzb.cc/favicon.ico - NONE/- image/x-icon
1529168871.190      0 160.124.255.102 TCP_MEM_HIT/200 1783 GET http://www.zqzb.cc/chatroom.html - NONE/- text/html
1529168871.215      0 160.124.255.102 TCP_MEM_HIT/200 83315 GET http://www.zqzb.cc/ - NONE/- text/html
1529168871.683      0 160.124.255.102 TCP_MEM_HIT/200 4685 GET http://www.zqzb.cc/favicon.ico - NONE/- image/x-icon
1529168872.199      0 160.124.255.102 TCP_MEM_HIT/200 1783 GET http://www.zqzb.cc/chatroom.html - NONE/- text/html
1529168872.226      0 160.124.255.102 TCP_MEM_HIT/200 17729 GET http://www.zqzb.cc/ - NONE/- text/html
1529168872.420      0 160.124.255.102 TCP_MEM_HIT/200 1157 GET http://www.zqzb.cc/tiantian1.html? - NONE/- text/html
1529168872.520      0 160.124.255.102 TCP_MEM_HIT/200 1783 GET http://www.zqzb.cc/chatroom.html - NONE/- text/html
1529168872.632      0 160.124.255.102 TCP_MEM_HIT/200 984 GET http://www.zqzb.cc/t.html? - NONE/- text/html
1529168872.701      0 160.124.255.102 TCP_MEM_HIT/200 1783 GET http://www.zqzb.cc/chatroom.html - NONE/- text/html
1529168872.859      0 160.124.255.102 TCP_MEM_HIT/200 1157 GET http://www.zqzb.cc/tiantian1.html? - NONE/- text/html
1529168873.067      0 160.124.255.102 TCP_MEM_HIT/200 83316 GET http://www.zqzb.cc/ - NONE/- text/html
1529168873.143      0 160.124.255.102 TCP_MEM_HIT/200 17729 GET http://www.zqzb.cc/ - NONE/- text/html
1529168873.151      0 160.124.255.102 TCP_MEM_HIT/200 4685 GET http://www.zqzb.cc/favicon.ico - NONE/- image/x-icon
1529168873.211      0 160.124.255.102 TCP_MEM_HIT/200 83316 GET http://www.zqzb.cc/ - NONE/- text/html
1529168873.571      0 160.124.255.102 TCP_MEM_HIT/200 17728 GET http://www.zqzb.cc/ - NONE/- text/html
1529168873.852      0 160.124.255.102 TCP_MEM_HIT/200 1157 GET http://www.zqzb.cc/tiantian1.html? - NONE/- text/html
1529168874.096      0 160.124.255.102 TCP_MEM_HIT/200 17729 GET http://www.zqzb.cc/ - NONE/- text/html
1529168874.213      0 160.124.255.102 TCP_MEM_HIT/200 83316 GET http://www.zqzb.cc/ - NONE/- text/html
1529168874.638      0 160.124.255.102 TCP_MEM_HIT/200 1783 GET http://www.zqzb.cc/chatroom.html - NONE/- text/html
1529168874.841      0 160.124.255.102 TCP_MEM_HIT/200 17728 GET http://www.zqzb.cc/ - NONE/- text/html
1529168874.911      0 160.124.255.102 TCP_MEM_HIT/200 4685 GET http://www.zqzb.cc/favicon.ico - NONE/- image/x-icon
1529168875.016      0 160.124.255.102 TCP_MEM_HIT/200 17728 GET http://www.zqzb.cc/ - NONE/- text/html
1529168875.069      0 160.124.255.102 TCP_MEM_HIT/200 83316 GET http://www.zqzb.cc/ - NONE/- text/html

## TCP_MEM_HIT 代表被 Squid 缓存到内存中并返回给用户的数据

[root@localhost database]# cat /var/log/squid/access.log | grep TCP_HIT

## TCP_HIT 代表被 Squid 缓存到 cache 目录(磁盘)中并返回给用户的数据

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

2条评论
  • xc

    2018年6月19日 下午6:21

    要是缓存出问题。不就是要爆炸?

  • zqzb

    2018年6月25日 下午4:27

    直接做页面缓存吧

发表评论

电子邮件地址不会被公开。 必填项已用*标注