0

I have a Sinatra app. I start two Thin servers listening on: 4567 and 4568. However, I have configured Nginx to only forward to 4567.

server {
        listen 443;
        listen [::]:443;

        root /var/www/example/public;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        ssl                        on;
        ssl_certificate        /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key  /etc/letsencrypt/live/example.com/privkey.pem;

        ssl_session_timeout 30m;

        location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_pass http://localhost:4567;

                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_pass_header Server;
                proxy_cache_bypass $http_upgrade;
                proxy_redirect off;

        }
}

server {
    listen      80;
    server_name example.com www.example.com;
    rewrite     ^   https://$server_name$request_uri? permanent;
}

How can I also forward to 4678? I could duplicate the location block, changing only proxy_pass but wouldn't that override the first location block? What is the correct way?

user3574603
  • 143
  • 3

1 Answers1

2

This is called load balancing and is covered in the nginx documentation at http://nginx.org/en/docs/http/load_balancing.html

Here is an example.

http {
upstream myapp1 {
    server srv1.example.com;
    server srv2.example.com;
    server srv3.example.com;
}

server {
    listen 80;

    location / {
        proxy_pass http://myapp1;
    }
}
}
Drifter104
  • 3,773
  • 2
  • 25
  • 39
vautee
  • 495
  • 3
  • 11