Nginx
Nginx
Nginx
The moment you press enter, your browser displays the home page of Google.
The web server sends back the HTTP response to the browser.
Web application: Any application hosted through the web. Ex: Website
Protocol: A set of rules agreed by both the parties for standardizing the
communication.
A web server is a computer that processes the Hyper Text Transfer Protocol (HTTP)
requests and sends back the HTTP response to clients.
Apache, IIS and Nginx are the most popular web servers.
Need of a Protocol
Loading image..
When two individuals communicate with each other, they should know a common
language to convey their thoughts.
The same way when two systems need to communicate, they should follow a common mode
of communication; hence they agree upon a common set of rules called a Protocol
that governs the communication.
TCP, UDP, FTP and HTTP are some of the communication protocols out of which HTTP is
considered the most important.
5 of 13
HTTP - The Hero
Wikipedia defines HTTP as the foundation of the data communication for WWW.
HTTP Request: Client sends a request with the URL of an HTML page.
HTTP Response: The web server transmits back the requested HTML page via an HTTP
response.
W3C httpd or CERN httpd is the world's first web server developed in 1990's by Tim
Berners-Lee and his team at CERN (European Organization for Nuclear Research).
Both Apache HTTP server and IIS were introduced in the year 1995, and even now they
are reigning as two giants in the world of web technology.
In 1999, Apache released its TOMCAT server to support Java servlets and JSPs.
One fine morning in 1999, Dan Kegal came up with an issue that all the traditional
web servers are incapable of handling 10K concurrent clients/connections and named
it C10K problem.
The C10K problem is the main reason why the next giant of the field called Nginx
grabbed the stage.
The kernel uses O(n^2)* algorithm, so 10K threads are not possible.
So, the engineers started moving away from threaded servers to event-driven servers
like Nginx
What is Nginx?
Pronounced as EngineX.
The Nginx server processes the HTTP requests and sends back the HTTP response to
the client.
A reverse proxy is also a proxy server that retrieves data from all the resources
on behalf of its client.
FastCGI
uwsgi
SCGI
Nginx acts as an effective load balancer for HTTP by distributing requests across
multiple applications.
It improves the performance, reduces the latency and also maximizes the throughput.
Round Robin
Least Connected
IP hash
4 of 5
Nginx Versions
Extract it anywhere.
Hold On!
Nginx is easy to install with windows, but what about the performance?
A Worker (Nginx instance) cannot handle more than 1024 connections concurrently.
Opportunities:
Obstacles:
7 of 13
From yum:
sudo yum install pcre-devel zlib-devel openssl-devel
Ensure that you are in the directory where you wish to install NginX.
wget http://www.nginx.org/download/nginx-1.3.15.tar.gz
cd nginx-1.3.15
./configure --help
If you have decided the modules to be included, run the command below.
./configure �with-foo
make
make install
Obstacles
or
apt-get install nginx
If the native package is old, it is good to install from binary packages provided
by Nginx
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
Here, if OS in CentOS then OS should be given centos, and OSRELEASE is the version
number.
Here, the codename can be substituted with lucid, quantal , oneiric or precise
accordingly.
Then run the following commands
apt-get update
Starting Nginx
Using a script is the best way of starting Nginx.
If you have installed Nginx through binary files, you can directly run the command
below:
If you have installed by other means, you need to install the script on your own.
To get help on quick setup, visit the Nginx community page for InitScripts
chmod +x /etc/rc.d/init.d/nNginx
Controlling Nginx
We have already started Nginx successfully. Let us see how to control it:
nginx -s stop
nginx -s quit
Reload nginx.conf
nginx -s reload
ngix -s reopen
nginx.conf is the configuration file and its location depends on how you have
installed Nginx.
Via source: if you have chosen the default path, then search for the subfolder
/conf inside /usr/local/nginx
Understand nginx.conf
Take a look at the file; you will find its layered structure.
Directive and Contexts are the two important terms to understand the configuration
file of Nginx.
main context
main is the first context that refers to the nginx.conf file itself.
worker_processes and user are the two essential directives contained within main
context.
The main context may have various sub contexts out of which events and http are
considered the two crucial sub contexts.
user: The user or group under which Nginx should run those configurations.
Nginx gives the optimal configuration automatically. Hence we can ignore this.
nginx -s reload
Modifying nginx.conf
Open the configuration file.
server {
location / {
root /data/www;
location /images/ {
root /data;
2 of 12
. . .
modifier is optional.
match defines what should be checked against the URI requested by the client.
Modifiers
No modifier: considered the prefix modifier. Ex: location / will match any URI
beginning with /.
= modifier: matches the exact URI. Ex: location /foo will match only
/foo URI.
~ modifier: case sensitive regular expression. Ex: location ~.(png | jpg | jpeg)$
will match any URI ending with .png or .jpg or .jpeg.
~* modifier: case insensitive regular expression. Ex: location ~.(png | jpg)$* will
match any URI ending with .png or .jpg or .PNG or .JPG.
^~ modifier: will match the prefix value. Ex: location ^~ /play/ will match any URI
beginning with /play/.
server {
location / {
proxy_pass http://localhost:8080/;
location ~ \.(gif|jpg|png)$ {
root /data/images;
server {
listen 8080;
root /data/play;
location / {
Install Apache2.
<VirtualHost 127.0.0.1:8000>
Install Nginx and configure it to pass the requests to Apache using the following
configuration.
server {
listen 80 default_server;
root /var/www/html;
server_name _;
location / {
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
What is CORS?
Cross-Origin Resource Sharing (CORS) is a way of accessing the restricted resources
of one domain from another domain.
The server must be capable of handling new request types with added headers for
CORS
if ($cors = 'true') {
if ($request_method = 'OPTIONS') {
add_header 'Content-Length' 0;
return 204;
Gzipping
1
Nginx offers Gzipping of JS, HTML and CSS files as a core feature which improves
your load time.
Pre-Gzipping
2
Instead of compressing files on each request, the site assets will be pre-
compressed and stored.
Video Streaming
5
FLV and MP4 modules can be used to stream videos with Nginx.