Skip to content

Instantly share code, notes, and snippets.

@onigra
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onigra/85cd9b282dc7b2db5405 to your computer and use it in GitHub Desktop.
Save onigra/85cd9b282dc7b2db5405 to your computer and use it in GitHub Desktop.
Nginxのパフォーマンスチューニング
$ ab -c 20 -n 100 http://192.168.50.7/

未修正

user nginx;
worker_processes 4;

pid        /var/run/nginx.pid;
error_log  /var/log/nginx/error.log;
error_log  /var/log/nginx/error.log  notice;
error_log  /var/log/nginx/error.log  info;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;

    sendfile           on;
    tcp_nopush         on;
    default_type       application/json;
    keepalive_timeout  65;

    log_format  ltsv  "remote_addr:$remote_addr\t"
                                 "remote_user:$remote_user\t"
                                 "time_local:$time_local\t"
                                 "request:$request\t"
                                 "status:$status\t"
                                 "body_bytes_sent:$body_bytes_sent\t"
                                 "http_refer:$http_referer\t"
                                 "http_user_agent:$http_user_agent";

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

    server {
        listen 80;
        server_name www.exapmle.jp;

        location / {
            proxy_pass http://exapmle;
        }
    }

    upstream exapmle {
        server unix:/path/to/example/shared/tmp/sockets/unicorn.sock;
    }
}
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.50.7 (be patient).....done


Server Software:        nginx/1.7.7
Server Hostname:        192.168.50.7
Server Port:            80

Document Path:          /
Document Length:        7733 bytes

Concurrency Level:      20
Time taken for tests:   0.623 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      800100 bytes
HTML transferred:       773300 bytes
Requests per second:    160.48 [#/sec] (mean)
Time per request:       124.627 [ms] (mean)
Time per request:       6.231 [ms] (mean, across all concurrent requests)
Transfer rate:          1253.90 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   42  85.4      1     218
Processing:     5   73  51.1    111     127
Waiting:        5   73  51.1    111     127
Total:          5  115  99.0    117     345

Percentage of the requests served within a certain time (ms)
  50%    117
  66%    122
  75%    122
  80%    125
  90%    288
  95%    320
  98%    331
  99%    345
 100%    345 (longest request)

ちょっと修正

user nginx;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 8192;

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

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

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

    server_tokens     off;
    sendfile          on;
    tcp_nopush        on;
    keepalive_timeout 65;

    log_format ltsv "remote_addr:$remote_addr\t"
                    "remote_user:$remote_user\t"
                    "time_local:$time_local\t"
                    "request:$request\t"
                    "status:$status\t"
                    "body_bytes_sent:$body_bytes_sent\t"
                    "http_refer:$http_referer\t"
                    "http_user_agent:$http_user_agent";

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

    server {
        listen 80;
        server_name www.example.jp;

        location / {
            proxy_pass http://example;
        }
    }

    upstream example {
        server unix:/path/to/example/shared/tmp/sockets/unicorn.sock;
    }
}
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.50.7 (be patient).....done


Server Software:        nginx
Server Hostname:        192.168.50.7
Server Port:            80

Document Path:          /
Document Length:        7733 bytes

Concurrency Level:      20
Time taken for tests:   0.611 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      799500 bytes
HTML transferred:       773300 bytes
Requests per second:    163.78 [#/sec] (mean)
Time per request:       122.114 [ms] (mean)
Time per request:       6.106 [ms] (mean, across all concurrent requests)
Transfer rate:          1278.75 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.3      1       1
Processing:    23  112  26.0    119     173
Waiting:       23  112  26.0    119     173
Total:         23  113  26.2    120     174

Percentage of the requests served within a certain time (ms)
  50%    120
  66%    123
  75%    126
  80%    127
  90%    134
  95%    138
  98%    143
  99%    174
 100%    174 (longest request)

いろいろ修正

user nginx;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 8192;

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

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

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

    server_tokens     off;
    sendfile          on;
    tcp_nopush        on;
    keepalive_timeout 65;

    gzip on;
    gzip_types text/css text/javascript;
    gzip_comp_level 1;

    proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=example:8m max_size=50m inactive=120m;
    proxy_temp_path /var/cache/nginx/tmp;

    proxy_redirect   off;
    proxy_set_header Host               $host;
    proxy_set_header X-Real-IP          $remote_addr;
    proxy_set_header X-Forwarded-Host   $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;

    log_format ltsv "remote_addr:$remote_addr\t"
                    "remote_user:$remote_user\t"
                    "time_local:$time_local\t"
                    "request:$request\t"
                    "status:$status\t"
                    "body_bytes_sent:$body_bytes_sent\t"
                    "http_refer:$http_referer\t"
                    "http_user_agent:$http_user_agent";

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

    server {
        listen 80;
        server_name www.example.jp;

        location / {
            proxy_pass http://example;
            proxy_cache example;
            proxy_cache_valid 200 302 60m;
            proxy_cache_valid 404 10m;
        }

        location ~ ^/public/.*\.(css|js) {
            expires 10d;
        }
    }

    upstream example {
        server unix:/path/to/example/shared/tmp/sockets/unicorn.sock;
    }
}
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.50.7 (be patient).....done


Server Software:        nginx
Server Hostname:        192.168.50.7
Server Port:            80

Document Path:          /
Document Length:        7733 bytes

Concurrency Level:      20
Time taken for tests:   0.018 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      799500 bytes
HTML transferred:       773300 bytes
Requests per second:    5496.02 [#/sec] (mean)
Time per request:       3.639 [ms] (mean)
Time per request:       0.182 [ms] (mean, across all concurrent requests)
Transfer rate:          42910.78 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.4      1       2
Processing:     1    2   0.7      2       3
Waiting:        1    2   0.7      2       3
Total:          2    3   0.8      3       5

Percentage of the requests served within a certain time (ms)
  50%      3
  66%      4
  75%      4
  80%      4
  90%      4
  95%      5
  98%      5
  99%      5
 100%      5 (longest request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment