微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx 反向代理/负载均衡

Nginx绑定多个IP

1、将/etc/sysconfig/network-scripts/ifcfg-eth0文件复制一份,命名为ifcfg-eth0:1
修改其中内容
DEVICE=eth0:1
IPADDR=192.168.1.3
其他项不用修改
2、重启系统

配置基于ip的虚拟主机:

#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#配置虚拟主机192.168.101.3
server {
#监听的ip和端口,配置192.168.1.3:80
listen 80;
#虚拟主机名称这里配置ip地址
server_name 192.168.1.3;
#所有的请求都以/开始,所有的请求都可以匹配此location
location / {
#使用root指令指定虚拟主机目录即网页存放目录
#比如访问http://ip/test.html将找到/usr/local/html/test.html
#比如访问http://ip/item/test.html将找到/usr/local/html/item/test.html
root /usr/local/Nginx/html;
#指定欢迎页面,按从左到右顺序查找
index index.html index.htm;
}
}
#配置虚拟主机192.168.1.4
server {
listen 80;
server_name 192.168.1.4;
location / {
root /usr/local/Nginx/html4;
index index.html index.htm;
}
}
}

配置基于端口的虚拟主机:

#配置虚拟主机
server {
listen 80;
server_name 192.168.1.3;
location / {
root /usr/local/Nginx/html80;
index index.html index.htm;
}
}
#配置虚拟主机
server {
listen 8080;
server_name 192.168.1.3;
location / {
root /usr/local/Nginx/html8080;
index index.html index.htm;
}
}

基于域名的虚拟主机配置:

#配置虚拟主机www.test.com
server {
#监听的ip和端口,配置本机IP和端口
listen 192.168.101.3:80;
#虚拟主机名称是www.test.com,请求域名www.test.com的url将由此server配置解析
server_name www.test.com;
location / {
root /usr/local/html;
index index.html index.htm;
}
}
#配置虚拟主机www.test1.com
server {
listen 192.168.1.3:80;
server_name www.test1.com;
location / {
root /usr/local/html;
index index.html index.htm;
}
}

Nginx反向代理:

#配置一个代理即tomcat1服务器
upstream tomcat_server1 {
server 192.168.1.3:8080;
}
#配置一个代理即tomcat2服务器
upstream tomcat_server2 {
server 192.168.1.4:8080;
}

#配置一个虚拟主机
server {
listen 80;
server_name www.test.com;
location / {
#域名www.test.com的请求全部转发到tomcat_server1即tomcat1服务上
proxy_pass http://tomcat_server1;
index index.jsp index.html index.htm;
}
}
server {
listen 80;
server_name www.test1.com;
location / {
#域名www.test1.com的请求全部转发到tomcat_server2即tomcat2服务上
proxy_pass http://tomcat_server2;
index index.jsp index.html index.htm;
}
}

Nginx反向代理:

upstream tomcat_server_pool{
server 192.168.1.3:8080 weight=2;
server 192.168.1.4:8080 weight=3;
}
server {
listen 80;
server_name aaa.test.com;
location / {
proxy_pass http://tomcat_server_pool;
index index.jsp index.html index.htm;
}
}

注意:

节点说明:
在http节点里添加:
#定义负载均衡设备的 Ip及设备状态
upstream myServer {
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用负载的Server节点下添加
proxy_pass http://myServer;
upstream 每个设备的状态:
down 表示单前的server暂时不参与负载
weight 认为1.weight越大,负载的权重就越大。
max_fails : 允许请求失败的次数认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
fail_timeout: max_fails 次失败后,暂停的时间。
backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

Nginx高可用:
解决高可用可以多建备份机

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐