Nginx负载均衡配置

nging.conf的配置如下

user  www www;
worker_processes  2;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  1024;
}
http {
        include mime.types;
        default_type    application/octet-stream;
        sendfile    on;
        client_header_timeout 10;
        client_body_timeout 10;
        reset_timedout_connection on;
        send_timeout 10;
        tcp_nodelay on;
        tcp_nopush  on;
        keepalive_timeout  20;
        ssi on;
        ssi_silent_errors on;
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types       text/plain application/javascript application/html pplication/x-javascript text/css application/xml;
        gzip_vary on;
        upstream api {   
            server   192.168.1.55:18088 weight=5;
            server   192.168.1.58:18088;
        }   
        include vhosts-api.conf;
}

vhosts-api.conf配置如下

server
  {
    listen      18080;
    server_name api.abc.cn;
    location / {
        proxy_pass http://api;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size    20m;
        proxy_connect_timeout   300;
        proxy_send_timeout      300;
        proxy_read_timeout      300;
        client_body_buffer_size 128k;
        access_log  /usr/local/nginx/logs/access.log;
        error_log   /usr/local/nginx/logs/error.log;
    }
}

说明:在nging.conf中配置一个服务api,转发的目标是如下配置的192.168.1.55、192.168.1.58,同时配置了192.168.1.55的权重是5,这样一般会是转发给192.168.1.555个请求,转发给192.168.1.581个请求。通过include vhosts-api.conf加载api这个转发的具体配置,可以看到vhosts-api.conf监听的是18080端口,名称是api.abc.cn,转发的是/即所有的请求,如果是/abc,这样只有api.abc.com/abc的请求才会被转发

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值