Nginx Proxy & Load Balance & LNMP

Nginx Proxy Service

E-commerce website
Server 1: provide image request response ( apache / nginx )
Server 2: provide dynamic request response ( php-fpm / apache-php )
Server 3: provide static request response ( apache / nginx )
Server 4: provide data request response ( mysql )

It need one additional nginx server
1. Install nginx
2. proxy to specific server

Example
location / {
    proxy_pass http://192.168.199.187:80/;
}

location /bbs/ {
    proxy_pass http://192.168.199.187:80/bbs/;
}

location ~* \.(jpg|png|gif)$  {
    proxy_pass http://172.16.100.7;
}

Enhance the log information and specific the hostname
location / {
    proxy_pass http://192.168.199.187:80/;
    proxy_set_header <parameter name>  $host;  (client requst the hostname )    proxy_set_header <parameter name> $remote_addr; (client IP address)
}

Example
location / {
    proxy_pass http://192.168.199.187:80/;
    proxy_set_header C_IP $remote_addr;
}

If we want to record the client IP address, need to change the httpd config file.
change from
LogFormat "%h 
to
LogFormat "%{C_IP}

Document
nginx document: nginx.org/en/docs
nginx proxy document: ngx_http_proxy_module

Nginx Load Balance --> Enhance the performance

E-commerce website
Server 1: provide image request response ( apache / nginx )
Server 2: provide dynamic request response ( php-fpm / apache-php )
Server 3: provide static request response ( apache / nginx )
Server 4: provide data request response ( mysql )

Running after some time, the Server 1 load quite high. 
So we add another server to process image request.
The new server path and file exactly same as Server 1

1. How to distribute image request to two or more servers
Solution: Need ngx_http_upstream_module
2. How to distribute dynamic request to two or more servers (http do not save the previous states)
Solution A: Using cookie / ip_hash to identify clients --> session bind with specific php server 

Load Balance test for image servers
Format 
upstream <name> {
    server address [parameters];
    ip_hash;
}
proxy_pass http://<name>

Descrption: 
parameters: define the server load balance mode: one by one / 75% to 25%
ip_hash: bind specific server

Example: two image server one by one response requests
upstream image {
    server 172.168.100.7;
    server 172.168.100.8;
}
proxy_pass http://image/

Test steps:
Env: three servers 1 nginx (proxy) + 2 httpd (image)
1. set two image servers with httpd service;
2. stop iptables on both two image servers;
3. copy the image resource on both sides
4. start the httpd service on both two servers;
5. go to nginx server, change the config file /etc/nginx/nginx.conf
put following in http part
upstream image {
    server 192.168.199.187;
    server 192.168.199.179;
}
put following part in location
proxy_pass http://image/;
6. Restart nginx service;
7. Create specific home page in both image servers;
8. Check the home page;
9. Change the nginx.conf to change the distribution;
from
upstream image {
    server 192.168.199.187;
    server 192.168.199.179;
}
to
upstream image {
    server 192.168.199.187 weight=2;
    server 192.168.199.179;
}

Test steps:
Env: three servers 1 nginx (proxy) + 2 lamp
1. setup both two lamp server with yum installation;
2. create the php mysql connection test page index.php;
3. change the nginx server nginx.conf 
from
upstream image {
    server 192.168.199.187;
    server 192.168.199.179;
}
to
upstream image {
    server 192.168.199.187;
    server 192.168.199.179;
    ip_hash;
}
4. Change the index.php add IP address;

Nginx fastcgi module --> LNMP
1. install mysql
yum install mysql-server mysql
2. install nginx
yum install php-fpm php-mysql
4. Check the php-fpm install files: rpm -ql php-fpm
5. Check the /etc/php-fpm.d/www.conf
A. listen
B. listen.allowed_clients
6. start mysql, php-fpm and nginx
service mysld start
service php-fpm start
service nginx start
7. create home page in /usr/local/nginx/html/index.php
8. change the config file /etc/nginx/nginx.conf 
Remove omit for the php-fpm part
9. Change the context of fastcgi_params in /etc/nginx
to
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
10. restart nginx service 
11. check the index.php homepage

Comments

Popular posts from this blog

Snort+barnyard2+Snorby CentOS 6.5_64 Installation

ORACLE Error