我有以下代码:

resource "docker_image" "nginx" {
  name = "nginx:1.11-alpine"
}
resource "docker_container" "nginx-server" {
  name = "nginx-server"
  image = "${docker_image.nginx.latest}"
  ports {
    internal = 80
    external = 80
  }
  volumes {
    container_path  = "/usr/share/nginx/html"
    host_path = "/home/scrapbook/tutorial/www"
    read_only = true
  }
}

在terraform文件(.tf)中...我想配置Web程序的两个实例,这些实例在同一服务器上的两个不同端口中提供。
resource "docker_container" "Odoo-iku_1" {
  name  = "Odoo-iku 1"
  image = "ikusolutions/odoo-iku:latest"
  links = ["db"]
  ports {
    external = 8070
    internal = 8069
  }
}


resource "docker_container" "Odoo-iku_2" {
  name  = "Odoo-iku 2"
  image = "ikusolutions/odoo-iku:latest"
  links = ["db"]
  ports {
    external = 8071
    internal = 8069
  }
}

如何使Nginx将外部端口8070和8071地址为域名? 示例:端口8070定向至https://iku.solutions,端口8071定向至https://demo.iku.solutions

最佳答案

Nginx根据您配置服务器的方式路由到达服务器的流量。
在您的情况下,您想将nginx配置为反向代理,以根据请求尝试到达的服务器名称路由请求。假设对Odoo-iku 1 Odoo-iku 2的请求分别来自Odoo-iku1.example.com和Odoo-iku2.example.com,则此配置应该可以为您服务-

server {
  listen      8001 default_server;
  server_name Odoo-iku1.example.com;
  location / {
    proxy_pass       http://127.0.0.1:8071;
  }
}

server {
  listen      8002 default_server;
  server_name Odoo-iku2.example.com;
  location / {
    proxy_pass       http://127.0.0.1:8070;
  }
}

upstream main {
  server 127.0.0.1:8001;
  server 127.0.0.1:8002;
}

server {
  listen      80;
  server_name example.com;
  location / {
    proxy_pass http://main;
  }
}

07-25 23:42