0

I am new to nginx, I am using it for request forwarding purpose, each incoming request is getting forwarded to another server/url below is my configuration :

    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';
            
        }
        
        }

}  

Every request on https://xyz.exmaple.com:8008/app/ is getting forwarded to https://proxy1.example.com:8888/abc/efg

Now I have two urls :

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

I want round robin load balancing for this two URL, How can i achieve that ?

1 Answer 1

1

Use upstream to solve this issue. Example:

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';
        }

    }

}
1
  • I tried with this, now problem i face is when i am directly forwarding request to proxy1 or proxy2 domain, i got response within second but with above configuration with upstream, i am getting response at 2 minutes for single request
    – utsav
    Nov 19, 2022 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .