Nginx - Tips and Tricks.

16,688 views

Published on

Published in: Technology
1 Comment
54 Likes
Statistics
Notes
No Downloads
Views
Total views
16,688
On SlideShare
0
From Embeds
0
Number of Embeds
560
Actions
Shares
0
Downloads
334
Comments
1
Likes
54
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Nginx - Tips and Tricks.

    1. 1. Nginx - Tips & Tricks #rootconf May 2012 @tuxtoti
    2. 2. /meBuilds scalable systems for ADIQUITYWorks with web servers/load balancersIdentify bottlenecks in the architecture.Responsible for smooth serving of MobileAds.
    3. 3. Nginx - Historycirca 2002. Igor Sysoev. Russian dev.writes mod_accel. Realizes the Apache’s lowscalability.2004 - powers rambler.ru. Initial publicversion released.was built to address the C10K problemNow after ~10 yrs its Nginx Inc.
    4. 4. Nginx - C10K problemTo serve concurrent 10K connectionsWhy? I/O is the bottleneck. A thread perconnection model fails. #apachefailselect() vs epoll()http://www.kegel.com/c10k.html
    5. 5. Nginx - Killer Features L7 Load Balancer/Reverse proxy Embedded perl interpreter On the fly binary upgrade. Awesome PCRE support. Useful for rewriting URLs. NGINX - Ultra-fast, Light weight, low memory footprint, feature rich
    6. 6. Nginx - Config Contexts http - The main scope . Typically configs set here will reflect everywhere. server - The to run multiple servers virtually on different ports or with different server names. location - Defines the scope for a URI. upstream - Config scope for a set of upstream/backend servers.
    7. 7. Nginx - Directivesworker_processes, worker_connections,worker_rlimit_nofile - Configure your setupto the amount of traffic you expect toreceive.Number of connections =worker_processes*worker_connectionskeepalive_requests, keepalive_timeout -Configure based on your concurrency andthroughput.
    8. 8. Nginx - Directives upstream - Set up a list of backends for load balancing. W/ rr and wrr it becomes very powerful. max_fails & fail_timeout - to consider a backend inoperative.upstream backend { server 192.168.1.1; server 192.168.1.5:8080; server 192.168.1.13 weight=3; server 192.168.1.16 max_fails=3 fail_timeout=10s; keepalive 2048; #nginx > 1.1.4}server { location / { proxy_pass http://backend; }}
    9. 9. Nginx - Directivesrr vs fair - Send the request to the leastbusy backend server. #Available as a thirdparty module upstream backend { fair; server 192.168.1.2; server 192.168.1.3; }fair - knows how many requests eachbackend is processing.no_rr
    10. 10. Nginx - Directivesproxy_next_upstream - To proceed or to notproceed?location ~ ^/(app) { proxy_read_timeout 12; proxy_set_header X-Feature-Foo “1”; proxy_pass http://test_backend; proxy_next_upstream error;} Takes values: error, timeout, invalid_header, http_*, off
    11. 11. Nginx - Directives add_header - Add custom headers to the response. #USE - Debug to find out the backend server / cache control headers.add_header Cache-Control private;proxy_set_header - Add custom headers tocontrol your backends. #USE - for enabling/disabling features in your backend.
    12. 12. Nginx - Directives empty_gif - Serves a 1x1 transparent gif from memory. #USE - for dropping beacons/ pixelslocation = /beacon { empty_gif;}
    13. 13. Nginx - Directiveslimit_req_zone - Throttle the frequency ofrequests for a client. #USE - mitigate DOSattacks.http { limit_req_zone $binary_remote_addrzone=one:10m rate=1r/s; ... server { ... location /search/ { limit_req zone=one burst=4; }
    14. 14. Nginx - Directiveslimit_conn_zone - Throttle concurrency ofconnections for a client.http { limit_conn_zone $binary_remote_addrzone=one:2m; server { location /download { limit_conn one 1; } }}
    15. 15. Nginx - Directives stub_status - To get the current status of nginx. #USE - gives you info like the curr. active conn., total conn. accepted and handled, current no. of read/write/wait conn.location /ngx_stat { stub_status on; access_log off;}Active connections: 291server accepts handled requests 16630948 16630948 31070465 Reading: 6 Writing: 179 Waiting: 106
    16. 16. Nginx - Directives map - To map a set of values to a different set of values. #USE - for dynamically changing hosts based on URIs.map $uri $new { default http://www.domain.com/home/; /aa http://aa.domain.com/; /bb http://bb.domain.com/; /john http://my.domain.com/users/john/;}server { server_name www.domain.com; rewrite ^$new redirect;}
    17. 17. Nginx - Directives split_clients - To split clients based on some conditions. #USE - for A/B testing in variation in colors/designs.http { split_clients "${remote_addr}" $variant { 0.5% .one; 2.0% .two; - ""; } server { location / { index index${variant}.html;
    18. 18. Nginx - Directivessub_filter - Search and replace content inthe response. #USE - Quick fix for staledata/ typos?error_pages - Custom error pages for your#failwhale moments.if/set/rewrite - powerful constructs forcondition based execution.
    19. 19. Nginx - Builtin Variables $arg_PARAM - To read the value of a GET request PARAM. $http_HEADER - To read the value of any request HEADER.
    20. 20. Nginx - Builtin Variables $request_time - Measure end to end time. #caveat - only from read() to write()/ sendfile() $upstream_response_time - Measure end to end time of your upstream server (w/ $upstream_addr)
    21. 21. Nginx - ModulesEmbedded Perl - To execute perl directlyinside nginx.XSLT - Transform your XML responses toHTML in nginx.FLV/MP4 - To stream FLV/MP4 content.Addition - To add content of other locationsto the current location.GeoIP/Mail/Image Filter/Memcached modules
    22. 22. Nginx - WoesNo dynamically loadable modules yet.Sparse/Bad documentation. (though betternow)
    23. 23. Nginx - AwesomenessVibrant community.Very helpful for novices.Active IRC/Mailing list
    24. 24. Q?Thanks ! @tuxtoti

    ×