Nginx四层负载+反向代理

Nginx负载均衡

四层负载配置示例

cat nginx.conf
http {
    ...
}
stream {
    server_traffic_status_zone;
    log_format proxy '$remote_addr - [$time_local] $status "$upstream_bytes_sent" "$upstream_bytes_received" - "$upstream_addr" "$upstream_connect_time"';

    access_log  logs/upstream-access.log  proxy;
    open_log_file_cache off;

    upstream credit-gateway {
       #hash $remote_addr consistent;
                 server 3.1.19.51:11200;
                 server 3.1.19.52:11200;
           }

    server {
       listen 11200;
       proxy_pass credit-gateway;
       proxy_timeout    300;
       proxy_connect_timeout   60;
       access_log  logs/upstream-gateway.log  proxy;
    }
}

反向代理

完整配置示例

user  root;
worker_processes  auto;

# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;

error_log  logs/error.log warn;
pid        logs/nginx.pid;

events {
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    lua_package_path "/usr/local/openresty/lualib/skywalking-nginx-lua/?.lua;;";

    lua_shared_dict tracing_buffer 100M;

    init_worker_by_lua_block {
        local metadata_buffer = ngx.shared.tracing_buffer
        metadata_buffer:set('serviceName', 'Front')
        metadata_buffer:set('serviceInstanceName', 'front-risk')
        metadata_buffer:set('includeHostInEntrySpan', false)
        require("skywalking.util").set_randomseed()
        require("skywalking.client"):startBackendTimer("http://3.1.19.157:12800")
        skywalking_tracer = require("skywalking.tracer")
    }


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format json escape=json
      '{"@timestamp":"$time_iso8601",'
      '"host":"$hostname",'
      '"server_ip":"$server_addr",'
      '"client_ip":"$remote_addr",'
      '"xff":"$http_x_forwarded_for",'
      '"domain":"$host",'
      '"url":"$uri",'
      '"referer":"$http_referer",'
      '"args":"$args",'
      '"upstreamtime":"$upstream_response_time",'
      '"responsetime":"$request_time",'
      '"request_method":"$request_method",'
      '"status":"$status",'
      '"size":"$body_bytes_sent",'
#      '"request_body":"$request_body",'
      '"request_length":"$request_length",'
      '"protocol":"$server_protocol",'
      '"upstreamhost":"$upstream_addr",'
      '"upstreamtime":"$upstream_response_time",'
      '"upstreamstatus":"$upstream_status",'
      '"file_dir":"$request_filename",'
      '"http_user_agent":"$http_user_agent"'
    '}';


    access_log  logs/access.log  json;
    sendfile        on;

    #tcp_nopush     on;
    keepalive_timeout  120;

    #gzip  on;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 8 128k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    client_header_buffer_size 128k;
    client_body_buffer_size 1m;
    underscores_in_headers on;

    # prometheus metircs: /status/format/prometheus
    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;
    stream_server_traffic_status_zone;

    server {
        listen       8002;
        server_name localhost;
        location /vts_metrics {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
        location /sts_metrics {
            stream_server_traffic_status_display;
            stream_server_traffic_status_display_format html;
        }
    }

    server {
        listen       8902;
        listen  [::]:8902;
        server_name  localhost;
        root   /data/htdocs/front-bank-risk;
        index  index.html index.htm;

        location /login/ {
            proxy_pass http://3.1.19.157:11200/dics/uat/login/;
            proxy_redirect off;
            proxy_set_header HOST $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Connection "Keep-Alive";
            proxy_connect_timeout   60;
            proxy_send_timeout      120;
            proxy_read_timeout      120;
        }
        access_log  logs/dics-risk-access.log json;
    }
}

关于方向代理proxy_pass

  • 配置 proxy_pass 时,当在后面的 url 加上了 /,相当于是绝对路径,则 Nginx 不会把 location 中匹配的路径部分加入代理 uri
  • 如果配置 proxy_pass 时,后面没有 /,Nginx 则会把匹配的路径部分加入代理 uri。

应用场景示例:

server {
        listen       8081;
        server_name  localhost;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
#情景1:proxy_pass后有/ ,表绝对路径,不把匹配部分加入最终代理路径(location 和proxy_pass结尾一致)
        #访问地址:http://localhost:8081/WCP.Service/wcp/modeladapter/download/asc.shtml
        #最终代理:http://10.194.171.7:13082/modeladapter/download/asc.shtml
		location /WCP.Service/wcp/modeladapter/download/ {
            proxy_pass   http://10.194.171.7:13082/modeladapter/download/;
        }
        #访问地址:http://localhost:8081/model/asc.shtml
        #最终代理:http://127.0.0.1:8082/model/asc.shtml
		location /model/ {
            proxy_pass   http://127.0.0.1:8082/model/;
        }
#情景2:proxy_pass后有/ ,表绝对路径,不把匹配部分加入最终代理路径(location 和proxy_pass结尾不一致)
        #访问地址:http://localhost:8081/model/asc.shtml
        #最终代理:http://127.0.0.1:8082/asc.shtml
		location /model/ {
            proxy_pass   http://127.0.0.1:8082/;
        }
#情景3:proxy_pass后没有 / ,Nginx会把匹配部分带到代理的url
        #访问地址:http://localhost:8081/model/asc.shtml
        #最终代理:http://127.0.0.1:8082/model/asc.shtml
		location /model/ {
            proxy_pass   http://127.0.0.1:8082;
        }
 
#情景4
        #访问地址:http://localhost:8081/model/asc.shtml
        #最终代理:http://127.0.0.1:8082/AAAmodel/asc.shtml
		location /model/ {
            proxy_pass   http://127.0.0.1:8082/AAA;
        }
#情景5
        #访问地址:http://localhost:8081/model/asc.shtml
        #最终代理:http://127.0.0.1:8082/asc.shtml
		location /model {
            proxy_pass   http://127.0.0.1:8082/;
        }
#情景6
        #访问地址:http://localhost:8081/modelBBB/asc.shtml
        #最终代理:http://127.0.0.1:8082/asc.shtml
		location /model {
            proxy_pass   http://127.0.0.1:8082/;
        }
 
 
		location /opus-front-sso {
            proxy_pass   http://10.194.170.94/opus-front-sso;
        }
		location /awater {
            proxy_pass   http://10.194.170.94/awater;
        }
 
        
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值