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

Nginx: The power within

Nginx: The power within

Nginx is a web server that packs a lot of punch in a small package. We will explore useful built in debug tools, powerful load balancing, reserve caching mechanisms and how to augment Nginx by loading in additional modules.

We will explore how to make Nginx talk directly to Memcached / Redis (perfect for APIs, without hitting your application), proxy functionality, improved headers manipulation, Lua in the config and the ability to query MySQL / Postgres among other things.

This is only the tip of the iceberg. I will take you on a journey to explore the hidden corners of Nginx!

Helgi Þorbjörnsson

June 08, 2013
Tweet

More Decks by Helgi Þorbjörnsson

Other Decks in Technology

Transcript

  1. ✓ Web Server ✓ Proxy ✓ Cache ✓ Mail Proxy

    ✓ And more! No! It’s so much more!
  2. Reload (HUP Signal) ‣ Reloads config ‣ Starts up new

    workers ‣ Old workers stop listening ‣ Finish up any work they have
  3. Upgrade (USR2 Signal) ‣ Live upgrade of Nginx executable ‣

    Starts up a new Master ‣ Run in parallel ‣ Old Workers gracefully shutdown ‣ Old Master can be brought back
  4. upstream web_workers { server www1.example.com; server www2.example.com weight=2 max_fails=2 fail_timeout=15;

    server www3.example.com weight=4 max_fails=3; server www4.example.com weight=4 max_fails=4 fail_timeout=20; keepalive 8; } Different Weights weight and ip_hash can work together in Nginx 1.3.1+
  5. http { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host

    $http_host; proxy_cache_path /dev/shm/nginx levels=1:2 keys_zone=my-cache:8m max_size=2g inactive=600m; proxy_temp_path /dev/shm/nginx/tmp; proxy_cache_use_stale updating; server { location / { proxy_pass http://example.net; proxy_cache my-cache; proxy_cache_valid 200 302 60m; proxy_cache_valid 404 1m; } } }
  6. more_set_headers 'Server: My-Temple'; # set and clear output headers location

    /bar { more_set_headers 'X-MyHeader: blah' 'X-MyHeader2: foo'; more_set_headers -t 'text/plain text/css' 'Content-Type: text/foo'; more_set_headers -s '400 404 500 503' -s 413 'Foo: Bar'; more_clear_headers 'Transfer-Encoding' 'Content-Type'; } # set output headers location /type { more_set_headers 'Content-Type: text/plain'; }
  7. # set input headers location /foo { more_set_input_headers 'Host: London';

    more_set_input_headers -t 'text/plain' 'X-PHP-UK: bah'; } # replace input header X-PHP-UK *only* if it already exists more_set_input_headers -r 'X-PHP-UK: howdy';
  8. location / { if ($request_method != GET) { rewrite .

    @fallback last; } # append an extenstion for proper MIME type detection if ($args ~* format=json) { rewrite ^/$uri/?(.*)$ /$uri.json$1 break; } if ($args ~* format=xml) { rewrite ^/$uri/?(.*)$ /$uri.xml$1 break; } if ($args ~* format=html) { default_type text/html; add_header "Content" "text/html; charset=utf8"; charset utf-8; } set $memcached_key "$uri?$args"; memcached_pass 127.0.0.1:2211; error_page 500 404 405 = @fallback; } location @fallback { /* pass to FastCGI */ }
  9. location /beer { set $amount $arg_amount; set_if_empty $amount 9999; set_unescape_uri

    $name $amount; set_quote_sql_str $quoted_name $name; }
  10. location /hmac { set $secret “superduper”; set $sign “I want

    this signed”; set_hmac_sha1 $signature $secret $sign; set_encode_base64 $signature $signature echo $signature; }
  11. location /secret { set_unescape_uri $name $arg_name; set_quote_sql_str $quoted_name $name; drizzle_query

    "INSERT INTO agents (name) VALUES ($quoted_name)"; drizzle_pass mysql_backend; }
  12. location /secret { set_unescape_uri $name $arg_name; set_quote_sql_str $quoted_name $name; drizzle_query

    "SELECT * FROM agents WHERE name = $quoted_name"; drizzle_pass mysql_backend; }
  13. location /secret { set_unescape_uri $name $arg_name; set_quote_sql_str $quoted_name $name; drizzle_query

    "SELECT * FROM agents WHERE name = $quoted_name"; drizzle_pass mysql_backend; rds_json on; }