Yadu Krishnan
Yadu Krishnan

Reputation: 3522

Issues with Websocket Upgrade header in Nginx while doing proxy

I have 3 scala and akka-http applications which are binding to localhost with different ports in an ubuntu machine. I want to access all the applications with the same port number. So I used nginx to proxy the request and redirect to the required port number internally. Everything was working fine as expected. Now, in each of the applications, I have in-build websocket built using akka-http. All the websocket request will be having the url .../ws/.. Eg:

App-1(HR) 
  Url => http://192.168.1.50:90/hr/ ....  nginx resolve to localhost:8181
  web socket url => http://192.168.1.50:90/hr/ws/...

App-2(Common) 
  Url => http://192.168.1.50:90/common/... nginx resolve to localhost:8182
  web socket url => http://192.168.1.50:90/common/ws/... 

App-3(accounts) 
  Url => http://192.168.1.50/accounts/.. nginx resolve to localhost:8183
  web socket url => http://192.168.1.50:90/accounts/ws/...

Websocket was working fine in my machine, but when I deployed to ubuntu server, it is giving errors in websocket. After checking the log, I found out the reason that, when the nginx proxy is done, it will not be carrying over the Upgrade header. So I made the following change in the nginx configuration file for the location element.

location /common {
                location /common/global {
                        proxy_pass http://127.0.0.1:8182/common/ws;
                        proxy_http_version 1.1;
                        proxy_set_header Upgrade $http_upgrade;
                        proxy_set_header Connection "upgrade";
                }
                location /common {
                        proxy_pass http://127.0.0.1:8182/common;
                }
}

Now the websocket is working fine. However, I need to add this to the two other location elements also. I am not sure if this is the correct approach for doing this. Can anyone please guide me with this ?

Upvotes: 0

Views: 2360

Answers (1)

Ryan Kopf
Ryan Kopf

Reputation: 544

Look at the inspector in Chrome at your headers. It sends a capital Upgrade not upgrade. I'm not sure if that's your only problem, but mine wouldn't work until this was fixed.

proxy_set_header Connection "upgrade";

should be

proxy_set_header Connection "Upgrade";

Upvotes: 2

Related Questions