Frontend Servers and NGINX: What, Where and How

4,031 views

Published on

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.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Frontend Servers and NGINX: What, Where and How

  1. 1. Front end servers andnginx What, when and how?
  2. 2. Simple web applicationBasic configuration:•Tomcat, Resin, JBoss, Apache Http + php, ...•MySQL, PostgreSQL, Oracle, ...Enough?
  3. 3. What does your server?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!
  4. 4. Why sites are dying?Few problems:•Weak hardware•Weak client connection channel•Lots of requests to static resource (especially imagesand video)•High loads
  5. 5. Front endFront end is an interface between the user and backend.Most popular servers to front end role:•nginx•lighthttpd•apache httpd•...
  6. 6. NginxPopular and productiveNginx - is a HTTP and reverse proxy server, as well asmail proxy server, written by Igor Sysoev.According to Netcraft nginx served or proxied 12.49%busiest sites in July 2012.
  7. 7. Who are using nginx?They are:•Rambler•Yandex•Mail.ru•vk
  8. 8. Features and figures • 100 000 connection per server • 60 000 requests per second • 2.5 Mb per 10 000 keep-alive connections • No threads
  9. 9. Features and figures
  10. 10. What will nginx do?General function of nginx oranother front end server:•Static content•Reserve proxying with caching•Compression response•Prepared thumbnails for images•Uploading, downloading, streaming, ...
  11. 11. Lets start with nginxSimple configurationworker_processes 4;events { worker_connections 4096;}
  12. 12. Virtual hostsserver { 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; }}
  13. 13. Other virtual hostsAs much as you need:server { listen 80; server_name img.domain.com; index index.php; root /home/domain.com/img;}
  14. 14. Compressinggzip 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;
  15. 15. Thumbnails, proxy to resize#Image resize locationlocation ~ ^/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;}
  16. 16. Thumbnails, resizelocation ~ ^/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;}
  17. 17. Secured linklocation ~ ^/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);}
  18. 18. Secured linklocation ~ ^/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);}
  19. 19. Uploadinglocation /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;}

×