+ All Categories
Home > Technology > Nginx - Tips and Tricks.

Nginx - Tips and Tricks.

Date post: 29-Jan-2018
Category:
Upload: harish-s
View: 16,809 times
Download: 0 times
Share this document with a friend
24
Nginx - Tips & Tricks #rootconf May 2012 @tuxtoti
Transcript
Page 1: Nginx - Tips and Tricks.

Nginx - Tips & Tricks#rootconfMay 2012

@tuxtoti

Page 2: Nginx - Tips and Tricks.

/me

Builds scalable systems for ADIQUITY

Works with web servers/load balancers

Identify bottlenecks in the architecture.

Responsible for smooth serving of Mobile Ads.

Page 3: Nginx - Tips and Tricks.

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.

Page 4: Nginx - Tips and Tricks.

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

Page 5: Nginx - Tips and Tricks.

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

Page 6: Nginx - Tips and Tricks.

Nginx - Config Contextshttp - 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.

Page 7: Nginx - Tips and Tricks.

Nginx - Directivesworker_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.

Page 8: Nginx - Tips and Tricks.

Nginx - Directivesupstream - 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;

}}

Page 9: Nginx - Tips and Tricks.

Nginx - Directivesrr 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

Page 10: Nginx - Tips and Tricks.

Nginx - Directivesproxy_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

Page 11: Nginx - Tips and Tricks.

Nginx - Directivesadd_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.

Page 12: Nginx - Tips and Tricks.

Nginx - Directivesempty_gif - Serves a 1x1 transparent gif from memory. #USE - for dropping beacons/pixels

location = /beacon { empty_gif;}

Page 13: Nginx - Tips and Tricks.

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; }

Page 14: Nginx - Tips and Tricks.

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; } }}

Page 15: Nginx - Tips and Tricks.

Nginx - Directivesstub_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

Page 16: Nginx - Tips and Tricks.

Nginx - Directivesmap - 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;}

Page 17: Nginx - Tips and Tricks.

Nginx - Directivessplit_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;

Page 18: Nginx - Tips and Tricks.

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.

Page 19: Nginx - Tips and Tricks.

Nginx - Builtin Variables

$arg_PARAM - To read the value of a GET request PARAM.

$http_HEADER - To read the value of any request HEADER.

Page 20: Nginx - Tips and Tricks.

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)

Page 21: Nginx - Tips and Tricks.

Nginx - ModulesEmbedded 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

Page 22: Nginx - Tips and Tricks.

Nginx - Woes

No dynamically loadable modules yet.

Sparse/Bad documentation. (though better now)

Page 23: Nginx - Tips and Tricks.

Nginx - Awesomeness

Vibrant community.

Very helpful for novices.

Active IRC/Mailing list

Page 24: Nginx - Tips and Tricks.

Q?

Thanks !@tuxtoti


Recommended