黑帽联盟

 找回密码
 会员注册
查看: 1269|回复: 0
打印 上一主题 下一主题

[基础服务] nginx+memcached提高网站访问速度

[复制链接]

852

主题

38

听众

3181

积分

管理员

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    郁闷
    2024-5-10 17:49
  • 签到天数: 1543 天

    [LV.Master]伴坛终老

    此架构用nginx+memcached来进行加速访问,这样的架构要比以前php+memcached要快好多倍,具体是多少倍不在本文的讨论之中.

      系统:centos 5.5

      需要的软件:memcached-1.4.5-1.el5.kb.i386.rpm
                 nginx-1.0.15.tar.gz
                 agentzh-memc-nginx-module-v0.13rc3-1-gee3fe43.tar.gz
                 agentzh-srcache-nginx-module-v0.13rc6-6-g01829d9.tar.gz
                 ngx_http_upstream_keepalive-d9ac9ad67f45.tar.gz (这些软件我都会提供给大家)

    1.安装memcached
    rpm -ivh memcached-1.4.5-1.el5.kb.i386.rpm
    我比较懒,不想编译安装memcached,如果有折腾帝想要折腾也可以去下memcached的tar.gz包来编译安装.
    启动memcached
    memcached -d -m 10 -u root -l 192.168.10.5 -p 11211 -c 256 -P /tmp/memcached.pid

    2.编译nginx并安装上第3方模块包
    ./configure --user=nginx --group=nginx --add-module=../agentzh-memc-nginx-module-ee3fe43 \
    --add-module=../agentzh-srcache-nginx-module-01829d9 --add-module=../ngx_http_upstream_keepalive-d9ac9ad67f45  \
    --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf  \
    --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log \
    --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
    --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_secure_link_module \
    --with-http_random_index_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module \
    --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module \
    --with-http_stub_status_module --with-http_perl_module --with-http_geoip_module --with-mail \
    --with-mail_ssl_module --with-cc-opt=-O3 --with-cpu-opt=pentium

    如果编译出错,大家可以去看我这篇文章编译安装nginx并修改版本头信息

    make && make install

    3.配置nginx
    user  nginx;
    worker_processes  2;
    worker_cpu_affinity 0001 0010;
    worker_rlimit_nofile 65535;

    error_log   /var/log/nginx/error.log;

    pid        /var/run/nginx.pid;

    events {
        use epoll;
        worker_connections  65535;
    }

    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 4k;
        large_client_header_buffers 4 32k;
        client_body_in_file_only clean;
        client_max_body_size 8m;

        sendfile        on;
        tcp_nopush      on;

        keepalive_timeout  60;
        tcp_nodelay on;
        server_tokens   off;

        fastcgi_connect_timeout 300s;
        fastcgi_send_timeout 300s;
        fastcgi_read_timeout 300s;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 8 128k;#8 128
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_intercept_errors on;

        #hiden php version
        fastcgi_hide_header X-Powered-By;

        gzip on;
        gzip_min_length 1k;
        gzip_buffers 4 16k;
        gzip_http_version 1.0;
        #gzip_disable "MSIE [1-5]\.";
        gzip_comp_level 4;
        gzip_types text/plain application/x-javascript text/css application/xml image/gif image/jpg image/jpeg image/png;
        #gzip_vary on;
        proxy_hide_header Vary;

        #Memcache Service upstream
        upstream memcache{
            server 192.168.10.5:11211;
            keepalive 512 single;
            }

        server {
            listen       80;
            server_name www.cnblackhat.com;
        #memc-nginx-module
        location /memc {
            internal;
            memc_connect_timeout 100ms;
            memc_send_timeout 100ms;
            memc_read_timeout 100ms;
            set $memc_key $query_string;
            set $memc_exptime 300;
            memc_pass memcache;
            }
        location / {
        root /var/www/vhosts/www.cnblackhat.com;
        index index.php index.html index.htm;
        #srcache-nginx-module
        set $key $uri$args;
        srcache_fetch GET /memc $key;
        srcache_store PUT /memc $key;
       
            location ~ .*\.(php|php5)?$ {
                fastcgi_pass   unix:/tmp/php-cgi.sock;
                fastcgi_index  index.php;
                include  fastcgi.conf;
            }
            }
        }
    }

    ps:
    memc-nginx是一个标准的upstream模块,因此首先需要定义memcache的upstream.
    这里我在本机上启动了一个memcache服务,端口为默认的11211,keepalive指令是http-upsteram-keepalive-module提供的功能, 这里我们最大保持512个不立即关闭的连接用于提升性能.

    下面是为memc-nginx-module配置location,我们配置为/memc,所有请求都通过请求这个location来操作 memcache.
    memc-nginx-module存取memcache是基于http method语义的,使用http的GET方法表示get、PUT方法表示set、DELETE方法表示delete.
    这里我们将/memc设为internal表示只接受内部访问,不接收外部http请求,这是为了安全考虑,当然如果需要通过http协议开放外部访问,可以去掉internal然后使用deny和allow指 令控制权限.比较重要的是$memc_key这个变量,它表示以什么作为key,这里我们直接使用Nginx内置的$query_string来作为 key,$memc_exptime表示缓存失效时间,以秒记.这里统一设为300(5分钟),在实际应用中可以根据具体情况为不同的内容设置不同的过期时间.

    最后我们为“/”这个location配置了缓存,这表示所有的请求都会结果被缓存,当然这里只是示例需要,实际中一般不会这么配,而是为特定需要缓存的location配置缓存. 比如只缓存图片,js,css等资源文件.

    srcache_fetch表示注册一个输入拦截处理器到location,这个配置将在location进入时被执行;
    而 srcache_store表示注册一个输出拦截器到location,当location执行完成并输出时会被执行.
    注意srcache模块实际可以与任何缓存模块进行配合使用,而不必一定是memc.这里我们以$uri$args作为缓存的key.

    经过上述配置后,相当于对Nginx增加了如下逻辑:当所请求的uri以“.php”结尾时,首先到memcache中查询有没有 以$uri$args为key的数据,如果有则直接返回;否则,执行location的逻辑,如果返回的http状态码为200,则在输出前以$uri$args为key,将输入结果存入memcache.
    帖子永久地址: 

    黑帽联盟 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与黑帽联盟享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和黑帽联盟的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、黑帽联盟管理员和版主有权不事先通知发贴者而删除本文

    勿忘初心,方得始终!
    您需要登录后才可以回帖 登录 | 会员注册

    发布主题 !fastreply! 收藏帖子 返回列表 搜索
    回顶部