具有不同路径的单个域路由到不同的 nginx 服务器

时间:2021-06-26 05:19:55

标签: amazon-web-services nginx amazon-ec2 amazon-route53

用例: 我有不同的 aws 服务器,如 server1、server2、server3 和 AWS ALB 名为 example.com 的单个域

example.com/s1 should route to server1
example.com/s2 should route to server2
example.com/s3 should route to server3

我的配置文件在 /etc/nginx/sites-available for server1,2,3

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    include snippets/self-signed.conf;

    server_name example.com albdns.com;

    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;
    location / {
        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;
        try_files $uri $uri/ =404;
    }
    location /login {
        proxy_pass https://publicipofserver1/;
    }
    location /s1 {
        return 301 https://publicipofserver1/;
    }
}
server {
        listen 80;
        listen [::]:80;

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

        server_name example.com albdns.com;

        #location / {
        #       try_files $uri $uri/ =404;
        #}
        return 301 https://$server_name$request_uri;

}

问题 1:

有了这个,当我使用域名时它可以重定向说

example.com/s1 -> publicipofs1/login

但不适用于 s2 和 s3 ..它说

example.com 将您重定向的次数过多。

这是正确的配置方式吗?

问题 2:

无论如何不要在重定向时暴露 publicip? 当我在浏览器中使用 albdns/s1 时,它会在 url 选项卡中更改为 s1ipaddress 像 albnds/s1 -> x.x.x.x/s1

0 个答案:

没有答案