Consider the code given below:
server {
listen 80 default_site;
root /var/www;
index index.html index.htm; !
server_name server_name.com http://www.server_name.com; !
location / {
try_files $uri $uri/ =404;
}
}
The above code shows a basic configuration of Nginx so as to serve files on a particular website. It listens on port number 80, which is just a regular HTTP port. We must tell it what the default site is.
#########################################################################
With Nginx, requests can be sent to HTTP listeners, to WSGI listeners, to fastCGI listeners, and communication with memcache can be done directly. Some fancy caching can be done in HTTP as shown below:
location /static {
try_files $uri $uri/ =404;
}
location / {
proxy_pass 127.0.0.1:9000;
proxy_param APPENV production;
include proxy_params;
}
#########################################################################
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
# Or:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param APPENV production;
include fastcgi.conf;
}
Note that with PHP-FPM, listening to ports is done by use of fastCGI rather than using HTTP. However, in our case above, we are listening to the port number 9000 which is located on the local host. An HTTP request will not be accepted in the above case. Only requests which are made in fastCGI will be accepted, and this specifies the standard way how PHP files are processed.
#######################################################
The type of server information which PHP is to use has also been specified.
location /static {
try_files $uri $uri/ =404;
}
location / {
uwsgi_pass 127.0.0.1:9000;
uwsgi_param APPENV production;
include uwsgi_params;
}
Lastly, the HTTP request is taken away by WSGI Nginx, in which it is converted into the correct protocol that the gateway uses, and then it is sent off to the gateway. The gateway will then communicate with the application where the request is processed, and then the response is sent back
#######################################################
Nginx as a Load Balancer
Nginx makes a good load balancer, just like other software such as HAProxy. It can be configured as follows for the purpose of load balancing:
upstream myapp {
zone backend 64k;
least_conn;
server 127.0.0.1:9000 max_fails=2 fail_timeout=30s;
server 127.0.0.1:9001 max_fails=2 fail_timeout=30s;
server 127.0.0.1:9002 max_fails=2 fail_timeout=30s;
}
server {
location / {
health_check;
include proxy_params;
proxy_pass http://myapp/;
# handling of the web socket connections
proxy_http_version 1.1;
proxy_set_header Upgrade $hupgrade;
proxy_set_header Connection “upgrade”;
}
}
Upstream are the load balancers to be load balanced within. Note that we have only three servers, and all are listening to port number 9000. Consider the code given below:
server {
location / {
health_check;
include proxy_params;
proxy_pass http://myapp/;
# Handling of the Web Socket connections
proxy_http_version 1.1;
proxy_set_header Upgrade $hupgrade;
proxy_set_header Connection “upgrade”;
}
Our aim is to stop the Nginx from sending its requests to the servers which might have broken down. That is why we have the above parameter. The Health check is responsible for checking this.
#######################################################
The next step should be the creation of a virtual host under “/etc/nginx/sites-available/website.” This is shown below:
server {
listen 80;
server_name _;
server_tokens off;
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $h;
proxy_set_header X-Real-IP $r_addr;
proxy_set_header X-Forwarded-For $p_add_x_forward_for;
proxy_cache my-cache;
proxy_cache_valid 3s;
proxy_no_cache $cookie_PHPSESSID;
proxy_cache_bypass $cookie_PHPSESSID;
proxy_cache_key “$scheme$host$request_uri”;
add_header X-Cache $upstream_cache_status;
}
}
server {
listen 8080;
server_name _;
root /var/www/root_for_document/;
index index.php index.html index.htm;
server_tokens off;
location ~ .php$ {
try_files $uri /index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~ /.ht {
all denied;
}
}
