Centos nginx Load Balancing implementation

Source: Internet
Author: User

I. Topology



Ii. Install software

Wget http://nginx.org/download/nginx-1.0.11.tar.gz
Wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz

1. Install pcre

Tar zxvf pcre-8.21.tar.gz
Cd pcre-8.21
./Configure
Make & make install

2. Install nginx

Tar zxvf nginx-1.0.11.tar.gz
Cd nginx-1.0.11
Useradd-s/sbin/nologin www
./Configure -- user = www -- group = www -- prefix =/usr/local/nginx -- with-http_stub_status_module -- with-http_ssl_module
Make
Make install

3. Configure nginx

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

Vi nginx. conf
######################################## #######################################
User www;
Worker_processes 1;
Error_log/usr/local/nginx/logs/nginx_error.log crit;
Pid/usr/local/nginx. pid;
Worker_rlimit_nofile 51200;
######################################## ######################################
Events {
Use epoll;
Worker_connections 51200;
}
######################################## ######################################
Http {
Include mime. types;
Default_type application/octet-stream;
Sendfile on;
Tcp_nopush on;
Keepalive_timeout 60;
Tcp_nodelay on;

######################################## ######################################
Upstream 192.168.1.250 {
Server 192.168.1.htm;
Server 192.168.1.252;
}
######################################## ######################################
Server
{
Listen 80;
Server_name www.test.com * .test.com;

Location /{
Proxy_pass http: // 192.168.1.250;

Proxy_set_header Host $ host;

Proxy_set_header X-Real-IP $ remote_addr;

Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
}
Location/nginx_status {
Stub_status on;
Access_log off;
Allow 192.168.1.253;
Deny all;
}

######################################## #######################################
Log_format www_test_com '$ 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/nginx. log www_test_com;

}

}
######################################## ########################################

Iv. test whether the configuration is normal

[Root @ linux-1 conf] #/usr/local/nginx/sbin/nginx-t
Nginx: the configuration file/usr/local/nginx/conf/nginx. conf syntax is OK
Nginx: configuration file/usr/local/nginx/conf/nginx. conf test is successful

############################# The configuration is correct ######## ###########

5. Start and Stop nginx Server Load balancer

Start:

Ulimit-SHn 51200
/Usr/local/nginx/sbin/nginx

Stop:

Killall-9 nginx #### note that nginx can be started and stopped by writing a script.

Vi. Test

Web1 --------> ip: 192.168.1.20.------------> index.html -------------> the is web1

Web2 --------> ip: 192.168.1.252 ------------> index.html -------------> the is web2

Test results:




VII. simple use of the monitoring module (without the powerful ipvsadm like LVS, just a simple view)

Http: // 192.168.1.250/nginx_status


########################## Complete some simple load configuration ####### ################


8. briefly explain the configuration file (Please advise if there are any errors ):

Worker_processes 1; ### the general configuration is the same as the number of CPUs
Error_log/usr/local/nginx/logs/nginx_error.log crit; ### nginx error log usage

Use epoll; ### epoll Mode
Worker_connections 51200; ### maximum number of connections per process: 51200

Upstream 192.168.1.250 {
Server 192.168.1.htm;
Server 192.168.1.252;
############### This is the load, and the following is the back-end source server.

Location /{
Proxy_pass http: // 192.168.1.250;

Proxy_set_header Host $ host;

Proxy_set_header X-Real-IP $ remote_addr;

Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
########## Obtain the real IP address of the backend source Web server through X-Forwarded-

Location/nginx_status {
Stub_status on;
Access_log off;
Allow 192.168.1.253;
Deny all;
########### This is the simple monitoring of nginx status

This article from the "game technology blog" blog, please be sure to keep this source http://youzao.blog.51cto.com/3946111/751891


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.