Nginx – TeamCity, nginx, and Websockets – 501 Error

nginxPROXYteamcitywebsocket

I'm currently setting up TeamCity behind an nginx reverse proxy, but I am getting an error in my browser. The error is as follows:

WebSocket connection to 'ws://ci.example.net/app/subscriptions?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=2.2.7-javascript&X-Atmosphere-Transport=websocket&X-Atmosphere-TrackMessageSize=true&X-atmo-protocol=true&browserLocationHost=http%3A%2F%2Fci.example.net' failed: Error during WebSocket handshake: Unexpected response code: 501

I've looked in both the nginx error logs and the TeamCity error logs, both of which turn up empty.

My nginx config is as follows:

server {
  listen 80;
  server_name ci.example.net;

  error_log /var/log/nginx/error.log;
  proxy_intercept_errors on;

  error_page 401 403 404 /404.html;

  location / {
    proxy_set_header        Host $host;
    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:8111;
    proxy_read_timeout  90;

    proxy_redirect      http://127.0.0.1:8111 http://ci.fluxmc.net;
  }
}

#Websocket configuration
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''   '';
}

server {
    listen 400;
    server_name ci.example.net;

    error_log /var/log/nginx/error.log;
    proxy_intercept_errors on;

    location /tc {
        proxy_pass http://localhost:8111/tc;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $server_name:$server_port;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

I am really not sure where to go from here, I've followed the documentation for TeamCity almost exactly. If you could provide any help, I would appreciate it!

Best Answer

The documentation for TeamCity makes a couple of assumptions that don't hold up for you:

TeamCity server is installed at URL: http://teamcity.local:8111/tc

It is visible to the outside world as URL: http://teamcity.public:400/tc

In your case TeamCity is visible to the outside world as URL: http://ci.example.net.

This changes your NGINX configuration as follows:

server {
  listen 80;
  server_name ci.example.net;

  error_log /var/log/nginx/error.log;
  proxy_intercept_errors on;

  error_page 401 403 404 /404.html;

  location / {
    proxy_set_header        Host $host;
    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:8111;
    proxy_read_timeout  90;

    proxy_redirect      http://127.0.0.1:8111 http://ci.fluxmc.net;
    proxy_http_version  1.1;
    proxy_set_header    Upgrade $http_upgrade;
    proxy_set_header    Connection $connection_upgrade;
  }
}

#Websocket configuration
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''   '';
}

For me, that was enough to get the websocket connection with TeamCity working.

Related Topic