Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Powering Your Applications With Nginx

Chris Fidao
September 09, 2014

Powering Your Applications With Nginx

How you can use Nginx to power your applications, from hosting, to load balancing, caching and more.

Chris Fidao

September 09, 2014
Tweet

More Decks by Chris Fidao

Other Decks in Technology

Transcript

  1. Powering Your Applications with Nginx

  2. • Web Server • Application Proxy • Load Balancer •

    Web Cache What is Nginx?
  3. • Gzip • SSL Termination • Logging • Streaming (Nginx+)

    • Monitoring (Nginx+) • more …and more
  4. Nginx as a Web Server

  5. vs Apache Same functionality, different PM new connection Nginx event

    loop process process Apache
  6. Web Server • Static Asset Efficiency • Roughly as fast

    as Varnish
  7. Web Server server { listen 80 default_site; ! root /var/www;

    index index.html index.htm; ! server_name example.com www.example.com; ! location / { try_files $uri $uri/ =404; } }
  8. Web Server Use H5BP Configuration

  9. Web Server • Gzip Setup • Mime Types • Cache

    Expiration • Proper SSL Config Use H5BP Configuration • SPDY Setup • X-Domain/Web Fonts • Cache Busting • Security (XSS, dot files)
  10. Nginx as an App Proxy

  11. App Proxy Gateway

  12. App Proxy • HTTP - Go, NodeJS, Gunicorn, Unicorn, uWSGI

    • FastCGI - PHP-FPM, uWSGI • uWSGI - uWSGI • (SCGI & Memcached, too!) Supported “Gateway Protocols”
  13. App Proxy HTTP Proxy location /static { try_files $uri $uri/

    =404; } ! location / { proxy_pass 127.0.0.1:9000; proxy_param APPENV production; include proxy_params; }
  14. App Proxy location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; ! fastcgi_pass

    127.0.0.1:9000; # Or: #fastcgi_pass unix:/var/run/php5-fpm.sock; ! fastcgi_index index.php; fastcgi_param APPENV production; include fastcgi.conf; } FastCGI (PHP-FPM)
  15. App Proxy uWSGI (Python) location /static { try_files $uri $uri/

    =404; } ! location / { uwsgi_pass 127.0.0.1:9000; uwsgi_param APPENV production; include uwsgi_params; }
  16. Nginx as a Load Balancer

  17. Load Balancer upstream app_example { zone backend 64k; least_conn; server

    127.0.0.1:9000 max_fails=3 fail_timeout=30s; server 127.0.0.1:9001 max_fails=3 fail_timeout=30s; server 127.0.0.1:9002 max_fails=3 fail_timeout=30s; } ! server { ! # usual stuff omitted ! location / { health_check; include proxy_params; ! proxy_pass http://app_example/; ! # Handle Web Socket connections proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } Proxy “Upstream”
  18. Load Balancer Upstream: upstream app_example { zone backend 64k; least_conn;

    server 127.0.0.1:9000 max_fails=3 fail_timeout=30s; server 127.0.0.1:9001 max_fails=3 fail_timeout=30s; server 127.0.0.1:9002 max_fails=3 fail_timeout=30s; } Servers & Algorithm
  19. Load Balancer Proxy Pass: • Health Check • Proxy •

    Web Sockets ! server { ! # usual stuff omitted ! location / { health_check; include proxy_params; ! proxy_pass http://app_example/; ! # Handle Web Socket connections proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
  20. Nginx as a Web Cache

  21. Web Cache • Static Files - Caches static files, following

    Cache-Control header rules as per HTTP spec • Dynamic Requests - Can cache results of dynamic (HTTP, FastCGI & uWSGI, etc) requests! • Works with Load Balancing - Do all the things for all the requests! Caches all the things
  22. Web Cache • Cache Server - Server (Nginx) which does

    web caching, pays attention to cache headers • Origin Server - Server containing actual resources that the cache server caches. Determines “Cache Policy” Terminology
  23. App Proxy Gateway Cache Server Origin Server

  24. Web Cache proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m inactive=60m; proxy_cache_key "$scheme$request_method$host$request_uri"; !

    server { # Stuff Omitted ! location / { proxy_cache my_zone; add_header X-Proxy-Cache $upstream_cache_status; ! include proxy_params; proxy_pass http://172.17.0.18:9000; } }
  25. Web Cache fastcgi_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m inactive=60m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; !

    server { # Stuff Omitted ! location / { fastcgi_cache my_zone; add_header X-Proxy-Cache $upstream_cache_status; ! include fastcgi_params; fastcgi_pass http://172.17.0.18:9000; } }
  26. Web Cache uwsgi_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m inactive=60m; uwsgi_cache_key "$scheme$request_method$host$request_uri"; !

    server { # Stuff Omitted ! location / { uwsgi_cache my_zone; add_header X-Proxy-Cache $upstream_cache_status; ! include uwsgi_params; uwsgi_pass http://172.17.0.18:9000; } }
  27. The End ServersForHackers.com