Nginx provides Web services and enables load balancing

Source: Internet
Author: User

############# #nginx直接作为web服务站点 ###########
Background: Set up two Web service sites for a single server
1)
MKDIR/WEB1/WEB2 # #建立二根个目录
Cd/web1
Vim index.html
www.web1.org # #随便写东西

Cd/web2
Vim index.html
www.web2.org # #随便写东西和第一个不一样就行
2)
vim/usr/local/nginx/conf/nginx.conf # #添加以下参数

server {
Listen 80;
server_name www.web1.org;
Location/{
Root/web1;
Index index.html;

}
}
server {
Listen 80;
server_name www.web2.org;
Location/{
ROOT/WEB2;
Index index.html;
}
}
3) Check and load the service
NGINX-T # #查看nginx服务是否配置成功
Nginx-s Reload # #重新加载nginx服务
4)
Vim/etc/hosts Write on IP parsing
172.25.8.1 server1.example.com www.web1.org www.web2.org
5) Testing
Curl www.web1.org
Return content: www.web1.org
Curl www.web2.org
Return content: www.web2.com
or open a browser to access: www.web1.org www.web2.org
(in which machine on which machine/etc/hosts add parsing 172.25.8.1 www.web1.org www.web2.org)


########## #nginx作为反向代理服务器实现负载均衡 #############
Nginx is used as a reverse proxy server, placed before two Apache, as a user access to the portal

Background: Server1 installed Nginx as a proxy server, Server2, 3 installation httpd services, providing real service
1) on the Server1
HTTP {
Upstream Web {# #在http段添加, the web is the name
Server 172.25.8.2:80;
Server 172.25.8.3:80;
}

vim/usr/local/lnmp/nginx/conf/nginx.conf Add the following
Note #root/web1; # #在server段注释
#index index.html;
Proxy_pass http://web; # #表示将所有请求转发到web服务器组中某一服务器上, this is the Load Balancer module, which is added in the server segment

2) Check and load the service
NGINX-T # #查看nginx服务是否配置成功
Nginx-s Reload # #重新加载nginx服务

3) in Server2, Server3
/ETC/INIT.D/HTTPD Start # #打开server2 Server3 's httpd service
Vim/var/www/html/index.html # #编辑文件
www.web2.org # #写入内容
Vim/var/www/html/index.html # #编辑文件
www.web3.org # #写入内容

4) Testing
Vim/etc/hosts
172.25.8.1 Web # #添加解析
Curl Http://web


/ETC/INIT.D/HTTPD # #关闭server2的httpd, Server3 can provide services




############# #Nginx的负载均衡策略 (only the first three types) ##############

Nginx's upstream currently supports 5 ways to allocate
1. Polling (default)
Each request is assigned to a different back-end server in chronological order, and can be automatically rejected if the backend server is down.
Upstream Web {
Server 192.168.0.14;
Server 192.168.0.15;
}

2, Weight
Specifies the polling probability, proportional to the weight and access ratios, for situations where the performance of the backend server is uneven.
Upstream Web {
Server 192.168.0.14 weight=5; # #数字大权重大
Server 192.168.0.15 weight=1;
}

3, Ip_hash
Each request is allocated according to the hash result of the access IP, so that each visitor has fixed access to a back-end server that resolves the session issue.
Upstream web{
Ip_hash;
Server 192.168.0.14:88;
Server 192.168.0.15:80;
}

4. Fair (third party)
The response time of the back-end server is allocated to the request, and the response time is short of priority allocation.
Upstream Web {
server Server1;
Server Server2;
Fair
}

5. Url_hash (Third Party)
Assign requests by the hash result of the access URL so that each URL is directed to the same back-end server, which is more efficient when the backend server is cached.
Upstream Westos {
Server squid1:3128;
Server squid2:3128;
Hash $request _uri;
Hash_method CRC32;
}

In servers that need to use load balancing, add
Proxy_pass http://web;
Upstream backserver{

Ip_hash;
Server 127.0.0.1:9090 down; (down indicates that the current server is temporarily not participating in the load)
Server 127.0.0.1:8080 weight=2; (weight defaults to 1.weight, the greater the load weight)
Server 127.0.0.1:6060;
Server 127.0.0.1:7070 backup; (When all other non-backup machines are down or busy, request the backup machine)
}

Max_fails: The number of times that a request failed is allowed defaults to 1. Returns the error defined by the Proxy_next_upstream module when the maximum number of times is exceeded
Fail_timeout:max_fails times after a failure, the time of the pause

This article from the "12148275" blog, reproduced please contact the author!

Nginx provides Web services and enables load balancing

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.