ecs-gen icon indicating copy to clipboard operation
ecs-gen copied to clipboard

Using multiple domain names causes an error

Open myoung34 opened this issue 6 years ago • 7 comments

docker inspect on my app container shows:

      "Env": [
        "VIRTUAL_HOST=kegduino.org www.kegduino.org",

My task definition:

      "environment": [
        {
          "name": "VIRTUAL_HOST",
          "value": "kegduino.org www.kegduino.org"
        }
      ],

However the reverse proxy errors with:

2018/07/10 18:25:49 found cluster region to be: us-east-1
2018/07/10 18:25:49 found cluster name to be: App 
2018/07/10 18:25:49 updating config
2018/07/10 18:25:49 [reverse-proxy] virtual_host environment variable not found. skipping
2018/07/10 18:25:49 running signal command
2018/07/10 18:25:50 ===== output start =====
2018/07/10 18:25:50 2018/07/10 18:25:49 [emerg] 14#14: invalid number of arguments in "upstream" directive in /etc/nginx/conf.d/default.conf:43
nginx: [emerg] invalid number of arguments in "upstream" directive in /etc/nginx/conf.d/default.conf:43
2018/07/10 18:25:50 ===== output end =====
2018/07/10 18:25:50 failed to run signal command
2018/07/10 18:25:50 error:  exit status 1
2018/07/10 18:25:50 []

myoung34 avatar Jul 10 '18 18:07 myoung34

Having a quick look at the code I couldn't find any obvious problem. Could you post the contents of /etc/nginx/conf.d/default.conf?

codesuki avatar Jul 11 '18 00:07 codesuki

Working fine, then added back VIRTUAL_HOST=domain.com www.domain.com

I captured the config by forking the ecs-nginx-proxy Dockerfile with this diff:

-CMD nginx && ecs-gen --signal="nginx -s reload" --template=nginx.tmpl --output=/etc/nginx/conf.d/default.conf
+CMD nginx && ecs-gen --signal="nginx -s reload" --template=nginx.tmpl --output=/etc/nginx/conf.d/default.conf || cat /etc/nginx/conf.d/default.conf

Error:

2018/07/11 01:38:44 2018/07/11 01:38:44 [emerg] 18#18: invalid number of arguments in "upstream" directive in /etc/nginx/conf.d/default.conf:43
nginx: [emerg] invalid number of arguments in "upstream" directive in /etc/nginx/conf.d/default.conf:43
2018/07/11 01:38:44 ===== output end =====
2018/07/11 01:38:44 failed to run signal command
2018/07/11 01:38:44 error:  exit status 1
2018/07/11 01:38:44 []

Generated Config:

# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  default $http_x_forwarded_proto;
  ''      $scheme;
}
# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any 
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
  default upgrade;
  '' close;
}
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
                 '"$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent"';
access_log off;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
# Mitigate httpoxy attack (see https://github.com/jwilder/nginx-proxy for details)
proxy_set_header Proxy ""; 
server {
        server_name _; # This is just an invalid value which will never trigger on a real hostname.
        listen 80; 
        access_log /var/log/nginx/access.log vhost;
        location = / { 
                return 200 'nginx is alive';
                add_header Content-Type text/plain;
        }   
        location / { 
                return 503;
        }   
}
upstream kegduino.org www.kegduino.org {

         server 10.0.0.153:32774;

}
server {
        server_name kegduino.org www.kegduino.org;
        listen 80; 
        access_log /var/log/nginx/access.log vhost;
        location / {
                proxy_pass http://kegduino.org www.kegduino.org;
        }
}

myoung34 avatar Jul 11 '18 01:07 myoung34

After some digging, its not supposed to be whitespace separated (what your docs say), but comma separated. Doing this with a comma no longer errors, but does not foward as expected (all versions of the domain name result in a page with nginx is alive. I think (unconfirmed), the problem is:

upstream kegduino.org,www.kegduino.org {
    
         server 10.0.0.153:32866;
    
}
server {
        server_name kegduino.org,www.kegduino.org;

        listen 80;
        access_log /var/log/nginx/access.log vhost;
        location / {
                proxy_pass http://kegduino.org,www.kegduino.org;
        }
}

Im pretty sure that this is valid:

upstream kegduino.org,www.kegduino.org {
    
         server 10.0.0.153:32866;
    
}

But this is not:

server {
        server_name kegduino.org,www.kegduino.org;

        listen 80;
        access_log /var/log/nginx/access.log vhost;
        location / {
                proxy_pass http://kegduino.org,www.kegduino.org;
        }
}

server_name should not be comma separated like the upstream, but should be multiple server blocks based on a split by comma

I think the final result should be:

upstream kegduino.org,www.kegduino.org {
    
         server 10.0.0.153:32866;
    
}
server {
        server_name www.kegduino.org;

        listen 80;
        access_log /var/log/nginx/access.log vhost;
        location / {
                proxy_pass http://www.kegduino.org;
        }
}
server {
        server_name kegduino.org;

        listen 80;
        access_log /var/log/nginx/access.log vhost;
        location / {
                proxy_pass http://kegduino.org;
        }
}

myoung34 avatar Jul 11 '18 02:07 myoung34

What should probably happen here is that the upstream/proxy_pass values get sluggified and the value gets used for server_name as is. That will remove the need for two server blocks.

josegonzalez avatar Jul 11 '18 03:07 josegonzalez

I will check this PR again https://github.com/codesuki/ecs-gen/pull/18 Sadly I don't have access to AWS anymore so I can't really test it much. I agree upstream / proxy_pass should be one name. (Described in the PR)

codesuki avatar Jul 11 '18 03:07 codesuki

One thing you could do is inject methods into the golang template library. Thats what we do with gliderlabs/sigil.

josegonzalez avatar Jul 11 '18 03:07 josegonzalez

Sorry, turns out dockerhub only had old versions. I improved the process now. Try the 0.5.0 image here https://hub.docker.com/r/codesuki/ecs-gen/tags/

codesuki avatar Jul 17 '18 12:07 codesuki