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!!!

Wednesday, December 28, 2011

Apache vs NginX


Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption

Nginx is one of a handful of servers written to address the C10K problem. Unlike traditional servers, Nginx doesn't rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.

Even if you don't expect to handle thousands of simultaneous requests, you can still benefit from Nginx's high-performance and small memory footprint. Nginx scales in all directions: from the smallest VPS all the way up to clusters of servers.

Nginx particularly excels at serving static files—like the Tectonicus map tile images. For larger websites, it's often employed as a front-end Web server to quickly dish up unchanging page content, while passing on requests for dynamic stuff to more complex Apache Web servers running elsewhere. However, I was interested in it purely as a fast single Web server.

http://joeandmotorboat.com/2008/02/28/apache-vs-nginx-web-server-performance-deathmatch/

http://www.wikivs.com/wiki/Apache_vs_nginx

Apache

The drawback to doing everything with processes is that Apache prefork can be a bit of a memory hog, especially under load. Another precompiled flavor of Apache can be installed as an alternative: Apache MPM worker. "Worker" differs from "prefork" in that worker's processes are multithreaded, giving them the ability to service more requests with fewer system resources. This can translate into faster pages served with less RAM and CPU. However, because some Apache modules don't necessarily work well when run under multithreaded Apache, you have to specifically select this version to install on Ubuntu and on other GNU/Linux distros with package management.

Alternatively we can use FastCGI with Apache to handle PHP requests. FastCGI can use a single persistent process which handles many requests over its lifetime

http://www.cyberciti.biz/tips/rhel-centos-fedora-apache2-fastcgi-php-configuration.html

Other Surveys for All Web Servers

http://news.netcraft.com/archives/2011/04/06/april-2011-web-server-survey.html

Friday, December 2, 2011

Apache Questions

Q1 How to enable Directory Listing

Ans Place "Options +Indexes" in Directory Tabs

Q2 Allow Apache to use / access symbolic links directory/files

Ans Place "Options +FollowSymLinks" in Directory Tabs

Q3 Allow Apache to use / access .htaccess file

Ans Place "AllowOverride All" in Directory Tabs

Q4 Can we replace .htaccess file to some other for our convenient

Ans Yes, change "AccessFileName .htaccess" to "AccessFileName .xyz"

Q5 Set default number of start processes

Ans Find "StartServers ..." and set as per your requirement

Q6 How to disable Apache signature/version

Ans ServerSignature Off

Q7 How to block/disable TRACE

Ans Use "TraceEnable Off" globally so that TRACE will be disabled for all VHOSTS

Q8 Best tools for monitoring Apache Requests

Ans use Apache Handler "server-status" and APACHETOP tool (its like TOP command in linux)

Q9 How can we protect our site to avoid DOS/DDOS attacks

Ans Use mod_evasive apache module or this can also be done through firewall

Q10 Redirect abc.com to www.abc.com

Ans Use following in abc.com VHOST : - "Redirect / http://www.abc.com/"

Q11 Which Apache processes are consuming higher memory

Ans Use ApacheTop or "ps -ylC httpd --sort rss" this command will show all apache processes in ascending order according to \memory size used by per process (RSS field)

Q12 What are different MPM's available in Apache for Linux and how they work

Ans There are 2 MPMs available 1) Worker 2) Prefork

     

        Worker :- This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-based server. However, it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.

The most important directives used to control this MPM are ThreadsPerChild, which controls the number of threads deployed by each child process and MaxClients, which controls the maximum total number of threads that may be launched

      Prefork :- This Multi-Processing Module (MPM) implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other.

This MPM is very self-regulating, so it is rarely necessary to adjust its configuration directives. Most important is that MaxClients be big enough to handle as many simultaneous requests as you expect to receive, but small enough to assure that there is enough physical RAM for all processes.


Q13 Write a Rewrite rule to redirect (www).abc.com/request.php to www.abc.com/request1.php

Ans RewriteEngine On
       RewriteCond %{HTTP_HOST} ^abc.com$ [NC,OR]
       RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
       RewriteRule ^/request.php$ http://www.abc.com/request1.php [L]

Q14 How many connections apache server can handle

Ans This completely depends on our server configuration mainly on RAM

        Max no. of connections = (Total Memory - Memory Used By System For Its use) / average single apache process size*

        *Average Single Apache Process Size : Can be find using "top" command or "ps -ylC httpd --sort rss"


To be continued....

Tuesday, March 29, 2011

Install Apache module without re-installation with APXS

In case we have installed Apache using source code and we would like to install one of its modules called mod_rewrite or whatever. Then we need to do the following to install module without re-installing Apache:




cd apache-source-code
cd modules/mappers

/path/to/apache2/bin/apxs -iac mod_rewrite.c


Add following to httpd.conf file:


LoadModule rewrite_module modules/mod_rewrite.so




Then Restart Apache, thats it!!!

Tuesday, March 15, 2011

Apache Virtual Hosting


What is Apache Virtual Hosting?

When setting up more then One Host/Site over Single Server, this is called Virtual Hosting.

Virtual Hosting can be of 2 type's:

1) Name Based Virtual Hosting
2) IP Based Virtual Hosting

1) Name Based Virtual Hosting : Running multiple virtual hosts over a Single IP is called Name Based Virtual Hosting.

2) IP Based Virtual Hosting : When a Server is having multiple IP's and per IP is having it's own Virtual Host is called IP Based Virtual Hosting.


How to Setup Name Based Virtual Hosting and IP Based Virtual Hosting?

Name Based Virtual Host :

edit /etc/httpd/conf/httpd.conf

Listen 192.168.1.1:80

NameVirtualHost 192.168..1.1:80

VirtualHost 192.168..1.1:80
ServerName localhost
ServerAlias localhost.localdomain
DocumentRoot /var/www/html

Directory /var/www/html
AllowOverride None
Options -Indexes -FollowSymLinks
Order allow, deny
Allow from All
/Directory

CustomLog logs/localhost_access.log common
ErrorLog logs/localhost_error.log
/VirtualHost


VirtualHost 192.168..1.1:80

ServerName fistsite.com
ServerAlias www.firstsite.com
DocumentRoot /var/www/html/firstsite

Directory /var/www/html/firstsite
AllowOverride None
Options -Indexes -FollowSymLinks
Order allow, deny
Allow from All
/Directory
CustomLog logs/firstsite_access.log common
ErrorLog logs/firstsite_error.log
/VirtualHost
------------------------------------------------------------------------------------------------

IP Based Virtual Host:


edit /etc/httpd/conf/httpd.conf

Listen 192.168.1.1:80
Listen 192.168.1.2:80


VirtualHost 192.168..1.1:80
ServerName localhost
ServerAlias localhost.localdomain
DocumentRoot /var/www/html

Directory /var/www/html
AllowOverride None
Options -Indexes -FollowSymLinks
Order allow, deny
Allow from All
/Directory

CustomLog logs/localhost_access.log common
ErrorLog logs/localhost_error.log
/VirtualHost


VirtualHost 192.168..1.2:80
ServerName fistsite.com
ServerAlias www.firstsite.com
DocumentRoot /var/www/html/firstsite

Directory /var/www/html/firstsite
AllowOverride None
Options -Indexes -FollowSymLinks
Order allow, deny
Allow from All
/Directory
CustomLog logs/firstsite_access.log common
ErrorLog logs/firstsite_error.log
/VirtualHost

VirtualHost 192.168..1.3:80
ServerName second.com
ServerAlias www.second.com

DocumentRoot /var/www/html/second

Directory /var/www/html/second
AllowOverride None
Options -Indexes -FollowSymLinks
Order allow, deny
Allow from All
/Directory

CustomLog logs/second_access.log common
ErrorLog logs/second_error.log
/VirtualHost

-------------------------------------------------------------------------------------------------