在lxc
内部容器中,我正在运行faye
个应用
Gemfile
:
source 'https://rubygems.org'
gem 'faye'
gem 'thin'
config.ru
:
require 'faye'
Faye::WebSocket.load_adapter('thin')
bayeux = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
run bayeux
然后
$ thin start
/etc/nginx/sites-available/domain.com
:
server {
server_name domain.com;
location /faye {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
在主持人身上:
/etc/nginx/sites-available/domain.com
:
server {
server_name domain.com;
location / {
proxy_pass http://10.0.0.109:80;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}
}
然后,我尝试从here(ws://domain.com/faye
)连接到它,但它失败了。我做错了什么?
该应用说:
Using rack adapter
Thin web server (v1.6.4 codename Gob Bluth)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
访客nginx
访问日志:
10.0.0.1 - - [09/Dec/2015:11:03:21 +0200] "GET /faye?encoding=text HTTP/1.0" 400 11 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
主机nginx
访问日志:
11.111.111.111 - - [09/Dec/2015:11:03:21 +0200] "GET /faye?encoding=text HTTP/1.1" 400 21 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
chrome
的开发者工具控制台:
与'ws://domain.com/faye?encoding = text'的WebSocket连接失败:WebSocket握手期间出错:意外响应代码:400
我试图在客人身上运行Myst建议的应用程序。
Gemfile
:
source 'https://rubygems.org'
gem 'plezi'
app.rb
:
#!/usr/bin/env ruby
# require the gems
require 'bundler'
Bundler.require(:default, ENV['ENV'].to_s.to_sym)
# handle requests
class MyController
# Http
def index
Iodine.log request_data_string
end
# Websockets
def on_message data
write ERB::Util.html_escape(data)
end
def pre_connect
puts Iodine.log(request_data_string)
true
end
def on_open
write 'Welcome!'
end
# formatting the request data
protected
def request_data_string
out = String.new
out << "Request headers:\n"
out << (request.headers.to_a.map {|p| p.join ': '} .join "\n")
out << "\n\nRequest cookies:\n"
out << (request.cookies.to_a.map {|p| p.join ': '} .join "\n")
out << "\n\nAll request data:\n"
out << (request.to_a.map {|p| p.join ': '} .join "\n")
out
end
end
route '*', MyController
# # you can also set up logging to a file:
# Plezi.logger = Logger.new("filename.log")
然后
$ ruby app.rb
/etc/nginx/sites-available/domain.com
:
server {
server_name domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
当我连接到ws://domain.com/
时,应用说:
Iodine 0.1.19 is listening on port 3000
Plezi is feeling optimistic running version 0.12.21.
Press ^C to stop the server.
Request headers:
connection: upgrade
host: localhost:3000
x-forwarded-for: 11.111.111.111
pragma: no-cache
cache-control: no-cache
origin: http://www.websocket.org
sec-websocket-version: 13
user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
accept-encoding: gzip, deflate, sdch
accept-language: en-US,en;q=0.8
sec-websocket-key: Qs2LMnJ12SjclOxlrYKwlg==
sec-websocket-extensions: permessage-deflate; client_max_window_bits
Request cookies:
All request data:
io: #<Iodine::Http::Http1:0x007fd5a0006b08>
cookies: {}
params: {:encoding=>"text"}
method: GET
query: /?encoding=text
version: 1.1
time_recieved: 2015-12-09 11:24:16 +0200
connection: upgrade
host: localhost:3000
x-forwarded-for: 11.111.111.111
pragma: no-cache
cache-control: no-cache
origin: http://www.websocket.org
sec-websocket-version: 13
user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
accept-encoding: gzip, deflate, sdch
accept-language: en-US,en;q=0.8
sec-websocket-key: Qs2LMnJ12SjclOxlrYKwlg==
sec-websocket-extensions: permessage-deflate; client_max_window_bits
headers_complete: true
client_ip: 11.111.111.111
scheme: http
host_name: localhost
port: 3000
path:
original_path: /
query_params: encoding=text
host_settings: {:index_file=>"index.html", :assets_public=>"/assets", :public=>nil, :assets_public_regex=>/^\/assets\//i, :assets_public_length=>8, :assets_refuse_templates=>/(erb|coffee|scss|sass|\.\.\/)$/i}11.111.111.111 [2015-12-09 09:24:16 UTC] "GET / http/1.1" 200 1659 0.6ms
访客nginx
访问日志:
10.0.0.1 - - [09/Dec/2015:10:55:44 +0200] "GET /?encoding=text HTTP/1.0" 200 1526 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
主机nginx
访问日志:
11.111.111.111 - - [09/Dec/2015:10:55:44 +0200] "GET /?encoding=text HTTP/1.1" 200 1526 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
chrome
的开发者工具控制台:
与'ws://domain.com/?encoding = text'的WebSocket连接失败:WebSocket握手期间出错:意外响应代码:200
答案 0 :(得分:1)
这不是一个答案,只是我对如何调试问题的想法。
将请求的标头输出到日志将提供有关该问题的更多信息(在代理标头更改后,应用程序看到的标头可能与预期不同)。
这里是一个使用Plezi的小应用程序,它既是websocket Echo服务器,也应该将标题打印到日志中(默认日志是STDOUT,但你也可以使用文件),允许你查看nginx设置和请求标头:
gemfile
:
gem plezi
使用thin
或任何其他服务器非常重要 ,因为Plezi使用它自己的碘服务器。
app.rb
文件:
#!/usr/bin/env ruby
# require the gems
require 'bundler'
Bundler.require(:default, ENV['ENV'].to_s.to_sym)
# handle requests
class MyController
# Http
def index
Iodine.log request_data_string
end
# Websockets
def on_message data
write ERB::Util.html_escape(data)
end
def pre_connect
puts Iodine.log(request_data_string)
true
end
def on_open
write 'Welcome!'
end
# formatting the request data
protected
def request_data_string
out = String.new
out << "Request headers:\n"
out << (request.headers.to_a.map {|p| p.join ': '} .join "\n")
out << "\n\nRequest cookies:\n"
out << (request.cookies.to_a.map {|p| p.join ': '} .join "\n")
out << "\n\nAll request data:\n"
out << (request.to_a.map {|p| p.join ': '} .join "\n")
out
end
end
route '*', MyController
# # you can also set up logging to a file:
# Plezi.logger = Logger.new("filename.log")
开始:
ruby app.rb
或(使用任何端口号,默认为3000):
./app.rb -p 3000
这应该是一个足够好的应用程序,用于测试和调试任何标头。
在您的问题中发布标题数据以获取任何进一步的帮助。可能是没有正确解析标题以进行websocket连接。
我注意到Plezi给出的响应是200 OK状态代码 - 意味着该请求被假定为Http请求。
这意味着它可能是一个标题问题,因为该请求未被识别为websocket升级请求。
在我的机器上,以下设置允许我代理Http请求和Websocket请求(但我使用Plezi,它允许我使用Websockets,Plezi的Http和Rack应用程序(即Rails) )在同一个港口):
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
}
...但你的配置也有效(只是混乱了Http&#39; s keep-alive,而不是websockets)......
...所以我怀疑是否发现了这个问题。
答案 1 :(得分:0)
可能更容易设置的方法是从主机js
直接代理到应用:
nginx
(主持人):
/etc/nginx/sites-available/domain.com
但是如果你想拥有相同的nginx配置,无论应用程序是在主机上还是在guest虚拟机上运行,你都可以用两个server {
server_name domain.com;
location / {
proxy_pass http://10.0.0.109:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
来代理它。
nginx
(主持人):
/etc/nginx/sites-available/domain.com
在此处传递server {
server_name domain.com;
location / {
proxy_pass http://10.0.0.109:80;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
标题至关重要。否则,guest Host
可能会让错误的虚拟主机处理请求。这是我在很长一段时间内无法实现的。
nginx
(嘉宾):
/etc/nginx/sites-available/domain.com