【问题标题】:Handle subdomain on nginx running on Docker based ElasticBeanstalk在基于 Docker 的 ElasticBeanstalk 上运行的 nginx 上处理子域
【发布时间】:2019-10-16 10:48:15
【问题描述】:

我有一个由前端和后端 Docker 容器以及另一个用于 nginx 反向代理的容器组成的场景。

目前,nginx配置将urlhttp://example.com指向前端容器,http://example.com/backend指向后端容器。

现在我需要配置 nginx,以便它也可以理解对 http://backend.example.com 的调用并将它们重定向到后端容器。

如果在 nginx 配置中使用的服务器名称是 localhost,我该如何实现?

server {
    listen       80 default_server;
    server_name  localhost;

    if ($http_x_forwarded_proto = 'http') { 
        return 301 https://$host$request_uri; 
    }

    location /backend/ {
        proxy_pass   http://backend:3000/;
    }

    location / {
        proxy_pass   http://frontend:3000/;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

我也尝试添加这个块,但它不起作用(实际上它使一切停止工作):

server {
    listen       80 default_server;
    server_name  backend.localhost;

    if ($http_x_forwarded_proto = 'http') { 
        return 301 https://$host$request_uri; 
    }

    location / {
        proxy_pass   http://backend:3000/;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

【问题讨论】:

  • 试试 server_name backend.example.com
  • 也没有用。

标签: nginx amazon-elastic-beanstalk


【解决方案1】:

我创建了测试环境。将 xyz.com 和 backend.xyz.com 添加到我的“主机”文件中。 这对我有用

server {
    listen       80;
    server_name  xyz.com;

    location /backend/ {
        proxy_pass   http://backend:8081/;
    }

    location / {
        proxy_pass   http://frontend:8082/;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

server {
    listen       80;
    server_name  backend.xyz.com;

    location / {
        proxy_pass   http://backend:8081/;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

【讨论】:

    猜你喜欢
    • 2019-11-25
    • 2016-02-08
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    相关资源
    最近更新 更多