Cookies fastcgi缓存登录用户的操作缓存,并为每个用户定制

Cookies fastcgi缓存登录用户的操作缓存,并为每个用户定制,cookies,nginx,php,Cookies,Nginx,Php,目前,我正在为未登录用户使用fastcgi_缓存进行缓存,并使用(if+fastcgi_no_缓存+fastcgi_缓存绕过)将登录用户直接传递到后端,即PHP-FPM 这已经足够好了,但是当PHP-FPM开始达到500+req/s时,会导致缓慢/加载启动 所以我想的是为登录用户创建一个缓存,每个用户都有自己的缓存文件,这可能吗?如果是的话,你能给我一些建议吗。我看了很多,但没什么帮助 使用mysql、memcached和apc运行自定义php cms的站点 cat/etc/nginx/ngin

目前,我正在为未登录用户使用fastcgi_缓存进行缓存,并使用(if+fastcgi_no_缓存+fastcgi_缓存绕过)将登录用户直接传递到后端,即PHP-FPM

这已经足够好了,但是当PHP-FPM开始达到500+req/s时,会导致缓慢/加载启动

所以我想的是为登录用户创建一个缓存,每个用户都有自己的缓存文件,这可能吗?如果是的话,你能给我一些建议吗。我看了很多,但没什么帮助

使用mysql、memcached和apc运行自定义php cms的站点

cat/etc/nginx/nginx.comf

user  username username;

worker_processes     8;
worker_rlimit_nofile 20480;

pid /var/run/nginx.pid;

events {

    worker_connections 10240;
    use epoll;
}

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      off;
    error_log   /var/log/nginx/error.log    warn;
    log_not_found       off;
    log_subrequest      off;

    server_tokens       off;
    sendfile        on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   3;
    keepalive_requests  50;
    send_timeout        120;    
    connection_pool_size    256;
    chunked_transfer_encoding on;
    ignore_invalid_headers   on;
    client_header_timeout   60; 
    large_client_header_buffers 4 128k;
    client_body_in_file_only off;
    client_body_buffer_size 512K;
    client_max_body_size    4M;
    client_body_timeout 60;
    request_pool_size   32k;
    reset_timedout_connection on;
    server_name_in_redirect off;
    server_names_hash_max_size 4096;
    server_names_hash_bucket_size 256;
    underscores_in_headers  off;
    variables_hash_max_size 4096;
    variables_hash_bucket_size 256;

    gzip            on;
    gzip_buffers        4 32k;
    gzip_comp_level     1;
    gzip_disable            "MSIE [1-6]\.";
    gzip_min_length     0;
    gzip_proxied        any;
    gzip_types      text/plain text/css application/x-javascript text/javascript text/xml application/xml application/xml+rss application/atom+xml;

    open_file_cache     max=3000 inactive=20s;
    open_file_cache_min_uses 1;
    open_file_cache_valid   20s;
    open_file_cache_errors  off;

    fastcgi_buffer_size     8k;
    fastcgi_buffers         512 8k;
    fastcgi_busy_buffers_size   16k;
    fastcgi_cache_methods   GET HEAD;
    fastcgi_cache_min_uses  1;
    fastcgi_cache_path /dev/shm/nginx levels=1:2 keys_zone=website:2000m inactive=1d max_size=2000m;
    fastcgi_connect_timeout 60;
    fastcgi_intercept_errors on;
    fastcgi_pass_request_body on;
    fastcgi_pass_request_headers on;
    fastcgi_read_timeout    120;
    fastcgi_send_timeout    120;
    proxy_temp_file_write_size 16k;

    fastcgi_max_temp_file_size  1024m;

    include /etc/nginx/vhosts/*.conf;

}
vhost设置:

server {

    listen 80;
    server_name domain.com;

    access_log  off;
    error_log  /var/log/nginx/error.log warn;
    root /home/username/public_html;

    location ~ \.php$ {

        # pass cache if logged in
        set $nocache "";
                if ($http_cookie ~ (MyCookieUser*|MyCookiePass*)) {
                  set $nocache "Y";
                }
                fastcgi_no_cache $nocache;
                fastcgi_cache_bypass $nocache;
        fastcgi_cache       website;
        fastcgi_cache_key         $host$uri$is_args$args;
        fastcgi_cache_valid       200 301 302 304 40s;
        fastcgi_cache_valid       any 5s;
        fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503 http_404;
        fastcgi_ignore_headers  Set-Cookie;
        fastcgi_hide_header     Set-Cookie;
        fastcgi_ignore_headers  Cache-Control;
        fastcgi_hide_header     Cache-Control;
        fastcgi_ignore_headers  Expires;
        fastcgi_hide_header     Expires;
                fastcgi_no_cache $nocache;
                fastcgi_cache_bypass $nocache;
                fastcgi_index  index.php;
                fastcgi_pass 127.0.0.1:8081;
                fastcgi_param  SCRIPT_FILENAME  /home/username/public_html$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;

    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|ppt|txt|mid|swf|midi|wav|bmp|js)$ {
        root            /home/username/public_html;
        expires             max;
        add_header          Cache-Control   cache;
    }

}
emergency_restart_threshold = 10
emergency_restart_interval = 60s
process_control_timeout =10s
rlimit_files = 102400
events.mechanism = epoll
[www]
user = username
group = username
listen = 127.0.0.1:8081
listen.backlog = 10000
pm = dynamic
pm.max_children = 2048
pm.start_servers = 64
pm.min_spare_servers = 20
pm.max_spare_servers = 128
pm.process_idle_timeout = 10s;
pm.max_requests = 50000
request_slowlog_timeout = 40s
request_terminate_timeout = 60s
php fpm配置

server {

    listen 80;
    server_name domain.com;

    access_log  off;
    error_log  /var/log/nginx/error.log warn;
    root /home/username/public_html;

    location ~ \.php$ {

        # pass cache if logged in
        set $nocache "";
                if ($http_cookie ~ (MyCookieUser*|MyCookiePass*)) {
                  set $nocache "Y";
                }
                fastcgi_no_cache $nocache;
                fastcgi_cache_bypass $nocache;
        fastcgi_cache       website;
        fastcgi_cache_key         $host$uri$is_args$args;
        fastcgi_cache_valid       200 301 302 304 40s;
        fastcgi_cache_valid       any 5s;
        fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503 http_404;
        fastcgi_ignore_headers  Set-Cookie;
        fastcgi_hide_header     Set-Cookie;
        fastcgi_ignore_headers  Cache-Control;
        fastcgi_hide_header     Cache-Control;
        fastcgi_ignore_headers  Expires;
        fastcgi_hide_header     Expires;
                fastcgi_no_cache $nocache;
                fastcgi_cache_bypass $nocache;
                fastcgi_index  index.php;
                fastcgi_pass 127.0.0.1:8081;
                fastcgi_param  SCRIPT_FILENAME  /home/username/public_html$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;

    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|ppt|txt|mid|swf|midi|wav|bmp|js)$ {
        root            /home/username/public_html;
        expires             max;
        add_header          Cache-Control   cache;
    }

}
emergency_restart_threshold = 10
emergency_restart_interval = 60s
process_control_timeout =10s
rlimit_files = 102400
events.mechanism = epoll
[www]
user = username
group = username
listen = 127.0.0.1:8081
listen.backlog = 10000
pm = dynamic
pm.max_children = 2048
pm.start_servers = 64
pm.min_spare_servers = 20
pm.max_spare_servers = 128
pm.process_idle_timeout = 10s;
pm.max_requests = 50000
request_slowlog_timeout = 40s
request_terminate_timeout = 60s
还需要改变php cms运行自己的cookie的方式吗

服务器RAM:32GB DDR3处理器:双E5620 Centos6 64位

为什么不为nginx从其上游cgi服务器(php fpm)获取的每个独特cookie使用不同的缓存呢?当您在站点的“登录”部分时,这或多或少意味着每个登录的用户都将获得自己的缓存。这不是最优的,但会有所帮助

如果你想开始在cookies/动态内容等中使用非常奇特的缓存选项,你可能需要在nginx前面使用varnish缓存


我还有一些特定的位置,可以在访问时清除任何缓存的数据(针对该URI),例如/admin或/system等——我最不希望的是,当php fpm脱机时,nginx向黑客提供我的管理员后端的缓存副本及其所有敏感信息。

您可能喜欢WordPress的以下示例:

        set $cs_session "";
        if ($http_cookie ~* "wordpress_logged_in_[^=]*=([^%]+)%7C") {
                set $cs_session wordpress_logged_in_$1;
        }

fastcgi_cache_key               "$scheme$request_method$host$request_uri$cs_session";