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
![Page 1: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/1.jpg)
Front end servers and nginx
What, when and how?
![Page 2: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/2.jpg)
Basic configuration:
•Tomcat, Resin, JBoss, Apache Http + php, ...•MySQL, PostgreSQL, Oracle, ...
Enough?
Simple web application
![Page 3: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/3.jpg)
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?
![Page 4: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/4.jpg)
Few problems:
•Weak hardware•Weak client connection channel •Lots of requests to static resource (especially images and video)•High loads
Why sites are dying?
![Page 5: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/5.jpg)
Front end is an interface between the user and back end.
Most popular servers to front end role:•nginx•lighthttpd•apache httpd•...
Front end
![Page 6: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/6.jpg)
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
![Page 7: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/7.jpg)
They are:
•Rambler•Yandex•Mail.ru •vk
Who are using nginx?
![Page 8: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/8.jpg)
Features and figures
• 100 000 connection per server• 60 000 requests per second• 2.5 Mb per 10 000 keep-alive
connections• No threads
![Page 9: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/9.jpg)
Features and figures
![Page 10: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/10.jpg)
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?
![Page 11: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/11.jpg)
Simple configuration
worker_processes 4;
events {
worker_connections 4096;
}
Let's start with nginx
![Page 12: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/12.jpg)
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
![Page 13: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/13.jpg)
As much as you need:
server { listen 80; server_name img.domain.com;
index index.php; root /home/domain.com/img;}
Other virtual hosts
![Page 14: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/14.jpg)
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
![Page 15: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/15.jpg)
#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
![Page 16: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/16.jpg)
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
![Page 17: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/17.jpg)
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
![Page 18: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/18.jpg)
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
![Page 19: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/19.jpg)
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
![Page 20: Frontend Servers and NGINX: What, Where and How](https://reader033.vdocuments.us/reader033/viewer/2022061218/54b6c0604a7959d7268b4574/html5/thumbnails/20.jpg)