Top Banner
26

Nginx

May 08, 2015

Download

Technology

Stefan Nistor
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Nginx
Page 2: Nginx

What's NGINX?

● Nginx (pronounced “Engine-X”) is an open source,high-performance:

● HTTP server● Reverse proxy server for HTTP, SMTP, POP3

and IMAP protocols.

Page 3: Nginx

Nginx - History

● Author Igor Sysoev● started development of Nginx in 2002.● first public release in 2004, - rambler.ru● Built to address the C10K problem.● Website www.nginx.org.

Page 4: Nginx

Why Nginx?

● Stability● Performance● Security● Third party modules● Easy to configure

Page 5: Nginx

Features

● Static file serving.

● SSL/TLS support.

● Virtual hosts.

● Reverse proxying.

● Load balancing.

● Compression.

● Access controls.

● URL rewriting.

● Custom logging.

● Server-side includes.

● FLV streaming.

● FastCGI.

Page 6: Nginx

Why nginx is faster than apache?

● Apache is a process-based server, while nginx is an event-based (asynchronous) web server.

● Nginx is lighter then Apache.● Buffering.

Page 7: Nginx

Architecture

● Master+workers● Event driven● Non-blocking● Single-threaded● Highlymodular

Page 8: Nginx

Master - Workers

● Master

-Monitor workers

-Handle signals,notify workers

(exit ,reconfiguration,update...)● Worker

-Process client requests

-Get cmd from master

Page 9: Nginx
Page 10: Nginx

Master process

Page 11: Nginx

Worker process

Page 12: Nginx

Modular

Page 13: Nginx

Event-driven Architecture (EDA)

● EDA method is capable of handling more number of clients with less threads compared to conventional way (one thread for one client).

Client 1

Client 2

Client N

worker

Apache P.1

Apache P.N

Apache P.2

Client 1

Client N

Client 2

Page 14: Nginx

Event● ngx_event_t

-Read

-Write

-Timeout

● Callbacks

● Handlers-ngx_event_accept

-ngx_process_events_and_timers

-ngx_handle_read_event

-ngx_handle_write_event

● Posted events-Posted accept event queue

-Posted event queue

Page 15: Nginx

Non-blocking

Asynchronous I/O allows a process to submit an I/O request without waiting for it to complete

Page 16: Nginx

Virtual Server

● Port● Address● Server name● Core server conf.

Page 17: Nginx

Reconfig

Page 18: Nginx

Upgrading

Page 19: Nginx

Directives:

● fair – send the request to the least busy backend server.

● add_header – add custom headers to the response

● limit_req_zone – limit requests for a client,prevent DOS attacks.

● limit_conn_zone – limit concurent conections for a client.

● stub_status – get current status of nginx.

● error_pages – custom error page.

● ip_hash;

● backup;

● If/set/rewrite.

Page 20: Nginx

Config

server {

listen 80;

server_name example.com www.example.com;

root /usr/local/www/example.com;

access_log /var/log/nginx/example.access.log;

error_log /var/log/nginx/example.error.log;

location / {

# Rewrite rules can go here

}

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {

expires max;

add_header Pragma public;

add_header Cache-Control "public, must-revalidate, proxy-revalidate";

}

location ~ \.php {

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $remote_addr;

proxy_set_header Host $host;

proxy_pass http://127.0.0.1:8080;

}

include drop.conf;

}

user www www;

worker_processes 2;

server {

listen 80;

server_name example.com www.example.com;

root /usr/local/www/example.com;

access_log /var/log/nginx/example.access.log;

error_log /var/log/nginx/example.error.log;

location / {

# Rewrite rules can go here

}

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {

expires max;

add_header Pragma public;

add_header Cache-Control "public, must-revalidate, proxy-revalidate";

}

location ~ \.php$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

include drop.conf;

}

Page 21: Nginx

Front-End-(Nginx) and Back-End-(Apache) Advantages

Client

Frontend(NGINX)

Static content

Backend(Apache)

HDD(files)

YES NO

Page 22: Nginx

Proxy balancing

upstream backend {

server 192.168.1.163:8081;

server 192.168.1.159:80 weight=3;

server 192.168.1.160:8080 max_fails=3 fail_timeout=10s;

}

Page 23: Nginx

Nginx – Memcached - Apache

server {

location / {

set $memcached_key $uri;

memcached_pass 127.0.0.1:11211;

default_type text/html;

error_page 404 = @fallback;

}

location @fallback {

proxy_pass backend;

}

}

Page 24: Nginx

Rewrite

Apache:

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]

RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

Nginx:

server {

server_name www.domain.com;

return 301 $scheme://domain.com$request_uri;

}

server {

server_name domain.com;

[...]

}

Page 25: Nginx

Nginx vs Apache

Page 26: Nginx

http://wiki.nginx.org/