R
R
Reddaemon2015-08-22 13:14:18
Nginx
Reddaemon, 2015-08-22 13:14:18

Websocket behind Apache and Nginx, headers are missing, how to fix?

Greetings! There is such a problem. Apache listens on the external ip and proxies all /test requests to nginx (on the internal ip), which proxies from the /city-dashboard location to the websocket application. In Apache config:

ProxyRequests On
  ProxyPreserveHost On
     <Location /test>
              ProxyPass "https://127.0.0.1:443"
              ProxyPassReverse "https://127.0.0.1:443"
              Allow from All
              Header set Connection "upgrade"
              Header set Upgrade "websocket"
           </Location>

nginx config: on nginx.conf:
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
 } 

   map $http_sec_websocket_key $upgr {
    ""      "";           # If the Sec-Websocket-Key header is empty, send no upgrade header
    default "websocket";  # If the header is present, set Upgrade to "websocket"
   }

   map $http_sec_websocket_key $conn {
    ""      $http_connection;  # If no Sec-Websocket-Key header exists, set $conn to the incoming Connection header
    default "upgrade";         # Otherwise, set $conn to upgrade
  }
include /etc/nginx/conf.d/*.conf; on default.conf

default.conf:
location /city-dashboard {
     rewrite ^/city-dashboard\/(.*)$ /$1 break;
     proxy_pass http://127.0.0.1:5001;
     proxy_set_header Host $host;
     proxy_http_version 1.1;
     #proxy_set_header upgrade $upgr;
     proxy_set_header Upgrade $upgr;
     #proxy_set_header Connection $conn;
     proxy_set_header Connection "Upgrade\n\r";
     proxy_set_header Sec-WebSocket-Key $http_sec_websocket_key;
     #add_header Upgrade websocket;
     #add_header Connection Upgrade;
     proxy_read_timeout 100;
     proxy_redirect off;
     proxy_set_header        X-Forwarded-Proto $scheme;
     add_header              Front-End-Https   on;
   }

request headers:
GET wss://example.com:4443/test/city-dashboard/stream HTTP/1.1
Host: example.com:4443
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: https://example.com:4443
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Sec-WebSocket-Key: RlhNyYNipJ1RUOU7nl4xMA==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

response headers:
HTTP/1.1 101 Switching Protocols
Server: nginx/1.8.0
Date: Sat, 22 Aug 2015 10:05:02 GMT
Sec-WebSocket-Accept: e7UKY/y5mIi8RTmhLQgFJ566dfo=

Upgrade and Connection are missing in the response headers, if you follow the instructions from the nginx website, I get an error 400 and websockets still do not work. It will not work to remove Apache from this chain, because it is located in another organization. Can anyone come across this and offer some workarounds? or just some solutions to make it work.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question