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

現在、2 つの URL があります。

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

この 2 つの 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';
        }

    }

}

関連情報