【问题标题】:Websockets with Nginx and ssl handshake带有 Nginx 和 ssl 握手的 Websockets
【发布时间】:2015-02-20 18:32:48
【问题描述】:

我有一个用 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 用于 websocket,是真的吗?如果是的话,在这种情况下应该如何看待 nginx 的配置?

【问题讨论】:

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


    【解决方案1】:

    对于 websocket 支持,您需要在 @app 位置块中添加以下指令

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

    阅读更多here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 2015-02-03
      • 2015-12-21
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      相关资源
      最近更新 更多