[ASK] Setting NGINX + PHP-FPM untuk High Traffic


Status
Not open for further replies.

ryandipranata

Poster 1.0
Halo mastah DWH semua, sebelumnya saya uda googling kesana kemari. Tapi saya cobain tetep hasilnya kurang memuaskan, alias masih sering High CPU sehingga website saya serring Error 502 kala dibuka. Di error_list NGINX ada tulisannya begini juga:
Code:
2014/05/01 13:27:38 [error] 64117#0: *2745 upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx.xxx.xxx server: xxxxxxx.com, request: "GET /xxxxxxxxxx", upstream: "fastcgi://127.0.0.1:9001"

2014/05/01 13:27:31 [error] 64117#0: *2415 upstream timed out (110: Connection timed out) while connecting to upstream"

Bagaimana ya setting NGINX+PHP-FPM yang benar untuk jumlah visitor online sekitar 200 - 500 an?? Saya juga pakai CloudFlare, berharap bisa reduce pemakaian CPU tapi tetap sama saja.

Ketika saya cek "htop" yang sering memakan CPU banyak adalah PHP-FPM, bisa hingga 80% setiap proses.

Spesifikasi VPS saya:
- Core 2
- RAM 4 GB
- KVM

Settingan Nginx saya:
Code:
#user  nobody;
worker_processes 2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /usr/local/apps/nginx/var/log/nginx.pid;

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

error_log /usr/local/apps/nginx/var/log/error_log debug;


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 /usr/local/apps/nginx/var/log/web.access.log  main;


      # Basic Settings

        access_log off;
    sendfile on;
    tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 30;
        keepalive_requests 100000;
        server_tokens off;

         reset_timedout_connection on;
         send_timeout 60;
         client_body_timeout 60;
         client_header_timeout 60;
         client_max_body_size 20M;
         client_body_buffer_size 1k;
         client_header_buffer_size 1k;
         large_client_header_buffers 4 8k;
         types_hash_max_size 2048;

        # File Cache Settings

        open_file_cache max=200000 inactive=20s;
        open_file_cache_valid 30s;
        open_file_cache_min_uses 2;
        open_file_cache_errors on;

     #Gzip  Setting

         gzip on;
         gzip_min_length 10240;
         gzip_proxied expired no-cache no-store private auth;
         gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
         gzip_disable "MSIE [1-6]\.";


         # If your domain names are long, increase this parameter.
     server_names_hash_bucket_size 64;

     include /usr/local/apps/nginx/etc/conf.d/*.conf;
         include /usr/local/apps/nginx/etc/sites-enabled/*.conf;
}

Settingan PHP-FPM saya:
Code:
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
;emergency_restart_threshold 10
;emergency_restart_interval 1m
;process_control_timeout 10s

;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;

[index]
listen = 127.0.0.1:9000
user = rap
group = rap
request_slowlog_timeout = 5s
slowlog = var/log/php-fpm-slowlog-index.log
"/usr/local/apps/php53/etc/php-fpm.conf" 64L, 1359C

listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 5
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500
listen.backlog = -1
request_terminate_timeout = 300
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
security.limit_extensions = FALSE
env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

[regular]
listen = 127.0.0.1:9001
user = rap
group = rap
request_slowlog_timeout = 5s
slowlog = var/log/php-fpm-slowlog-regular.log
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 5
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500
listen.backlog = -1
request_terminate_timeout = 300
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
security.limit_extensions = FALSE
env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

Terimakasih sebelumnya :)
 
Last edited:

mustafaramadhan

Hosting Guru
Ini masalah timeout pada sisi nginx dan php-fpm. Harus dinaikkan dan sinkron antara keduanya.
 

arieonline

Expert 1.0
itu ram 4 GB ya mas, worker utk php-fpm nya imut amat, seharusnya ram nya sisa banyak ya mas?

coba dinaikin sbb
Code:
pm = dynamic
pm.max_children = 64
pm.start_servers = 16
pm.min_spare_servers = 16
pm.max_spare_servers = 32

jgn lupa direstart php-fpm nya
 

ryandipranata

Poster 1.0
itu ram 4 GB ya mas, worker utk php-fpm nya imut amat, seharusnya ram nya sisa banyak ya mas?

coba dinaikin sbb
Code:
pm = dynamic
pm.max_children = 64
pm.start_servers = 16
pm.min_spare_servers = 16
pm.max_spare_servers = 32

jgn lupa direstart php-fpm nya

Sudah saya coba mas, tapi saya cek di "htop" process yg running dan load average jadi membengkak ya mas?
Atau ada settingan nginx / mysql saya yg salah ya
 

ryandipranata

Poster 1.0
Settingan mysql saya:
Code:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0

# MyISAM #
key-buffer-size                    = 64M
myisam-recover                  = FORCE,BACKUP
myisam_sort_buffer_size   = 32M
join_buffer_size                 = 1M
read_buffer_size                = 1M
sort_buffer_size                 = 2M

# SAFETY #
max-allowed-packet             = 64M
open-files-limit               = 65535

# CACHES AND LIMITS #
table_cache = 1024
table-definition-cache         = 1024
thread_cache_size = 1K
wait_timeout = 7000
connect_timeout = 300
tmp-table-size                 = 256M
max-heap-table-size            = 256M
query-cache-size               = 128M
query_cache_limit = 8M
max-connections                = 500
table-open-cache               = 2048
low_priority_updates=1
concurrent_insert=ALWAYS
long_query_time = 5
skip-innodb

# LOGGING #
log-error                      = /var/lib/mysql/mysql-error.log
log-queries-not-using-indexes  = 1
slow-query-log                 = 1
slow-query-log-file            = /var/lib/mysql/mysql-slow.log

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
 

mustafaramadhan

Hosting Guru
Di nginx kan ada beberapa timeout (misalnya send_timeout). Nilai ini sebaiknya sama atau lebih tinggi daripada nilai pada 'request_terminate_timeout' di php-fpm.conf.

Tapi nilai yang terlalu tinggi (misalnya 300) memiliki efek terlalu lama idle. Coba dulu nilai 120-180.
 

junior riau

Hosting Guru
Verified Provider
kenapa gak pake http proxy?
di port 80 dihandle sama nginx
di port [n] dihandle sama apache + php-fpm
di php, tambahin module/extension memcached dan memcache dari PECL

memcache buat PHP nya, memcached bakal running listener cache di system/server
terus naikin juga xcache buat loadnya, + mod_pagespeed by google
 

WarnaHost

Expert 2.0
Verified Provider
Itu key_buffer_size nya kecil sekali ya, coba tambahin di my.conf key_buffer_size= 128M atau 256MB, kalau ram 4 GB terlalu kecil kalau cuma 64MB

Barangkali membantu coba pada my.conf di edit, sebelumnya backup dulu :)
PHP:
key_buffer_size= 128M
open-files-limit= 2114
table_open_cache= 128
myisam_sort_buffer_size= 128M
concurrent_insert= 2
read_buffer_size= 2M
read_rnd_buffer_size= 1M

query_cache_type= OFF
 

ryandipranata

Poster 1.0
Di nginx kan ada beberapa timeout (misalnya send_timeout). Nilai ini sebaiknya sama atau lebih tinggi daripada nilai pada 'request_terminate_timeout' di php-fpm.conf.

Tapi nilai yang terlalu tinggi (misalnya 300) memiliki efek terlalu lama idle. Coba dulu nilai 120-180.

Uda mendingan mastah.. Tapi masih suka ada beginian di error_listnya
Code:
*89052 upstream prematurely closed connection while reading response header from upstream

kenapa gak pake http proxy?
di port 80 dihandle sama nginx
di port [n] dihandle sama apache + php-fpm
di php, tambahin module/extension memcached dan memcache dari PECL

memcache buat PHP nya, memcached bakal running listener cache di system/server
terus naikin juga xcache buat loadnya, + mod_pagespeed by google

Nggak tau cara settingnya mastah :(

Itu key_buffer_size nya kecil sekali ya, coba tambahin di my.conf key_buffer_size= 128M atau 256MB, kalau ram 4 GB terlalu kecil kalau cuma 64MB

Barangkali membantu coba pada my.conf di edit, sebelumnya backup dulu :)
PHP:
key_buffer_size= 128M
open-files-limit= 2114
table_open_cache= 128
myisam_sort_buffer_size= 128M
concurrent_insert= 2
read_buffer_size= 2M
read_rnd_buffer_size= 1M

query_cache_type= OFF

Oke kang ivan saya cobain dulu :D
 
Status
Not open for further replies.

Top