Caddy: How to make http and websocket work in the same path?

Created on 15 Dec 2019  路  2Comments  路  Source: caddyserver/caddy

I want make http and websocket work in the same path.

For example this is a test path: test.com/test/

if caddy get http(s) link, i want caddy return index.html.
if caddy get websocket link, i want caddy proxy to 127.0.0.1:10000.

How do I need to configure Caddyfile to achieve this?

question

Most helpful comment

Next time, please ask your usage questions on https://caddy.community as this is the issue board for bugs and feature requests.

In Caddy v1, you're missing one piece to make it work. You need to set up a rewrite that will translate any path that is intended to upgrade a request from HTTP to websockets to some dummy path that will be used for routing it to the proxy internally. Here, I used /websocket-proxy, because it's probably unlikely you'll be using that path for anything useful in your app. You can choose whatever you want here, users will never see that path anyways.

localhost {
    root /home/www
    gzip
    tls [email protected]

    rewrite / {
        if {>Connection} has Upgrade
        if {>Upgrade} is websocket
        to /websocket-proxy/{path}?{query}
    }

    proxy /websocket-proxy 127.0.0.1:10000 {
        without /websocket-proxy
        transparent
        websocket
    }
}

With Caddy v2, we now have matcher blocks which make this process a bit less confusing.

All 2 comments

now i use this config to proxy to 127.0.0.1:10000
websocket work normal, but webpage can't work, i don't know how to make webpage work.

localhost {
    root /home/www
    gzip
    tls [email protected]
    proxy /test 127.0.0.1:10000 {
        without /test
        websocket
    }
}

I found a success story here, but he use nginx:
https://www.chenxublog.com/2019/09/26/openresty-nginx-websocket-http-same-path.html

location /v2
{
    try_files /nonexistent @$http_upgrade;
}
location @websocket {
    proxy_redirect off;
    proxy_pass http://127.0.0.1:10000;#浠g悊杩欎釜ws閾炬帴
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $http_host;
}
location @ {
    root  /www/wwwroot/xxxxx.xxx.com;
    index index.php index.html index.htm default.php default.htm default.html;
}

Next time, please ask your usage questions on https://caddy.community as this is the issue board for bugs and feature requests.

In Caddy v1, you're missing one piece to make it work. You need to set up a rewrite that will translate any path that is intended to upgrade a request from HTTP to websockets to some dummy path that will be used for routing it to the proxy internally. Here, I used /websocket-proxy, because it's probably unlikely you'll be using that path for anything useful in your app. You can choose whatever you want here, users will never see that path anyways.

localhost {
    root /home/www
    gzip
    tls [email protected]

    rewrite / {
        if {>Connection} has Upgrade
        if {>Upgrade} is websocket
        to /websocket-proxy/{path}?{query}
    }

    proxy /websocket-proxy 127.0.0.1:10000 {
        without /websocket-proxy
        transparent
        websocket
    }
}

With Caddy v2, we now have matcher blocks which make this process a bit less confusing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhilmacFLy picture PhilmacFLy  路  3Comments

crvv picture crvv  路  3Comments

mikolysz picture mikolysz  路  3Comments

mschneider82 picture mschneider82  路  3Comments

jgsqware picture jgsqware  路  3Comments