Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Loading in …3
×
1 of 24

Nginx - Tips and Tricks.

56

Share

Download to read offline

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Nginx - Tips and Tricks.

  1. 1. Nginx - Tips & Tricks #rootconf May 2012 @tuxtoti
  2. 2. /me Builds scalable systems for ADIQUITY Works with web servers/load balancers Identify bottlenecks in the architecture. Responsible for smooth serving of Mobile Ads.
  3. 3. Nginx - History circa 2002. Igor Sysoev. Russian dev. writes mod_accel. Realizes the Apache’s low scalability. 2004 - powers rambler.ru. Initial public version released. was built to address the C10K problem Now after ~10 yrs its Nginx Inc.
  4. 4. Nginx - C10K problem To serve concurrent 10K connections Why? I/O is the bottleneck. A thread per connection model fails. #apachefail select() 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 - Directives worker_processes, worker_connections, worker_rlimit_nofile - Configure your setup to the amount of traffic you expect to receive. Number of connections = worker_processes*worker_connections keepalive_requests, keepalive_timeout - Configure based on your concurrency and throughput.
  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 - Directives rr vs fair - Send the request to the least busy backend server. #Available as a third party module upstream backend { fair; server 192.168.1.2; server 192.168.1.3; } fair - knows how many requests each backend is processing. no_rr
  10. 10. Nginx - Directives proxy_next_upstream - To proceed or to not proceed? 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 to control 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/ pixels location = /beacon { empty_gif; }
  13. 13. Nginx - Directives limit_req_zone - Throttle the frequency of requests for a client. #USE - mitigate DOS attacks. http { limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; ... server { ... location /search/ { limit_req zone=one burst=4; }
  14. 14. Nginx - Directives limit_conn_zone - Throttle concurrency of connections for a client. http { limit_conn_zone $binary_remote_addr zone=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: 291 server 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 - Directives sub_filter - Search and replace content in the response. #USE - Quick fix for stale data/ typos? error_pages - Custom error pages for your #failwhale moments. if/set/rewrite - powerful constructs for condition 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 - Modules Embedded Perl - To execute perl directly inside nginx. XSLT - Transform your XML responses to HTML in nginx. FLV/MP4 - To stream FLV/MP4 content. Addition - To add content of other locations to the current location. GeoIP/Mail/Image Filter/Memcached modules
  22. 22. Nginx - Woes No dynamically loadable modules yet. Sparse/Bad documentation. (though better now)
  23. 23. Nginx - Awesomeness Vibrant community. Very helpful for novices. Active IRC/Mailing list
  24. 24. Q? Thanks ! @tuxtoti

Editor's Notes

  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • ×