带有Nginx和ssl握手的Websockets

时间:2015-02-20 18:32:49

标签: ruby-on-rails ssl nginx websocket faye

我有用Rails和Ember前端编写的应用程序。它可以被nginx服务器访问。这是Rails部分的配置:

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

这是ember部分的配置:

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;
  }
}

现在应用程序增长并具有websockets服务器(使用faye)。客户端无法连接到服务器:

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

我已经读过,我需要为此握手启用SSL。我怎么能在nginx中这样做?我还读过,我不需要使用https,我只能将SSL用于websockets,这是真的吗?如果是的话,在这种情况下应该如何看待nginx的配置?

1 个答案:

答案 0 :(得分:2)

对于websocket支持,您需要在@app位置块

中添加以下指令
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade”;

了解更多here