Ngnix The Basics
Ngnix The Basics
Ngnix The Basics
The moment you press enter, your browser displays the home page of Google.
Web Resource: Any static or dynamic file (HTML/JSON/PDF/..), served by a web application.
Each resource has its unique URL.
Protocol: A set of rules agreed by both the parties for standardizing the communication.
Need of a Protocol
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.
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.
The C10K problem is the main reason why the next giant of the field
called Nginxgrabbed the stage.
What is C10K Problem?
Threaded servers like Apache cannot handle 10K connections because,
So, the engineers started moving away from threaded servers to event-driven servers like
Nginx
Quick Fact
We are not far away from the C10M problem!
o FastCGI
o uwsgi
o SCGI
It improves the performance, reduces the latency and also maximizes the throughput.
o Round Robin
o Least Connected
o IP hash
Nginx Versions
Nginx has three versions:
Development: Highly recommended as it includes fixes for bugs in the stable version.
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.
Only one worker can be used at a time.
Windows Vista or later versions do not support cache module.
Obstacles:
From apt:
From yum:
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
If the native package is old, it is good to install from binary packages provided by Nginx
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
Here, the codename can be substituted with lucid, quantal , oneiric or precise
accordingly.
apt-get update
apt-get install nginx
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
Edit the path as per your system in the file and run the following command:
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
o 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.
Each block defined within a pair of curly braces {} is a context.
Directive and Contexts are the two important terms to understand the configuration file
of Nginx.
Contexts:
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.
Modifying nginx.conf
Open the configuration file.
Find the http block.
Add a server block to it.
Add two location blocks to it to serve html files and images.
server {
location / {
root /data/www;
location /images/ {
root /data;
. . .
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.
server {
location / {
proxy_pass http://localhost:8080/;
location ~ \.(gif|jpg|png)$ {
root /data/images;
server {
listen 8080;
root /data/play;
location / {
Install Apache2.
Listen 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;
}
Reload Nginx and ping localhost from your browser.
What is CORS?
Cross-Origin Resource Sharing (CORS) is a way of accessing the restricted resources
of one domain from another domain.
Traditionally CORS was restricted by web browsers for security reasons.
Modern web browsers support client side elements for CORS.
The server must be capable of handling new request types with added headers for
CORS
if ($http_origin ~ '^https?://(www.play.com|www.talk.com)') {
if ($cors = 'true') {
if ($request_method = 'OPTIONS') {
add_header 'Content-Length' 0;
return 204;
Gzipping
1
You are now ready to start hosting a web application from your local machine, convert Nginx
as a reverse proxy for other web servers and more.
QUIZ: