简体   繁体   中英

Websockets with Nginx and ssl handshake

I have the application written in Rails and Ember frontend for it. It is accessible by nginx server. Here is configuration for Rails part:

upstream app_project_app {
  server unix:///tmp/project.sock fail_timeout=0;
}

And here is configuration for ember part:

server {
  listen 80;
  server_name project.demo.domain.pl;
  root /home/lunar/apps/project-ember/current;
  try_files /system/maintenance.html $uri/index.html $uri.html $uri @app;
  access_log /var/log/nginx/project_app_access.log;
  error_log /var/log/nginx/project_app_error.log;

  keepalive_timeout 5;
  proxy_read_timeout 60;
  proxy_send_timeout 60;
  proxy_connect_timeout 60;

  if ($request_method !~ ^(GET|HEAD|PUT|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  location ~ ^/assets/ {
    expires max;
    add_header Cache-Control public;

    add_header ETag "";
    break;
  }

  location = /favicon.ico {
    expires    max;
    add_header Cache-Control public;
  }


  location / {
    try_files $uri/index.html $uri.html $uri @app;
    error_page 404              /404.html;
    error_page 422              /422.html;
    error_page 500 502 503 504  /500.html;
    error_page 403              /403.html;
  }

  location @app {
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://app_project_app;
  }
}

Now the application grown and has websockets server (using faye). And the client can't connect to the server:

WebSocket connection to 'ws://project.demo.domain.pl/faye' failed: Error during WebSocket handshake: Unexpected response code: 400

I've read, that I need to enable SSL for this handshake. How can I do this in nginx? I also read, that I don't need to use https and I can use SSL only for websockets, is it true? And if yes, how should look configuration for nginx in this case?

For websocket support you need add the following directives in your @app location block

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade”;

Read more here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM