Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

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. • Gzip • SSL Termination • Logging • Streaming (Nginx+)

    • Monitoring (Nginx+) • more …and more
  2. 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; } }
  3. 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)
  4. App Proxy • HTTP - Go, NodeJS, Gunicorn, Unicorn, uWSGI

    • FastCGI - PHP-FPM, uWSGI • uWSGI - uWSGI • (SCGI & Memcached, too!) Supported “Gateway Protocols”
  5. 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; }
  6. 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)
  7. 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; }
  8. 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”
  9. 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
  10. 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"; } }
  11. 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
  12. 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
  13. 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; } }
  14. 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; } }
  15. 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; } }