Load balancing with nginx

Source: Internet
Author: User
I. Preparation: Nginx load server: Centos6.2IP: 192.168.1.93WEB server: Web1: 192.168.1.155Web2: 192.168.1.205 purpose: When a user accesses 192.168.1.93, load to WEB1 and WEB2 through Nginx ============

I. preparation:

Nginx load server:

Centos 1, 6.2

IP: 192.168.1.93

WEB server:

Web1: 192.168.1.155

Web2: 192.168.1.205

Objective: to load to WEB1 and WEB2 through Nginx when users access 192.168.1.93

========================================================== =====

II. configure the Nginx server load balancer

Configure the environment of WEB1 and WEB2 and upload the program. Make sure the data of the two servers is consistent!

1.1 disable Selinux

# Vi/etc/selinux/config

# SELINUX = enforcing change to disabled

#: Wq

# Shutdown-r now

2.2 File storage:

Software source code storage location:/usr/local/src

Remote compilation and installation location:/usr/local/software name

2.3 download the software package

Download nginx (Current stable version)

Http://nginx.org/download/nginx-1.0.15.tar.gz

Download pcre

Ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz

Download ngx_cache_purge (clear the specified URL cache for future expansion and configuration of nginx cache server)

Http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz

2.4 install and compile the Tool Library

# Yum install-y make apr * autoconf automake curl-devel gcc-c ++
Zlib-devel openssl-devel pcre-devel gd kernel keyutils patch perl
Kernel-headers compat * cpp glibc libgomp libstdc ++-devel
Keyutils-libs-devel libsepol-devel libselinux-develkrb5-devel
Zlib-devel libXpm * freetype libjpeg * libpng * php-common php-gd ncurses *
Libtool * libxml2 libxml2-devel patch

==============================================

III. Installation

# Groupadd www # Add a www Group

# Useradd-g www-s/bin/false # Create an nginx running account www and add it to www, www users are not allowed to log on to the system cd/usr/local/src # to enter the installation directory.

# Tar zxvf ngx_cache_purge-1.5.tar.gz # extract

# Tar zxvf nginx-1.0.15.tar.gz # extract

# Tar zxvf pcre-8.30.tar.gz # extract
# Cd nginx-1.0.15

#./Configure? Prefix =/usr/local/nginx? Without-http_memcached_module
? User = www? User = www? Group = www? With-http_stub_status_module
? With-openssl =/usr/
? With-pcre =/usr/local/src/pcre-8.30? Add-module = ../ngx_cache_purge-1.5 # configuration
Note :? With-pcre =/usr/local/src/pcre-8.30 points to the source package decompression path, while
It is not the installation path. Otherwise, an error is reported.
# Make # compile
# Make install # install
#/Usr/local/nginx/sbin/nginx # start Nginx

Enable nginx startup
# Echo "/usr/local/nginx/sbin/nginx">/etc/rc. local

========================================================== ====================

IV. configuration

Configure Nginx

# Backing up nginx configuration files

# Cp/usr/local/nginx/conf/nginx. conf/usr/local/nginx/conf/nginx. conf. bak

4.1 Set the nginx running account

# Vi/usr/local/nginx/conf/nginx. conf # Edit
Find the user nobody; change it to user www; # on the first line
4.2 disable nginx from null host headers
# Vi/usr/local/nginx/conf/nginx. conf # Edit and find the server and add the following content in the line above:
##############################
Server {
Listen 80 default;
Server_name _;
Location /{

Root html;
Return 404;
}
Location ~ /. Ht {
Deny all;
}
}
##############################

#/Usr/local/nginx/sbin/nginx-s reload # smoothly restart the nginx service

After the configuration is completed, the access from the bear host will jump directly to the nginx404 interface.

4.3 add nginx VM inclusion files

# Cd/usr/local/nginx/conf/# enter the nginx installation directory
# Mkdir vhost # Create a virtual directory
# Vi/usr/local/nginx/conf/nginx. conf # Edit
Find the code added in the previous step and add the following content at the end: include vhost/*. conf; for example:
##############################
Server {
Listen 80 default;
Server_name _;
Location /{
Root html;
Return 404;
}
Location ~ /. Ht {
Deny all;
}
}
Include vhost/*. conf;
##############################

4.4 add a Web server list file
# Cd/usr/local/nginx/conf/# enter the Directory
# Touch vhost. conf # Create a file
# Vi/usr/local/nginx/conf/nginx. conf # Edit
Locate the code added in the previous step and add a line below
Include vhost. conf;
4.5 set nginx global parameters
Vi/usr/local/nginx/conf/nginx. conf # Edit
Worker_processes 2; # Number of worker processes, which is the number of CPU cores or twice
Events
{
Use epoll; # Add
Worker_connections 65535; # Change to 65535, the maximum number of connections.
}
############ Add and modify the following code in http ##############
Server_names_hash_bucket_size 128; # Add
Client_header_buffer_size 32 k; # increase
Large_client_header_buffers 4 32 k; # increase
Client_max_body_size 300 m; # increase
Tcp_nopush on; # Change to on
Keepalive_timeout 60; # Change to 60
Tcp_nodelay on; # Add
Server_tokens off; # Added. nginx version information is not displayed.
Gzip on; # Change to on
Gzip_min_length 1 k; # increase
Gzip_buffers 4 16 k; # increase
Gzip_http_version 1.1; # Add
Gzip_comp_level 2; # Add
Gzip_types text/plain application/x-javascript text/css
Application/xml; # Add
Gzip_vary on; # Add

========================================================== ================

4.6 set the Web server list

# Cd/usr/local/nginx/conf/# enter the Directory
# Vi vhost. conf # edit and add the following code
Upstream osyunweihost {
Server 192.168.1.155: 80 weight = 1 max_fails = 2 fail_timeout = 30 s;
Server 192.168.1.205: 80 weight = 1 max_fails = 2 fail_timeout = 30 s;
Ip_hash;
}
4.7 create a VM configuration file

# Cd/usr/local/nginx/conf/vhost # enter the VM Directory
# Touch vhost. conf # Create a VM configuration file
# Vi vhost. conf # Edit
Server
{
Listen 80;
Server_name www.a.com bbs.a.com sns.a.com;
Location/
{
Proxy_next_upstream http_502 http_504 error timeout
Invalid_header;
Proxy_pass http: // ahost;
# Proxy_redirect off;
Proxy_set_header Host $ host;
Proxy_set_header X-Forwarded-For $ remote_addr;
}
Log_format access' $ remote_addr? $ Remote_user [$ time_local]
$ Request'
'"$ Status" $ body_bytes_sent
"$ Http_referer "'
'"$ Http_user_agent"
"$ Http_x_forwarded_for "';
Access_log/usr/local/nginx/logs/access. log access;
Location/NginxStatus {
Stub_status on;
Access_log on;
Auth_basic "NginxStatus ";
# Auth_basic_user_file pwd;
}
}
: Wq! # Save configuration
#/Usr/local/nginx/sbin/nginx-s relooad # restart nginx smoothly

========================================================== ============

V. test

Domain name:
Www.a.com
Bbs.a.com
Sns.a.com
Resolution to 192.168.1.93
When the customer accesses these three sites, Nginx will load balancer to the Web1 and Web2 servers based on the ip_hash value accessed by the customer.

So far, Nginx server load balancer configuration is complete.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.