如何配置 Nginx 代理以传递到多个 Sinatra 端口?

如何配置 Nginx 代理以传递到多个 Sinatra 端口?

我有一个 Sinatra 应用。我启动了两个 Thin 服务器,监听 4567 和 4568。但是,我已将 Nginx 配置为仅转发到 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;
}

我怎样才能转发到 4678?我可以复制位置块,只更改,proxy_pass但这不会覆盖第一个位置块吗?正确的方法是什么?

答案1

这就是所谓的负载均衡请参阅 nginx 文档http://nginx.org/en/docs/http/load_balancing.html

这是一个例子。

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

server {
    listen 80;

    location / {
        proxy_pass http://myapp1;
    }
}
}

相关内容