5 things you didn't know nginx could do velocity

40
5 things you didn’t know NGINX could do Sarah Novotny Nginx, Inc.

description

NGINX is a well kept secret of high performance web service. Many people know NGINX as an Open Source web server that delivers static content blazingly fast. But, it has many more features to help accelerate delivery of bits to your end users even in more complicated application environments. In this talk we’ll cover several things that most developers or administrators could implement to further delight their end users.

Transcript of 5 things you didn't know nginx could do velocity

Page 1: 5 things you didn't know nginx could do   velocity

5 thingsyou didn’t know NGINX could do

Sarah NovotnyNginx, Inc.

Page 2: 5 things you didn't know nginx could do   velocity

Many people know NGINX as an HTTP request and load balancing server that powers many of the world's busiest websites. But, there are a lot of ancillary pieces that go into the software to make it a whole web application accelerator.

Page 3: 5 things you didn't know nginx could do   velocity

What is NGINX?

InternetN

Web ServerServe content from disk

Application ServerFastCGI, uWSGI, Passenger…

ProxyCaching, Load Balancing… HTTP traffic

Page 4: 5 things you didn't know nginx could do   velocity

146,000,000Websites

NGINX Accelerates

Page 5: 5 things you didn't know nginx could do   velocity

Advanced Features

Bandwidth ManagementContent-based RoutingRequest ManipulationResponse RewritingApplication AccelerationSSL and SPDY termination

AuthenticationVideo DeliveryMail ProxyGeoLocationPerformance MonitoringHigh Availability

Page 6: 5 things you didn't know nginx could do   velocity

23%Top 1 million websites

39%Top 10,000 websites

Page 7: 5 things you didn't know nginx could do   velocity

Some things you might not know

Form spamming

Compress assets

Thread exhaustion

Rewrite content

Online upgrades

Configure flags

A/B testing Include directive

Manipulate proxy

headers

Page 8: 5 things you didn't know nginx could do   velocity

Compress data to reduce bandwidth

• Reduce bandwidth requirements per client

– Content Compression reduces text and HTML– Image resampling reduces image sizes

Page 9: 5 things you didn't know nginx could do   velocity

HTTP gzip module• Provides Gzip capabilities so that responses

from NGINX are compressed to reduce file size• Directives can be used in the http, server and

location contexts• Key directives

– gzip – gzip_types– gzip_proxied

Page 10: 5 things you didn't know nginx could do   velocity

Gzip exampleEnable gzipgzip on;

Apply gzip for text, html and CSSgzip_types text/plain text/html text/css;

Enable gzip compression for any proxied requestgzip_proxy any;

It is not advisable to enable gzip for binary content types such as images, word documents or videos

Page 11: 5 things you didn't know nginx could do   velocity

HTTP image filter• Provides inline image manipulation to

transform images for optimal delivery• Directives can be used in the location

context• Key directives– image_filter size;– image_filter resize width height;– image_filter crop width height;

Page 12: 5 things you didn't know nginx could do   velocity

HTTP image filter examplelocation /img/ { proxy_pass http://backend; image_filter resize 150 100; image_filter rotate 90; error_page 415 = /empty;}location = /empty { empty_gif;}

Page 13: 5 things you didn't know nginx could do   velocity

We talk about the ‘N second rule’:

– 10 seconds(Jakob Nielsen, March 1997)

– 8 seconds(Zona Research, June 2001)

– 4 seconds (Jupiter Research, June 2006)

– 3 seconds (PhocusWright, March 2010)

Page 14: 5 things you didn't know nginx could do   velocity

Stop brute force retries

• Stop brute force password attacks• Stop form spamming

– Use the NGINX limit request module

Page 15: 5 things you didn't know nginx could do   velocity

HTTP limit req module• Allows granular control of request

processing rate• Directives an be used in http, server

and location contexts• Key directives– limit_req_zone– limit_req

Page 16: 5 things you didn't know nginx could do   velocity

HTTP limit req modulehttp { limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; … server { … location /search/ { limit_req zone=one burst=5; }

}}

Page 17: 5 things you didn't know nginx could do   velocity

Protect Apache from thread exhaustion attacks

• Use NGINX in front of Apache• Mitigates ‘slow loris’, ‘keep dead’ and

‘front page of hacker news’ attacks

Page 18: 5 things you didn't know nginx could do   velocity

What is thread exhaustion?

http process

http process

http process

http process

http process

http process

http process

Client-side:

Multiple Connections

HTTP Keepalives

Server-side:

Limited concurrency

Page 19: 5 things you didn't know nginx could do   velocity

How NGINX mitigates thread exhaustion

N

Large numbers of clients, with long-term keepalive connections

NGINX reduces connections to the minimum number necessary

Page 20: 5 things you didn't know nginx could do   velocity

Rewrite content inline

• Use the power of substitution to simplify updates• Directives can be used in the http, server and

location contexts• Key directives

– sub_filter_once– sub_filter– sub_filter_types

Page 21: 5 things you didn't know nginx could do   velocity

HTTP sub filter example

location / {sub_filter_once off;sub_filter_types text/html;

sub_filter “__copyright_date__” “2014”; }

Page 22: 5 things you didn't know nginx could do   velocity

Online Binary updates and configuration changes

• Update either the configuration files or the binary without losing any connections

Page 23: 5 things you didn't know nginx could do   velocity

Configuration file update

[root@localhost ~]# nginx -s reload[root@localhost ~]#

Page 24: 5 things you didn't know nginx could do   velocity

Yep. It’s that simple

Page 25: 5 things you didn't know nginx could do   velocity

Binary Upgrade

[root@localhost ~]# cat /var/run/nginx.pid1991[root@localhost ~]# kill –USR2 1991

• Choose your method of binary installation• Replace the binary

Page 26: 5 things you didn't know nginx could do   velocity

Binary Upgrade[root@localhost ~]# ps -ef |grep nginxroot 1991 1 0 08:06 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.confnginx 2974 1991 0 08:22 ? 00:00:00 nginx: worker process nginx 2975 1991 0 08:22 ? 00:00:00 nginx: worker process root 3123 2948 0 08:43 pts/0 00:00:00 grep nginxroot 3124 1991 0 08:43 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

Page 27: 5 things you didn't know nginx could do   velocity

Binary Upgrade

[root@localhost ~]# kill –WINCH 1991

[root@localhost ~]# kill –QUIT 1991

• Verify things are working as expected(you can still back out gracefully at this point)

Page 28: 5 things you didn't know nginx could do   velocity

nginx –V gives a nearly complete configuration script for compiling

Configure Flags

Page 29: 5 things you didn't know nginx could do   velocity

[root@localhost ~]# nginx -Vnginx version: nginx/1.5.7built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) TLS SNI support enabledconfigure arguments: --prefix=/etc/nginx/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module--etc

Page 30: 5 things you didn't know nginx could do   velocity

A/B testing

Internet

N

Content A

HTTP traffic

Content B

Page 31: 5 things you didn't know nginx could do   velocity

Split Clients Modulehttp { split_clients "${remote_addr}AAA" $variant { 0.5% .A; 2.0% .B; * "”; } server { location / { index index${variant}.html;

Page 32: 5 things you didn't know nginx could do   velocity

Measurement and analysis is left as an exercise to the reader

Page 33: 5 things you didn't know nginx could do   velocity

Include Directive• Includes files• Directives can be used in the any

context• Key directives– include

Page 34: 5 things you didn't know nginx could do   velocity

HTTP include example

http {include /etc/nginx/conf.d/mime.types;include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;

}

Page 35: 5 things you didn't know nginx could do   velocity

Manipulate proxy headers

• Mask content source (like assets in S3)• Manage proxy behavior• Inject your own headers (host header or x-

forward-for etc)

Page 36: 5 things you didn't know nginx could do   velocity

Proxy Header Manipulation• Allows perception management of content

delivery through headers• Directives can be used in the http, server and

location contexts• Key directives– proxy_hide_header– proxy_set_header– proxy_ignore_header

Page 37: 5 things you didn't know nginx could do   velocity

Proxy hide header example

location / {proxy_pass

http://your_bucket.s3.amazonaws.com; proxy_hide_header x-amz-id-2; proxy_hide_header x-amz-meta-s3fox-filesize; proxy_hide_header x-amz-request-id; proxy_hide_header x-amz-meta-s3fox-modifiedtime;... }

Page 38: 5 things you didn't know nginx could do   velocity

Proxy set header example

location / {proxy_pass http://localhost:8000;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;

... }

Page 39: 5 things you didn't know nginx could do   velocity

More resources

• Check out our blog on nginx.com• Webinars: nginx.com/webinars

Try:NGINX F/OSS (nginx.org) NGINX Plus (nginx.com)

Page 40: 5 things you didn't know nginx could do   velocity

Thanks for your time!

@sarahnovotnyEvangelist, NGINXProgram Chair, OSCON