Nginx多域名负载均衡配置
Nginx负载均衡设置
环境:
负载均衡:192.168.188.128:80
Web1:192.168.188.128:81
Web2:192.168.188.129:80
正式环境中,需要解析域名www.doubles.cn、abc.dd.cn到负载均衡机器192.168.188.128,我们现在测试,就直接在本地windows下的hosts里面绑定域名:
192.168.188.128 www.doubles.cn
192.168.188.128 abc.dd.cn
1、单个域名的负载均衡
1.1、在web1(192.168.188.128)上搭好web环境:
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
...
include vhost/*.conf;
...
}
在http{}最下面添加include vhost/*.conf;每个域名对应一个conf文件。
新建vhost目录。
[root@localhost conf]# mkdir /usr/local/nginx/conf/vhost/
新建www.doubles.cn.conf文件:
[root@localhost conf]# vim /usr/local/nginx/conf/vhost/www.doubles.cn.conf
server {
listen 81;
server_name www.doubles.cn localhost 192.168.188.128;
location / {
root /usr/local/nginx/html/;
index index.html index.php index.htm TempLoginPanel.html;
}
location ~ \.php$ {
root /usr/local/nginx/html/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在/usr/local/nginx/html/里面写好html文件:
[root@localhost conf]# vim /usr/local/nginx/html/index.html
#测试内容自定义
……
重新加载nginx配置文件
[root@localhost 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
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
测试web1:
1.2、在web2(192.168.188.129)上搭好web环境:
按照1.1的方法同样搭建web2的环境,
新建虚拟主机:
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
...
include vhost/*.conf;
...
}
[root@localhost conf]# mkdir /usr/local/nginx/conf/vhost/
[root@localhost conf]# vim /usr/local/nginx/conf/vhost/www.doubles.cn.conf
server {
listen 80;
server_name www.doubles.cn localhost 192.168.188.129;
location / {
root /usr/local/nginx/html/;
index index.html index.php index.htm TempLoginPanel.html;
} location ~ \.php$ {
root /usr/local/nginx/html/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在/usr/local/nginx/html/里面写好html文件:
[root@localhost conf]# vim /usr/local/nginx/html/index.html
#测试内容自定义
……
重新加载nginx配置文件
[root@localhost 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
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
测试如下:
注意:正式环境当中,web1和web2机器上面的网页内容应该是一致的,才能做负载均衡。这里为了区分两台机,所以在网页内容中区分了下。
1.3、在负载均衡机器(192.168.188.128)上:
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
...上面的省略...
upstream doublesweb {
#ip_hash;
server 192.168.188.128:81;
server 192.168.188.129:80;
} server {
listen 80;
server_name localhost;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://doublesweb;
proxy_connect_timeout 2s;
}
....略...
(注意):这里upstream与proxy_pass的名字必须一致,这里都是doublesweb。
重新加载nginx配置文件
[root@localhost 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
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
测试:
在windows上打开浏览器:输入网址www.doubles.cn,发现已经可以轮询web1和web2了
再刷新:
2、多个域名的负载均衡
在1中我们只对www.doubles.cn做了负载均衡,实际环境中我们可能需要对多个域名进行负载均衡。这里我们添加一个abc.dd.cn。
2.1、web1上面新建虚拟主机abc.dd.cn
增加网页目录/usr/local/nginx/html/dd/:
[root@localhost html]# mkdir /usr/local/nginx/html/dd/
[root@localhost html]# vim /usr/local/nginx/html/dd/index.html
添加一个abc.dd.cn.conf的虚拟主机:
[root@localhost html]# vim /usr/local/nginx/conf/vhost/abc.dd.cn.conf
server {
listen 81;
server_name abc.dd.cn;
location / {
root /usr/local/nginx/html/dd/;
index index.html index.php index.htm TempLoginPanel.html;
} location ~ \.php$ {
root /usr/local/nginx/html/dd/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重新加载nginx配置文件
[root@localhost 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
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
2.2、在web2上新建虚拟主机abc.dd.cn
跟web1同样的操作:
增加网页目录/usr/local/nginx/html/dd/:
[root@localhost html]# mkdir /usr/local/nginx/html/dd/
[root@localhost html]# vim /usr/local/nginx/html/dd/index.html
添加一个abc.dd.cn.conf的虚拟主机:
[root@localhost html]# vim /usr/local/nginx/conf/vhost/abc.dd.cn.conf
server {
listen 80;
server_name abc.dd.cn;
location / {
root /usr/local/nginx/html/dd/;
index index.html index.php index.htm TempLoginPanel.html;
} location ~ \.php$ {
root /usr/local/nginx/html/dd/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重新加载nginx配置文件
[root@localhost 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
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
2.3、配置负载均衡
在负载均衡机器(192.168.188.128)上:
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
...
upstream www.doubles.cn {
#ip_hash;
server 192.168.188.128:81;
server 192.168.188.129:80;
} upstream abc.dd.cn {
#ip_hash;
server 192.168.188.128:81;
server 192.168.188.129:80;
} server {
listen 80;
server_name localhost;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://$host;
proxy_connect_timeout 2s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
.....
(注意):这里upstream后面的名字必须跟你访问的域名保持完全一致,否则将无法代理。因为proxy_pass是根据$host来匹配的。
重新加载nginx配置文件
[root@localhost 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
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
测试:
再刷新:
测试之前的域名:
仍然可以进行负载均衡,所以生效。
Nginx多域名负载均衡配置的更多相关文章
- Nginx 简单的负载均衡配置示例(转载)
原文地址:Nginx 简单的负载均衡配置示例(转载) 作者:水中游于 www.s135.com 和 blog.s135.com 域名均指向 Nginx 所在的服务器IP. 用户访问http://www ...
- nginx安装及负载均衡配置
Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗斯访问量第二 ...
- Centos7.4 Nginx反向代理+负载均衡配置
Ningx是一款高性能的HTTP和反向代理服务器,配置起来也比较简单. 测试环境: 172.16.65.190 Nginx-反向代理 172.16.65.191 Ningx-Web 172.16.65 ...
- 在Nginx中做负载均衡配置的实例讲解
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法. 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某 ...
- Nginx 简单的负载均衡配置示例
http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html 在此记录下Nginx服务器nginx.conf的配置文件说明, ...
- Linux 下 Nginx 反向代理 负载均衡配置
转载请注明出处:http://blog.csdn.net/smartbetter/article/details/52036350 上一篇分享了 Nginx + JDK + Tomcat + MySQ ...
- Nginx 简单的负载均衡配置演示样例
近期在做开放查询应用的时候,因为数据两天特别多,两千多万条呢,用户訪问需求也比較大,所以就用nginx做了 负载均衡,以下是改动之后的相关内容. http://www.cnblogs.com/xiao ...
- Nginx代理与负载均衡配置与优化
Nginx代理 Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.Nginx的Web缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反 ...
- [转]Nginx+mysql+php-fpm负载均衡配置实例
转 : http://www.jbxue.com/article/7923.html 介绍一个nginx.mysql.php-fpm环境下配置负载均衡的例子,有需要的朋友,可以参考下. 系统环境如下: ...
随机推荐
- 部署和调优 1.1 nfs部署和优化-1
NFS服务会经常用到,用于在网络上共享存储.举一个例子来说明一下 NFS .假如有三台机器 A.B.C,它们需要访问同一个目录,目录中都是图片,传统的做法是把这些图片分别放到 A.B.C.但是,若使用 ...
- Linux 下安装redis
记录一下linux下的安装步骤,还是比较复杂的 1. 下载redis-2.8.19.tar.gz: ftp传到linux01上: 解压: tar –zxvf redis-2.8.19.tar.gz 2 ...
- 将字符串str1复制为字符串str2的三种方法
1.自己编写函数,将两个字符串进行复制 #include<iostream> using namespace std; int main(){ char str1[]="I lo ...
- 生产者与消费者-1:1-基于list
一个生产者/一个消费者: /** * 生产者 */ public class P { private MyStack stack; public P(MyStack stack) { this.sta ...
- C++中的纯虚函数和虚函数的作用
1. 虚函数和纯虚函数可以定义在同一个类(class)中,含有纯虚函数的类被称为抽象类(abstract class),而只含有虚函数的类(class)不能被称为抽象类(abstract class) ...
- CentOS6.5 安装python
前言: CENTOS 6.X 系列默认安装的 Python 2.6 ,目前开发中主要是使用 Python 2.7 ,这两个版本之间还是有不少差异的,程序在 Python 2.6 下经常会出问题. 比如 ...
- SPOJ LCMSUM - LCM Sum
题意是求: $\sum_{i = 1}^{n}lcm(i, n)$ $= \sum_{i = 1}^{n}\frac{ni}{gcd(i, n)}$ $= n\sum_{i = 1}^{n}\frac ...
- CF321E Ciel and Gondolas & BZOJ 5311 贞鱼
一眼可以看出$O(kn^{2})$的$dp$方程,然后就不会了呜呜呜. 设$f_{i, j}$表示已经选到了第$i + 1$个数并且选了$j$段的最小代价,那么 $f_{i, j} = f_{p, j ...
- Ubuntu学习小结(二)PostgreSQL的使用,进程的查看关闭,编辑器之神Vim入门
距离上次发布文章已经过去了很久.在过去的半年中,虽然写的代码不多,但是在接触了计算机一些其他的知识,包括数据库.网络之后,感觉能够融会贯通,写代码水平又有了一定的提高.接下来,将会发表几篇文章,简单介 ...
- SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)
Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...