简体   繁体   English

Websockets与Nginx和ssl握手

[英]Websockets with Nginx and ssl handshake

I have the application written in Rails and Ember frontend for it. 我有用Rails和Ember前端编写的应用程序。 It is accessible by nginx server. Nginx服务器可以访问它。 Here is configuration for Rails part: 这是Rails部分的配置:

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). 现在,该应用程序已增长,并具有websockets服务器(使用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. 我读过,我需要为此握手启用SSL。 How can I do this in nginx? 如何在Nginx中做到这一点? I also read, that I don't need to use https and I can use SSL only for websockets, is it true? 我还读到,我不需要使用https,而只能将SSL用于websocket,是真的吗? And if yes, how should look configuration for nginx in this case? 如果是的话,在这种情况下应该如何看待nginx的配置?

For websocket support you need add the following directives in your @app location block 为了获得websocket支持,您需要在@app位置块中添加以下指令

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

Read more here 在这里阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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