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高可用:

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容