Friday, February 17, 2012

Reverse Proxy With Nginx

As we know Nginx is event driven and uses persistent connections which makes it faster as compare to other Web / Proxy Servers. So, here's the simple example for setting up reverse proxy with NginX with some caching options:

Install Nginx http://apache-error.blogspot.in/2012/02/how-to-install-nginx-webserver.html

Create a file in /etc/nginx/conf.d/whatever.conf and enter following contents:



server {
    listen       80;
    server_name  server1.com www.server1.com ; # Server Names for which you want to make this server proxy


    access_log  off;
    error_log off;


    # proxy to Apache 2 and mod_python
    location / {
        proxy_pass         http://Backend_Server_IP:80/;
        proxy_redirect     off;


        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_max_temp_file_size 0;


        proxy_connect_timeout      60;
        proxy_send_timeout         60;
        proxy_read_timeout         60;


        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}

For Caching contents create a file /etc/nginx/conf.d/cache.conf and enter following contents:


## Size Limits
  client_body_buffer_size     128K;
  client_header_buffer_size   1M;
  client_max_body_size          1M;
  large_client_header_buffers 8 8k;


 ## Timeouts
  client_body_timeout   60;
  client_header_timeout 60;
  expires               24h;
  keepalive_timeout     60 60;
  send_timeout          60;


 ## General Options
  ignore_invalid_headers   on;
  keepalive_requests      100;
  limit_zone gulag $binary_remote_addr 5m;
  recursive_error_pages    on;
  sendfile                 on;
  server_name_in_redirect off;
  server_tokens           off;


 ## TCP options
  tcp_nodelay on;
  tcp_nopush  on;


 ## Compression
  gzip              on;
  gzip_buffers      16 8k;
  gzip_comp_level   6;
  gzip_http_version 1.0;
  gzip_min_length   0;
  gzip_types        text/plain text/css image/x-icon application/x-perl application/x-httpd-cgi;
  gzip_vary         on;


 ## Log Format
  log_format  main  '$remote_addr $host $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" "$http_user_agent" '
                    '"$gzip_ratio" "$http_x_forwarded_for"';


This cache.conf will be used by default for all your VHOSTS content caching, so you can manage accordingly :)

Start nginx service : service nginx start


Please correct if find somewhere mis-spelled or incorrect!!!

Wednesday, February 1, 2012

How to Install Nginx WebServer


How to Install Nginx Web Server using Yum

Add Nginx yum repository, create a file named /etc/yum.repos.d/nginx.repo and paste one of the configurations below:

For CentOS

CentOS:[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

For Redhat

RHEL:[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
gpgcheck=0
enabled=1

Due to differences between how CentOS, RHEL, and Scientific Linux populate the $releasever variable, it is necessary to manually replace $releasever with either "5" (for 5.x) or "6" (for 6.x), depending upon your OS version.

Now Run following command to install NginX Web Server:

yum -y install nginx

Start NginX Service : service nginx start


How to Install Nginx Web Server using source code

Download stable source code from http://nginx.org/download/nginx-1.0.11.tar.gz

Unzip source code : tar zxvf nginx-1.0.11.tar.gz

Unzipped directory should be nginx-1.0.11:

cd nginx-1.0.11

Run following commands to install:

./configure --prefix=/opt/nginx      (--prefix=[path where to install].... use ./configure --help to find more options to enable or disable features)

make

make install

Start Nginx Web Server : /opt/nginx/bin/nginx
Stop Nginx Web Server : /opt/nginx/bin/nginx -s stop
Nginx Logs and PID file : /opt/nginx/logs/{access.log}{error.log}{nginx.pid}

Thats it!!!