[Web server] Hướng dẫn cài đặt Nginx, setup Load Balancer, Replication Session trên Ubuntu Server 12.04

Nginx là một máy chủ web (web server), proxy ngược (reserve proxy) và e-mail proxy (IMAP/POP3) nhẹ, hiệu năng cao, sử dụng giấy phép kiểu BSD. Nó có thể chạy trên UNIX, Linux, các dòng BSD, Mac OS X, Solaris và Microsoft Windows.
Bài viết này sẽ hướng dẫn các bạn cài đặt Nginx, setup Load Balancer, Replication Session trên Ubuntu Server 12.04.
1. Cài đặt Nginx
sudo apt-get install nginx
2. Cấu hình Virtual Host
cd /etc/nginx/sites-available
Tạo virtual host nginx.local
cp default nginx.local
Thêm vào file nginx.local nội dung sau:
...
server {
        listen   80 default;
# Day la ten virtual host
        server_name  nginx.local;
        access_log  /var/log/nginx/localhost.access.log;
        location / {
#Chú ý, port ở đây là port html
        proxy_pass http://localhost:8080;
        }
}
...

Enable file virtual host ta vừa tạo
sudo ln -s /etc/nginx/sites-available/nginx.local /etc/nginx/sites-enabled/
Sau đó ta xóa file defaul trong sites-enabled đi.
Xong ta reload lại nginx
sudo /etc/init.d/nginx reload
Tiếp theo ta chỉnh sửa file server.xml trong tomcat.
Thêm proxyPort="80" proxyName="nginx.local"/ như trong hình dưới
    <Connector URIEncoding="UTF-8" port="8085" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" proxyPort="80" proxyName="nginx.local"/>
Ta khai host và test lại.


3. Cài đặt HTTPS
Thực hiện các bước sau
Step 1. Cài đặt trình biên dịch ggc
apt-get install gcc
Step 2. Cài đặt thư viện PCRE
apt-get install libpcre3 libpcre3-dev
Step 3. Cài đặt openssl
sudo apt-get install libssl-dev
Step 4. Biên dịch Nginx
Download Nginx (http://linux.softpedia.com/get/Internet/Proxy/nginx-28901.shtml)
Giải nén, truy cập vào folder source nginx đã giải nén, thực hiện các lệnh sau:
./configure --with-http_ssl_module
make && make install
Thử lệnh apt-get install openssl để có thể bỏ qua 4 bước trên

Step 5. Tạo khóa
mkdir /srv/ssl/
cd /srv/ssl/
openssl req -new -x509 -days 365 -nodes -out /srv/ssl/nginx.pem -keyout /srv/ssl/nginx.key
Common Name: điền tên domain
Step 6. Cấu hình file virtual host
Thêm đoạn này vào trong file virtual host nginx.local
listen 443;
      ssl on;
      ssl_certificate      /srv/ssl/nginx.pem;
      ssl_certificate_key  /srv/ssl/nginx.key;

Reload lại Nginx. Kiểm tra kết quả


4. Cấu hình Load Balacing
Backup file nginx.conf trong /etc/nginx/
Mở file /etc/nginx/nginx.conf, xóa toàn bộ nội dung, thay thế bằng nội dung sau:
user www-data;
worker_processes  1;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
    # multi_accept on;
}
http {
upstream nginx.local {
server 192.168.2.60:8080; #weight=3
server 192.168.2.61:8080;
}
server {
listen 80;
server_name nginx.local;
location / {
proxy_pass http://nginx.local;
}
}
}


Ta cấu hình file virtual host như sau:
# You may add here your
# server {
#       ...
# }
# statements for each of your virtual hosts
server {
        listen   80;
        server_name  nginx.local;
        access_log  /var/log/nginx/server1.access.log;
        location / {
        proxy_pass http://192.168.2.60:8080;
        proxy_intercept_errors on;
        proxy_redirect     on;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
         }
}
server {
#        listen   80;
        server_name  nginx.local;
        access_log  /var/log/nginx/server2.access.log;
        location / {
        proxy_pass http://192.168.2.61:8080;
        proxy_intercept_errors on;
        proxy_redirect     on;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
}

5. Cấu hình Replication Session
Note: ta thực hiện như nhau trên cả 2 server. Bắt buộc phải load balancer 2 server Tomcat trước khi thực hiện.
Step 1. Config file server.xml
Thêm đoạn sau vào dưới    <Engine name="Catalina"....>
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="8">
          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"/>
          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="auto"
                      port="4000"
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>
            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
          </Channel>
          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=""/>
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>
          <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>


Step 2: <distributable/>
Mở  file web.xml theo đường dẫn sau:
$TOMCAT_HOME/tomcat-7.0.23/webapps/ROOT/WEB-INF/web.xml
Theo dòng này vào giữa <web-app> </web-app>
<web-app>
  <distributable/>
. . .
 </web-app>

Step 3: Config lại file hosts
sudo nano /etc/hosts
chỉnh lại 127.0.1.1 thành IP_Address của máy

TÀI LIỆU THAM KHẢO

http://prasadl.livejournal.com/2333.html
http://prasadl.livejournal.com/tag/nginx%20%2B%20tomcat%20using%20mod_jk
http://quantrilinux.com/diendan/Thread-nginx-co-ban--37
http://www.quantrimang.com.vn/m/linux/83117.aspx
http://library.linode.com/web-servers/nginx/configuration/ssl
http://www.geeksww.com/tutorials/libraries/openssl/installation/installing_openssl_on_ubuntu_linux.php
http://www.thegeekstuff.com/2011/07/install-nginx-from-source/
https://www.digitalocean.com/community/articles/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3
http://serverfault.com/questions/172542/configuring-nginx-for-use-with-tomcat-and-ssl