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

Cranking Nginx to 11

Cranking Nginx to 11

You may see Nginx as the run of the mill web server, geared towards serving up static files and your language of choice but there is so much more to Nginx than that.

Under the hood is a treasure trove of additional features and configurations that can help you take things to the next level. This includes having Nginx talk directly to Memcached / Redis (perfect for APIs, without hitting your application), proxy functionality, GeoIP, Lua in the config, MogileFS file serving, improved headers manipulation, and the less useful yet cool ability to query Drizzle / Postgres directly. This is only the tip of the iceberg.

I will take you one a journey to explore the hidden corners of Nginx and show you how to crank it up to 11!

Helgi Þorbjörnsson

February 22, 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! Friday, 22 February 13
  2. Reload (HUP Signal) ‣ Reloads config ‣ Starts up new

    workers ‣ Old workers stop listening ‣ Finish up any work they have Friday, 22 February 13
  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 Friday, 22 February 13
  4. Rewrite Log entries are notice level rewrite_log on; error_log /path/to/log

    notice; Debug Rewrite Rules Friday, 22 February 13
  5. Can work with most Nginx variables such as $http_cookie, $user_agent,

    $uri and countless others. Friday, 22 February 13
  6. upstream web_workers { ip_hash; server www1.example.com; server www2.example.com; server www3.example.com;

    server www4.example.com; } Consistent IP Routing Friday, 22 February 13
  7. 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+ Friday, 22 February 13
  8. 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; } } } Friday, 22 February 13
  9. 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'; } Friday, 22 February 13
  10. # 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'; Friday, 22 February 13
  11. 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 */ } Friday, 22 February 13
  12. location /beer { set $amount $arg_amount; set_if_empty $amount 9999; }

    /beer?amount=12 /beer?amount= Friday, 22 February 13
  13. location /beer { set $amount $arg_amount; set_if_empty $amount 9999; set_unescape_uri

    $name $amount; set_quote_sql_str $quoted_name $name; } Friday, 22 February 13
  14. 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; } Friday, 22 February 13
  15. 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; } Friday, 22 February 13
  16. 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; } Friday, 22 February 13