nginx – 为强大的服务器优化php-fpm和varnish

前端之家收集整理的这篇文章主要介绍了nginx – 为强大的服务器优化php-fpm和varnish前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的设置是英特尔®酷睿™i7-2600和内存16 GB DDR3内存

清漆Nginx PHP-fpm apc用于W3 Total Cache和CDN的非常重的wordpress博客

我的问题是,根据blitz.io清漆开始发出超时后,每秒55次点击.此时的cpu使用率几乎不是1%.可用内存始终为10GB.

我尝试直接对PHP-fpm进行基准测试,结果为150次点击/秒,没有任何超时.但之后cpu使用率达到100%并停止响应.

你能帮我优化一下来处理更多吗?

据我所知,Nginx在这里无关,所以我没有包含它的配置.

PHP-fpm配置

listen = /tmp/PHP5-fpm.sock
listen.allowed_clients = 127.0.0.1
user = Nginx
group = Nginx
pm = dynamic
pm.max_children = 150
pm.start_servers = 7
pm.min_spare_servers = 2
pm.max_spare_servers = 15
pm.max_requests = 500
slowlog = /var/log/PHP-fpm/www-slow.log
PHP_admin_value[error_log] = /var/log/PHP-fpm/www-error.log
PHP_admin_flag[log_errors] = on

APC

extension = apc.so
apc.enabled=1
apc.shm_size=512MB
apc.num_files_hint=0
apc.user_entries_hint=0
apc.ttl=7200
apc.use_request_time=1
apc.user_ttl=7200
apc.gc_ttl=3600
apc.cache_by_default=1
apc.filters
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.file_update_protection=2
apc.enable_cli=0
apc.max_file_size=1M
apc.stat=1
apc.stat_ctime=0
apc.canonicalize=0
apc.write_lock=1
apc.report_autofilter=0
apc.rfc1867=0
apc.rfc1867_prefix =upload_
apc.rfc1867_name=APC_UPLOAD_PROGRESS
apc.rfc1867_freq=0
apc.rfc1867_ttl=3600
apc.include_once_override=0
apc.lazy_classes=0
apc.lazy_functions=0
apc.coredump_unmap=0
apc.file_md5=0
apc.preload_path

清漆VCL

backend default {
    .host = "127.0.0.1";
    .port = "8080";
.connect_timeout = 6s;
 .first_byte_timeout = 6s;
 .between_bytes_timeout = 60s;
}

acl purgehosts {
    "localhost";
    "127.0.0.1";
}

# Called after a document has been successfully retrieved from the backend.
sub vcl_fetch {
    # Uncomment to make the default cache "time to live" is 5 minutes,handy
    # but it may cache stale pages unless purged. (TODO)
    # By default Varnish will use the headers sent to it by Apache (the backend server)
    # to figure out the correct TTL.
    # WP Super Cache sends a TTL of 3 seconds,set in wp-content/cache/.htaccess

    set beresp.ttl   = 24h;

    # Strip cookies for static files and set a long cache expiry time.
    if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
            unset beresp.http.set-cookie;
            set beresp.ttl   = 24h;
    }
 # If wordpress cookies found then page is not cacheable
    if (req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)") {
       # set beresp.cacheable = false;#versions less than 3
        #beresp.ttl>0 is cacheable so 0 will not be cached
        set beresp.ttl = 0s;
    } else {
        #set beresp.cacheable = true;
        set beresp.ttl=24h;#cache for 24hrs
    }

    # Varnish determined the object was not cacheable
        #if ttl is not > 0 seconds then it is cachebale
    if (!beresp.ttl > 0s) {
    #    set beresp.http.X-Cacheable = "NO:Not Cacheable";
    } else if ( req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)" ) {
        # You don't wish to cache content for logged in users
        set beresp.http.X-Cacheable = "NO:Got Session";
        return(hit_for_pass); #prevIoUsly just pass but changed in v3+
    }  else if ( beresp.http.Cache-Control ~ "private") {
        # You are respecting the Cache-Control=private header from the backend
        set beresp.http.X-Cacheable = "NO:Cache-Control=private";
        return(hit_for_pass);
    } else if ( beresp.ttl < 1s ) {
        # You are extending the lifetime of the object artificially
        set beresp.ttl   = 300s;
        set beresp.grace = 300s;
        set beresp.http.X-Cacheable = "YES:Forced";
    } else {
        # Varnish determined the object was cacheable
        set beresp.http.X-Cacheable = "YES";
 if (beresp.status == 404 || beresp.status >= 500) {
        set beresp.ttl = 0s;
    }

    # Deliver the content
    return(deliver);
}

sub vcl_hash {
    # Each cached page has to be identified by a key that unlocks it.
    # Add the browser cookie only if a wordpress cookie found.
    if ( req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)" ) {
        #set req.hash += req.http.Cookie;
        hash_data(req.http.Cookie);
         }
}


# vcl_recv is called whenever a request is received
sub vcl_recv {
    # remove ?ver=xxxxx strings from urls so css and js files are cached.
    # Watch out when upgrading wordpress,need to restart Varnish or flush cache.
    set req.url = regsub(req.url,"\?ver=.*$","");

    # Remove "replytocom" from requests to make caching better.
    set req.url = regsub(req.url,"\?replytocom=.*$","");
 remove req.http.X-Forwarded-For;
    set    req.http.X-Forwarded-For = client.ip;

    # Exclude this site because it breaks if cached
    if ( req.http.host == "sr.ituts.gr" ) {
        return( pass );
    }

    # Serve objects up to 2 minutes past their expiry if the backend is slow to respond.
    set req.grace = 120s;
    # Strip cookies for static files:
    if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
        unset req.http.Cookie;
        return(lookup);
    }
    # Remove has_js and Google Analytics __* cookies.
    set req.http.Cookie = regsuball(req.http.Cookie,"(^|;\s*)(__[a-z]+|has_js)=[^;]*","");
    # Remove a ";" prefix,if present.
    set req.http.Cookie = regsub(req.http.Cookie,"^;\s*","");
    # Remove empty cookies.
    if (req.http.Cookie ~ "^\s*$") {
        unset req.http.Cookie;
    }
    if (req.request == "PURGE") {
        if (!client.ip ~ purgehosts) {
                error 405 "Not allowed.";
        }
#prevIoUs version ban() was purge()
        ban("req.url ~ " + req.url + " && req.http.host == " + req.http.host);
        error 200 "Purged.";
    }

    # Pass anything other than GET and HEAD directly.
    if (req.request != "GET" && req.request != "HEAD") {
        return( pass );
    }      /* We only deal with GET and HEAD by default */

    # remove cookies for comments cookie to make caching better.
    set req.http.cookie = regsub(req.http.cookie,"1231111111111111122222222333333=[^;]+(; )?","");

    # never cache the admin pages,or the server-status page,or your Feed? you may want to..i don't
    if (req.request == "GET" && (req.url ~ "(wp-admin|bb-admin|server-status|Feed)")) {
        return(pipe);
    }
    # don't cache authenticated sessions
    if (req.http.Cookie && req.http.Cookie ~ "(wordpress_|PHPSESSID)") {
        return(lookup);
    }
    # don't cache ajax requests
    if(req.http.X-Requested-With == "XMLHttpRequest" || req.url ~ "nocache" || req.url ~
"(control.PHP|wp-comments-post.PHP|wp-login.PHP|bb-login.PHP|bb-reset-password.PHP|register.PHP)") {
        return (pass);
    }
    return( lookup );
}

清漆守护进程选项

DAEMON_OPTS="-a :80 \
             -T 127.0.0.1:6082 \
             -f /etc/varnish/ituts.vcl \
             -u varnish -g varnish \
             -S /etc/varnish/secret \
             -p thread_pool_add_delay=2 \
             -p thread_pools=8 \
             -p thread_pool_min=100 \
             -p thread_pool_max=1000 \
             -p session_linger=50 \
             -p session_max=150000 \
             -p sess_workspace=262144 \
             -s malloc,5G"

我不知道从哪里开始.我应该从优化PHP-fpm开始然后去清漆吗?或者现在是最大的PHP-fpm所以我应该开始寻找清漆中的问题了吗?

最佳答案
在配置中:Varnish Daemon选项

更改后,请遵循specials参数,并默认.我看他们一样

猜你在找的Nginx相关文章