Mit nginx und FastCGI skalieren

3,439 views

Published on

In den meisten Projekten wird Apache mit mod_php zur Auslieferung der Webseiten eingesetzt. Doch wird der Funktionsumfang von Apache für die meisten Projekte nicht genutzt. In dieser Session geht es um die Vorteile, die sich durch den Umstieg auf den leichtgewichtigen nginx und die Einbindung von PHP als FastCGI-Service erzielen lassen.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,439
On SlideShare
0
From Embeds
0
Number of Embeds
170
Actions
Shares
0
Downloads
34
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Mit nginx und FastCGI skalieren

  1. 1. Mit nginx und FastCGI skalierenMichel Hartmann I 01.06.2011 © Mayflower GmbH 2011
  2. 2. Michel Hartmann● Studium der Informatik an der Universität Würzburg● Seit 2008 Entwickler bei der Mayflower GmbH
  3. 3. Kurt Ringsock Mayflower GmbH I 3
  4. 4. Apache Mayflower GmbH I 4
  5. 5. Verarbeitung ... Mayflower GmbH I 5
  6. 6. Lösung Mayflower GmbH I 6
  7. 7. Nutzung Totals for Active Servers Across All Domains June 2000 - May 2011 Quelle: netcraft.com Mayflower GmbH I 7
  8. 8. Apache (mpm_worker) RAM 60 MB CPU 100 % Netzwerk 45 % Requests / s 1.500 Mayflower GmbH I 8
  9. 9. nginx RAM 60 MB 12 MB CPU 100 % 5% Netzwerk 40 % 100 % Requests / s 1.500 4.000 Mayflower GmbH I 9
  10. 10. lighttpd RAM 60 MB 12 MB X CPU 100 % 5% X Netzwerk 40 % 100 % X Requests / s 1.500 4.000 X Mayflower GmbH I 10
  11. 11. nginx Mayflower GmbH I 11
  12. 12. Verarbeitung – Reverse Proxy ... Mayflower GmbH I 12
  13. 13. Konfiguration – Basisuser www-data www-data;worker_processes 10;events { worker_connections 1000;}http { include conf/mime.types; default_type application/octet-stream; sendfile on; gzip on; gzip_min_length 1000; gzip_types text/plain application/xml; keepalive_timeout 75 20; include sites-enabled/*;} Mayflower GmbH I 13
  14. 14. Konfiguration – VHOSTserver { server_name ringsock.de www.ringsock.de; root /var/www/ringsock.de/httpdocs; location / { try_files $uri /index.php?q=$uri&$args; } location ~* .php$ { proxy_pass http://127.0.0.1:8000/; } location = /favicon.ico { access_log off; }} Mayflower GmbH I 14
  15. 15. Benchmark Mayflower GmbH I 15
  16. 16. Cachingproxy_cache_path /data/proxy-cache/ringsock levels=1:2 keys_zone=ringsock:10m max_size=1g inactive=30m;proxy_temp_path /data/proxy-temp/ 1;server { [...] location ~* .php$ { proxy_cache ringsock; proxy_cache_key "$host$request_uri"; proxy_cache_methods GET; proxy_cache_bypass $cookie_nocache $arg_nocache; proxy_no_cache $cookie_nocache $arg_nocache; proxy_cache_valid 200 302 60m; proxy_cache_valid 404 1m; proxy_pass http://127.0.0.1:8000/; }} Mayflower GmbH I 16
  17. 17. Verarbeitung ... Mayflower GmbH I 17
  18. 18. Verarbeitung FastCGI ... Mayflower GmbH I 18
  19. 19. FastCGI Process Manager (FPM) Mayflower GmbH I 19
  20. 20. Konfiguration – php-fpm[global]pid = /var/run/php5-fpm.piderror_log = /var/log/php5-fpm.log[ringsock]listen = /tmp/ringsock-fpm.socketuser = ringsock_degroup = ringsock_depm = dynamicpm.start_servers = 20pm.max_children = 100pm.min_spare_servers = 5pm.max_spare_servers = 20 Mayflower GmbH I 20
  21. 21. Konfiguration – nginxserver { server_name ringsock.de www.ringsock.de; root /var/www/ringsock.de/httpdocs; location / { try_files $uri /index.php?q=$uri&$args; } location ~* .php$ { fastcgi_pass unix:/tmp/ringsock-fpm.socket; include fastcgi_params; }} Mayflower GmbH I 21
  22. 22. Konfiguration – nginx (fastcgi_params)fastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE nginx;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param REMOTE_ADDR $remote_addr;fastcgi_param REMOTE_PORT $remote_port;fastcgi_param SERVER_ADDR $server_addr;fastcgi_param SERVER_PORT $server_port;fastcgi_param SERVER_NAME $server_name Mayflower GmbH I 22
  23. 23. Benchmark Mayflower GmbH I 23
  24. 24. Features Mayflower GmbH I 24
  25. 25. php-fpm – Worker Pools[global]pid = /var/run/php5-fpm.piderror_log = /var/log/php5-fpm.log[ringsock]listen = /tmp/ringsock-fpm.socketuser = ringsock_degroup = ringsock_de…[ringschuh]listen = /tmp/ringschuh-fpm.socketuser = ringschuh_degroup = ringschuh_de… Mayflower GmbH I 25
  26. 26. php-fpm – fastcgi_finsish_request()<?phprequire_once My/Application.php;$application = new My_Application();$application->run();fastcgi_finish_request();$application->postRun(); Mayflower GmbH I 26
  27. 27. php-fpm – Slowlog[global]pid = /var/run/php5-fpm.piderror_log = /var/log/php5-fpm.log[ringsock]listen = /tmp/ringsock-fpm.socketuser = ringsock_degroup = ringsock_depm = dynamicpm.start_servers = 20pm.max_children = 100pm.min_spare_servers = 5pm.max_spare_servers = 20request_slowlog_timeout = 30slowlog = /var/log/php5/ringsock-slow.log Mayflower GmbH I 27
  28. 28. php-fpm – Status & Ping[global]pid = /var/run/php5-fpm.piderror_log = /var/log/php5-fpm.log[ringsock]listen = /tmp/ringsock-fpm.socket pool ringsockuser = ringsock_de process manager dynamicgroup = ringsock_de accepted conn 158pm = dynamic listen queue len 0pm.start_servers = 20pm.max_children = 100 max listen queue len 12pm.min_spare_servers = 5 idle processes 20pm.max_spare_servers = 20 active processes 2pm.status_path = /status total processes 22ping.path = /ping max children reached 25ping.response = pong Mayflower GmbH I 28
  29. 29. php-fpm – Emergency Restart[global]pid = /var/run/php5-fpm.piderror_log = /var/log/php5-fpm.log[ringsock]listen = /tmp/ringsock-fpm.socketuser = ringsock_degroup = ringsock_depm = dynamicpm.start_servers = 20pm.max_children = 100pm.min_spare_servers = 5pm.max_spare_servers = 20emergency_restart_threshold = 10emergency_restart_interval = 5m Mayflower GmbH I 29
  30. 30. Sicherheit www.ringsock.de All your stockings are belong to us Mayflower GmbH I 30
  31. 31. Konfiguration – nginxserver { server_name ringsock.de www.ringsock.de; root /var/www/ringsock.de/httpdocs; location / { try_files $uri /index.php?q=$uri&$args; } location ~* .php$ { fastcgi_pass unix:/tmp/ringsock-fpm.socket; include fastcgi_params; }} Mayflower GmbH I 31
  32. 32. Konfiguration – nginxserver { server_name ringsock.de www.ringsock.de; root /var/www/ringsock.de/httpdocs; location / { try_files $uri /index.php?q=$uri&$args; } location /index.php { fastcgi_pass unix:/tmp/ringsock-fpm.socket; include fastcgi_params; }} Mayflower GmbH I 32
  33. 33. Konfiguration – nginxserver { server_name ringsock.de www.ringsock.de; root /var/www/ringsock.de/httpdocs; location / { try_files $uri /index.php?q=$uri&$args; } location ^~ /index.php { fastcgi_pass unix:/tmp/ringsock-fpm.socket; include fastcgi_params; } location ~* .php$ { deny all; }} Mayflower GmbH I 33
  34. 34. Weiter skalieren Mayflower GmbH I 34
  35. 35. Lastverteilung -fpm -fpm ... ... Mayflower GmbH I 35
  36. 36. Loadbalancingupstream worker_pool { server worker01.ringsock.de:9000 weight=2; server worker02.ringsock.de:9000 weight=3;}server { [...] location ^~ /index.php { fastcgi_pass worker_pool; include fastcgi_params; } […]} Mayflower GmbH I 36
  37. 37. ip_hashupstream worker_pool { ip_hash; server worker01.ringsock.de:9000; server worker02.ringsock.de:9000 down;}server { [...] location ^~ /index.php { fastcgi_pass worker_pool; include fastcgi_params; } […]} Mayflower GmbH I 37
  38. 38. Failoverupstream worker_pool { server worker01.ringsock.de:9000 weight=2 max_fails=2 fail_timeout=60s; server worker02.ringsock.de:9000 weight=3 max_fails=2 fail_timeout=60s; server unix:/tmp/ringsock-fpm.socket backup;}server { [...] location ^~ /index.php { fastcgi_pass worker_pool; fastcgi_next_upstream error timeout; include fastcgi_params; } […]} Mayflower GmbH I 38
  39. 39. Resümee Mayflower GmbH I 39
  40. 40. Fragen? Mayflower GmbH I 40
  41. 41. Vielen Dank für Ihre Aufmerksamkeit! Kontakt Michel Hartmann michel.hartmann@mayflower.de +49 931 35965 1146 Mayflower GmbH Pleichertorstr. 2 97070 Würzburg06/01/11 Mayflower GmbH 41
  42. 42. Load Balancerhttp { upstream webserver { server web01:80 weight=3; server web02:80; server web03:80; } server { server_name www.domain.com; location / { proxy_pass http://webserver; } }} Mayflower GmbH I 42

×