로드 밸런싱을 사용하여 nginx가 다른 URL로 요청을 전달합니다.

로드 밸런싱을 사용하여 nginx가 다른 URL로 요청을 전달합니다.

나는 nginx를 처음 사용하고 있으며 요청 전달 목적으로 사용하고 있습니다. 들어오는 각 요청은 아래의 내 구성입니다/URL로 전달됩니다.

    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile on;  
        keepalive_timeout  65;
        
        server {
            listen       8008 ssl;
            listen       localhost:8008 ssl;
            listen       xyz.exmaple.com:8008 ssl;
            server_name  xyz.exmaple.com;
            more_set_headers 'Server: ABC';
            
            ssl_protocols TLSv1.2;
            ssl_prefer_server_ciphers on;
            ssl_ciphers "EECDH+AESGCM,EDH+AESGCM";      
            ssl_certificate /logs/ssl/SSL.crt;      
            ssl_certificate_key /logs/ssl/key.key;

location /app/ {
        
            
            proxy_pass      https://proxy1.example.com:8888/abc/efg;            
            proxy_read_timeout 60s;

            # May not need or want to set Host. Should default to the above hostname.
            proxy_pass_header Server;
            proxy_set_header          Host            $host;
            proxy_set_header          X-Real-IP       $remote_addr;
            proxy_set_header          X-Forwarded-For $proxy_add_x_forwarded_for;
            add_header  X-Frame-Options "SAMEORIGIN" always;
            more_set_headers 'Server: ABC';
            
        }
        
        }

}  

모든 요청https://xyz.exmaple.com:8008/app/으로 전달되고 있습니다https://proxy1.example.com:8888/abc/efg

이제 두 개의 URL이 있습니다.

1) https://proxy1.example.com:8888/abc/efg
2) https://proxy2.example.com:8888/abc/efg

이 두 URL에 대해 라운드 로빈 로드 밸런싱을 원합니다. 어떻게 이를 달성할 수 있습니까?

답변1

이 문제를 해결하는 데 사용하십시오 upstream. 예:

http
{
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    upstream backend
    {
        server proxy1.example.com:8888;
        server proxy2.example.com:8888;
    }

    server
    {
        listen 8008 ssl;
        listen localhost:8008 ssl;
        listen xyz.exmaple.com:8008 ssl;
        server_name xyz.exmaple.com;
        more_set_headers 'Server: ABC';

        ssl_protocols TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "EECDH+AESGCM,EDH+AESGCM";
        ssl_certificate /logs/ssl/SSL.crt;
        ssl_certificate_key /logs/ssl/key.key;

        location /app/
        {
            proxy_pass https://backend/abc/efg;
            proxy_read_timeout 60s;

            # May not need or want to set Host. Should default to the above hostname.
            proxy_pass_header Server;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            add_header X-Frame-Options "SAMEORIGIN" always;
            more_set_headers 'Server: ABC';
        }

    }

}

관련 정보