Websocket (wss) connection issue (status 200 instead 101) between two nginx systems

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Websocket (wss) connection issue (status 200 instead 101) between two nginx systems

vergil
Hello,

I’ve a websocket (wss) connection issue (status 200 instead 101) between a
“server” (running nginx/1.14.2 reverse proxy) and a “black box” (running
nginx/1.8.1 web server with websocket).

The “server” has access to Internet and to the local network where is
connected the “black box”. I called it “black box” because I can’t change
anything except the nginx config file.

From local network, the nginx web site of the “black box” is working
properly including websocket connection.

From Internet, the “server”, the nginx reverse proxy gives me an access to
the nginx web site of the “black box” everything works except the websocket,
from my web browser I receive a status 200 but I should get 101 switching
protocol.

I’ve tried different setup without success.

Please let me know what you think,

Thanks a lot,

Regards,
YAGA

The “black box” wss websocket uses 80 (which is not usual) and its https
website uses 443.

“black box” web server nginx config (extract)

    server {
        listen  443 ssl;
        ssl_certificate     /opt/xxx/cert.crt;
        ssl_certificate_key /opt/xxx/cert.key;

        server_name  localhost;
        proxy_buffering off;
       
        location / {
            root /opt/xxx/web;
            try_files $uri $uri/ /index.html;
        }
        location /websocket {
            proxy_pass      <a href="https://127.0.0.1:80;">https://127.0.0.1:80;
        }
        location /api/ {
            proxy_pass      <a href="https://127.0.0.1:80;">https://127.0.0.1:80;
        }
        location /static/ {
            root /opt/xxx/website;
            expires 10d;
        }


“server” reverse proxy nginx config (extract)

server {
                listen 443 ssl;
                listen 80 ssl;
                server_name my_server.xyz;
                client_max_body_size 100M;
                proxy_buffering off;
                proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                location / {
                        proxy_pass https://192.168.1.20/;
                        auth_basic "Private";
                        auth_basic_user_file /etc/nginx/.htpasswd;
                }
                location /api/ {
                        proxy_pass https://192.168.1.20:80/api/;
                        auth_basic off;
                }
                location /websocket {
                        proxy_set_header Upgrade $http_upgrade;
                        proxy_set_header Connection upgrade;
                        proxy_http_version 1.1;
                        proxy_set_header Origin "";
                        proxy_pass https://192.168.1.20:80/;
                        auth_basic off;
                }
}

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,287709,287709#msg-287709

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Websocket (wss) connection issue (status 200 instead 101) between two nginx systems

Francis Daly
On Sat, Apr 18, 2020 at 05:39:38PM -0400, YAGA wrote:

Hi there,

> From local network, the nginx web site of the “black box” is working
> properly including websocket connection.

> From Internet, the “server”, the nginx reverse proxy gives me an access to
> the nginx web site of the “black box” everything works except the websocket,
> from my web browser I receive a status 200 but I should get 101 switching
> protocol.

Your config for the "black box" does not show the normal proxy_*
directives that are used for websockets.

Your config for the "server" does.

http://nginx.org/en/docs/http/websocket.html

Does anything change if you add those directives to the "black box" system?

> “black box” web server nginx config (extract)

>         location /websocket {
>             proxy_pass      <a href="https://127.0.0.1:80;">https://127.0.0.1:80;
>         }

> “server” reverse proxy nginx config (extract)

>                 location /websocket {
>                         proxy_set_header Upgrade $http_upgrade;
>                         proxy_set_header Connection upgrade;
>                         proxy_http_version 1.1;
>                         proxy_set_header Origin "";
>                         proxy_pass https://192.168.1.20:80/;
>                         auth_basic off;
>                 }

I don't actually know if a websocket connection will pass cleanly through
two reverse proxies. I guess this is as good a time as any to learn if
it can!

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Websocket (wss) connection issue (status 200 instead 101) between two nginx systems

vergil
Hi Francis,

Many thanks for your message and for your help, it’s very kind of you.

As you suggested, I’ve tried to add these lines on the “black box” side:

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

But unfortunately, without success, these lines didn’t improve the websocket
connection. So, I’ve decided to roll back to previous version without the
connection upgrade on the “black box”.

I’ve tried different changes to the setup and I finally found my mistake, on
the server side I’ve added an extra slash at the end:
proxy_pass https://192.168.1.20:80/;
I change this line with:
proxy_pass <a href="https://192.168.1.20:80;">https://192.168.1.20:80;

Now, it works smoothly.

The evil always comes from details…

Thanks again Francis for your time and your assistance,

Regards,
YAGA

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,287709,287727#msg-287727

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Websocket (wss) connection issue (status 200 instead 101) between two nginx systems

Francis Daly
On Mon, Apr 20, 2020 at 05:06:14PM -0400, YAGA wrote:

Hi there,

Great that you've found the fix!

> proxy_pass https://192.168.1.20:80/;
> I change this line with:
> proxy_pass <a href="https://192.168.1.20:80;">https://192.168.1.20:80;
>
> Now, it works smoothly.

And thanks for sharing the resolution with the list; that will hopefully
help the next person with the same problem find it more quickly :-)

Cheers,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx