拿nginx做负载均衡

拿nginx做负载均衡

情况是这样的现在有两台机子有一模一样的服务,需要用nginx做负载均衡。

平时项目里面某个请求的地址是这样的

http://127.0.0.1:13014/FaceRetrieveService/SearchFace

所以我们可以用nginx这样做负载均衡

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

worker_rlimit_nofile 65535;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 20480;
}

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

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    client_max_body_size 0;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    upstream go-faceURL {
        server 127.0.0.1:13014;
        server xxx.xxx.xxx.xxx:13014;
        ip_hash;
    }

    server {
    gzip on;
    gzip_buffers 32 4K;
    gzip_comp_level 6;
    	gzip_min_length 100;
    gzip_types application/javascript text/css text/xml;
    	gzip_vary on;
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;


    #人脸识别接口
    location /faceURL {
            proxy_set_header Host $host; #notice:very important(注�~D~O)
            proxy_set_header X-Real-IP $remote_addr; #notice:very important(注�~D~O)
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #notice:very important(注�~D~O)
            proxy_pass http://go-faceURL/FaceRetrieveService/SearchFace;
        }
        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

这样子项目里面请求地址就可以变成这样

http://172.18.132.42/faceURL

nginx会识别go-XXX的文字然后把前面的地址拿一个来替换这个go-XXX的位置

猜你喜欢

转载自blog.csdn.net/qq_43535595/article/details/114365292