frontend servers and nginx: what, where and how
DESCRIPTION
Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Igor Sysoev started working on developing Nginx in 2002 and released it to the public in 2004. Since then Nginx is hosting nearly 12.18% (22.2M) of active sites across all domains and is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. This report will give you a full overview of the Nginx and explain why this server is so popular.TRANSCRIPT
Front end servers and nginx
What, when and how?
Basic configuration:
•Tomcat, Resin, JBoss, Apache Http + php, ...•MySQL, PostgreSQL, Oracle, ...
Enough?
Simple web application
General list:
•Process requests to static resource (css, images, etc)•Process request to dynamic part (servlet). •Exchange with database. •Process schedulers, quartz, etc •Generate reports, calculate statistic...
One day he may say: "enough!". Be ready!
What does your server?
Few problems:
•Weak hardware•Weak client connection channel •Lots of requests to static resource (especially images and video)•High loads
Why sites are dying?
Front end is an interface between the user and back end.
Most popular servers to front end role:•nginx•lighthttpd•apache httpd•...
Front end
Popular and productive
Nginx - is a HTTP and reverse proxy server, as well as mail proxy server, written by Igor Sysoev.
According to Netcraft nginx served or proxied 12.49% busiest sites in July 2012.
Nginx
They are:
•Rambler•Yandex•Mail.ru •vk
Who are using nginx?
Features and figures
• 100 000 connection per server• 60 000 requests per second• 2.5 Mb per 10 000 keep-alive
connections• No threads
Features and figures
General function of nginx or another front end server:
•Static content•Reserve proxying with caching•Compression response•Prepared thumbnails for images •Uploading, downloading, streaming, ...
What will nginx do?
Simple configuration
worker_processes 4;
events {
worker_connections 4096;
}
Let's start with nginx
server {
listen 80 default;
server_name www.domain.com;
root /home/domain.com
location / {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Virtual hosts
As much as you need:
server { listen 80; server_name img.domain.com;
index index.php; root /home/domain.com/img;}
Other virtual hosts
gzip on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain application/xml text/javascript text/css text/json;
gzip_disable "msie6";
gzip_comp_level 4;
Compressing
#Image resize location
location ~ ^/preview/(?P<oper>[abc])/(?P<remn>.+) {
root $path/cache;
error_page 404 = @fetch;
}
location @fetch {
root $path/cache;
proxy_pass http://127.0.0.1:81/$oper/$remn;
proxy_store on;
}
Thumbnails, proxy to resize
location ~ ^/s/(.+) {
image_filter_buffer 6M;
alias $path/static/$1;
try_files "" @404;
image_filter crop 80 80;
}
location ~ ^/c/[^/]+/(\d+|-)x(\d+|-)/(.+) {
set $width $1;
set $height $2;
alias $path/static/$3;
try_files "" @404;
if ($secure_link = "") { return 404; }
image_filter crop $width $height;
}
Thumbnails, resize
location ~ ^/int/(.*)$ {
alias $path/$1;
internal;
}
@RequestMapping("/secret/**")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void checkAccess(HttpServletRequest request,
HttpServletResponse response) {
String img = request.getServletPath();
String path = "/int/" + img;
response.setHeader("X-Accel-Redirect", path);
}
Secured link
location ~ ^/int/(.*)$ {
alias $path/$1;
internal;
}
@RequestMapping("/secret/**")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void checkAccess(HttpServletRequest request,
HttpServletResponse response) {
String img = request.getServletPath();
String path = "/int/" + img;
response.setHeader("X-Accel-Redirect", path);
}
Secured link
location /upload {
upload_pass @backend;
upload_store /location;
client_max_body_size 15m;
upload_max_file_size 4m;
# Set specified fields in request body
upload_set_form_field $upload_field_name.content_type "$upload_content_type";
upload_set_form_field $upload_field_name.path "$upload_tmp_path";
upload_cleanup 400 403 404 405 499 500-505;
}
location @backend {
proxy_pass http://127.0.0.1:8080;
}
Uploading