NGINX、PHP-FPM+APC 和 Varnish 的内存问题

NGINX、PHP-FPM+APC 和 Varnish 的内存问题

我运行的是 512MB 的内存,但有很多内存问题。我认为这是由于我的配置造成的。

root@s1:~# free -m
             total       used       free     shared    buffers     cached
Mem:          1024       1022          1          0          0          0
-/+ buffers/cache:       1022          1
Swap:            0          0          0
root@s1:~# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  23760  1756 ?        Ss   15:33   0:00 init
root      1086  0.0  0.0  21304  1016 ?        Ss   15:33   0:00 /sbin/udevd --daemon
root      1153  0.0  0.2  49988  2908 ?        Ss   15:33   0:00 /usr/sbin/sshd -D
root      1199  0.0  0.1  14924  1104 ?        Ss   15:33   0:00 /usr/sbin/xinetd -dontfork -pidfile /var/run/xinetd.pid -stayalive -inetd_com
root      1201  0.0  0.0  19068  1020 ?        Ss   15:33   0:00 cron
syslog    1235  0.0  0.0  12704   816 ?        Ss   15:33   0:00 /sbin/syslogd -u syslog
root      1276  0.0  0.0   4352   748 ?        S    15:33   0:00 /bin/sh /usr/bin/mysqld_safe
mysql     1345  0.0  4.0 512600 42444 ?        Sl   15:33   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/li
root      1435  0.0  0.1  76844  1404 ?        Ss   15:33   0:00 nginx: master process /usr/sbin/nginx
www-data  1436  0.0  0.1  77164  1928 ?        S    15:33   0:00 nginx: worker process
www-data  1437  0.0  0.1  77164  1960 ?        S    15:33   0:00 nginx: worker process
www-data  1440  0.0  0.1  77164  1960 ?        S    15:33   0:00 nginx: worker process
www-data  1443  0.0  0.1  77164  1940 ?        S    15:33   0:00 nginx: worker process
root      1466  0.0  0.5 337844  6232 ?        Ss   15:33   0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)      
www-data  1468  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1471  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1472  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1473  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1474  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1475  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1476  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1477  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1478  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1479  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1483  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1484  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1486  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1488  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1490  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
root      1592  0.0  0.1 117216  1436 ?        Ss   15:33   0:00 /usr/sbin/varnishd -P /var/run/varnishd.pid -a :80 -T localhost:6082 -f /etc/
www-data  1593  0.0  0.2 593584  2672 ?        Sl   15:33   0:00 /usr/sbin/varnishd -P /var/run/varnishd.pid -a :80 -T localhost:6082 -f /etc/
root      1690  0.0  0.2  89132  2248 ?        Ss   15:33   0:00 sendmail: MTA: accepting connections          
root      1751  0.0  0.3  73384  3672 ?        Rs   15:35   0:00 sshd: root@pts/1    
root      1767  0.0  0.2  20920  2132 pts/1    Ss   15:35   0:00 -bash
root      1833  0.0  0.1  18112  1248 pts/1    R+   15:51   0:00 ps aux

我的配置是:

/etc/nginx/nginx.conf

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    multi_accept on;
    use epoll;
}

http {

    # Let NGINX get the real client IP for its access logs
    set_real_ip_from 127.0.0.1;
    real_ip_header X-Forwarded-For;

    # Basic Settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 20;
    client_max_body_size 15m;
    client_body_timeout 60;
    client_header_timeout 60;
    client_body_buffer_size  1K;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 8k;
    send_timeout 60;
    reset_timedout_connection on;
    types_hash_max_size 2048;
    server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Logging Settings
    # access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # Log Format
    log_format main '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    # Gzip Settings
    gzip on;
    gzip_static on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 512;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/css text/javascript text/xml text/plain text/x-component 
    application/javascript application/x-javascript application/json 
    application/xml  application/rss+xml font/truetype application/x-font-ttf 
    font/opentype application/vnd.ms-fontobject image/svg+xml;

    # Virtual Host Configs
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

php.ini

short_open_tag = Off
ignore_user_abort = Off
post_max_size = 15M
upload_max_filesize = 15M
default_charset = "UTF-8"
allow_url_fopen = Off
default_socket_timeout = 30
mysql.allow_persistent = Off

[apc]
apc.stat = "0"
apc.max_file_size = "1M"
apc.localcache = "1"
apc.localcache.size = "64"
apc.shm_segments = "1"
apc.ttl = "3600"
apc.user_ttl = "7200"
apc.gc_ttl = "3600"
apc.cache_by_default = "1"
apc.filters = ""
apc.write_lock = "1"
apc.num_files_hint= "512"
apc.user_entries_hint="4096"
apc.shm_size = "64M"
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.include_once_override = "0"
apc.file_update_protection="2"
apc.canonicalize = "1"
apc.report_autofilter="0"
apc.stat_ctime="0"
;This should be used when you are finished with PHP file changes.
;As you must clear the APC cache to recompile already cached files.
;If you are still developing, set this to 1.
apc.stat="0"

/etc/php5/fpm/pool.d/www.conf

user = www-data
group = www-data
listen = /var/run/php-fpm.socket
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 15
pm.min_spare_servers = 5
pm.max_spare_servers = 25
pm.process_idle_timeout = 60s
request_terminate_timeout = 30
security.limit_extensions = .php

php_flag[display_errors] = off
php_admin_value[error_reporting] = 0
php_admin_value[error_log] = /var/log/php5-fpm.log
php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 128M

/etc/默认/清漆

DAEMON_OPTS="-a :80 \
    -T localhost:6082 \
    -f /etc/varnish/default.vcl \
    -u www-data -g www-data \
    -S /etc/varnish/secret \
    -p thread_pools=2 \
    -p thread_pool_min=25 \
    -p thread_pool_max=250 \
    -p thread_pool_add_delay=2 \
    -p session_linger=50 \
    -p sess_workspace=262144 \
    -p cli_timeout=40 \
    -s malloc,256m"

答案1

即使在按照@etagenklo 的建议进行更改之后,您仍然有多达 10 台服务器,每台服务器使用 128 MB 的 PHP RAM,再加上 64 MB 的 APC RAM……在您考虑任何其他类型的开销或每个进程的 RAM 使用情况之前,这就已经超过 1G了。

猜测一下,你需要减少 PHP 应用程序的内存使用量;128 MB 的内存限制是限制,不是硬分配,所以你实际上使用每个进程占用大量内存。XHProf 或 TraceView 等工具可以帮助您确定所有 RAM 都用在了哪里。否则,pm.max_children = 4即使在病态情况下,设置也应将内存保持在 512 MB 以下。

当然,另一个解决方案就是购买一台更大的服务器。你花了多少时间或将要花多少时间调试这个问题?你的时间值多少钱?多一点内存要花多少钱?

答案2

您的值fpm-pool.conf似乎太高了。请尝试以下方法:

pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 2

答案3

使用 php-fpm

pm = ondemand
pm.max_requests = 10000

并创建某种交换

https://www.varnish-software.com/static/book/Tuning.html#storage-backends

现在获取更大的服务器/vps 已经没有问题了。

相关内容