Swarm on AWS hangs on "Installing Rancher agent"

Hi,

rancher/server started with docker run -d --restart=unless-stopped -p 8080:8080 rancher/server on Amazon Linux. This EC2 has rancher-machine security group and another one which accepts TCP 8080 from 0.0.0.0.0/0.

Created new environment, added two EC2 hosts (Ubuntu 16.04 ami-e13739f6), both with rancher-machine security group.

In Infrastructure>Hosts both are hanging on Installing Rancher agent, but if I SSH into them, docker ps shows that agent is running. I can ping IP of EC2 which runs rancher/server, and I can telnet to 8080 (although connection is closed the next second):

Escape character is '^]'.
Connection closed by foreign host.

What am I missing here?

Thanks

docker logs on the running agent container?

A lot of lines and then:
time="2017-01-27T16:36:54Z" level=fatal msg="Exiting. Error: websocket: bad handshake"

Might be useful, NGINX proxy config for Rancher:

listen 80;
server_name rancher.x.y; <--- is this the issue? Does each host uses domain name of server or IP?
location / {
            proxy_set_header Host $host:$server_port;
            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 $scheme;
            proxy_pass       http://127.0.0.1:8080;
        }

Fixed by editing NGINX config to:

location / {
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:8080;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }