Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proxy) supports websockets #1827

Merged
merged 3 commits into from
Dec 3, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions kong/core/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ return {
ngx.log(ngx.ERR, "failed the initial dns/balancer resolve: ", err)
return responses.send_HTTP_INTERNAL_SERVER_ERROR()
end

-- Websocket
local is_upgrade = ngx.var.http_connection and ngx.var.http_connection:lower() == "upgrade"
ngx.var.upstream_connection = is_upgrade and "upgrade" or "keep-alive"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic changed here: it is relying on the value of the Connection header, and not Upgrade anymore. It should rely on Upgrade.

Also, any particular reason for not implementing this in a map anymore?

Following those 2 rules, this could/should be:

map $http_upgrade $upstream_connection {
    default ''; # assumed keepalive connection by default since this is HTTP/1.1
    websocket upgrade; # set 'Connection: Upgrade' if 'Upgrade: WebSocket'
}

I think the upstream Upgrade header should also receive particular attention, being cleared by default, and only forwarded when its value is Upgrade: WebSocket

map $http_upgrade $upstream_upgrade {
    default '';
    websocket websocket;
}

end,
-- Only executed if the `resolver` module found an API and allows nginx to proxy it.
after = function()
Expand Down
4 changes: 4 additions & 0 deletions kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,19 @@ server {
location / {
set $upstream_host nil;
set $upstream_url nil;
set $upstream_connection nil;

access_by_lua_block {
kong.access()
}

proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $upstream_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $upstream_connection;
proxy_pass_header Server;
proxy_pass $upstream_url;

Expand Down
44 changes: 44 additions & 0 deletions spec/02-integration/05-proxy/04-websockets_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local client = require "resty.websocket.client"
local helpers = require "spec.helpers"
local cjson = require "cjson"

describe("Websockets", function()
setup(function()
assert(helpers.start_kong())

assert(helpers.dao.apis:insert {
request_path = "/ws",
strip_request_path = true,
upstream_url = "http://sockb.in"
})
end)

teardown(function()
helpers.stop_kong()
end)

local function make_request(uri)
local wb = assert(client:new())
assert(wb:connect(uri))
assert(wb:send_text("testing Kong"))

local data = assert(wb:recv_frame())
assert.equal("testing Kong", cjson.decode(data).reqData)

assert(wb:send_close())

return true
end

it("works without Kong", function()
assert(make_request("ws://sockb.in"))
end)

it("works with Kong", function()
assert(make_request("ws://"..helpers.test_conf.proxy_ip..":"..helpers.test_conf.proxy_port.."/ws"))
end)

it("works with Kong under HTTPS", function()
assert(make_request("wss://"..helpers.test_conf.proxy_ssl_ip..":"..helpers.test_conf.proxy_ssl_port.."/ws"))
end)
end)