wuampa
wuampa

Reputation: 363

Nginx as a forward proxy for websockets and post requests

I'm currently using nginx as a forward proxy for websockets, and it has been working fine up to now. Client connects with:

var ws = new WebSocket('ws://10.30.0.142:8020/');

I want to forward a post request as well. In this case, client adds /post to the ws address, so that the address is extended to 'ws://10.30.0.142:8020/post'. However requests to that address return:

http://10.30.0.142/post 404 (Not Found)

I'm using the following configuration file (nginx.conf), which most probably is wrong for the post request (location /post/):

upstream websocket {
        server 127.0.0.1:8010;
}

server {
        listen       8020;
        server_name  server;
        root /var/www/html;
        expires off;

        keepalive_timeout 0;

        access_log /dev/null;

        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }

        location /post/ {
            proxy_pass http://127.0.0.1:8010;
       }

        location ~* \.(?:css|js|map|jpe?g|gif|png)$ { }

    }

 }

How should I configure this file correctly to solve this issue?

Upvotes: 3

Views: 2173

Answers (1)

Jonas
Jonas

Reputation: 1593

Luckily, the fix is super simple: Just change the line location /post/ { to location /post {, the extra slash matches only request to /post/<something else> which, based on your description, isn't what you want.

In fact, you may even want to change that line to location =/post { if you want to match only requests to /post, not requests to /post<some other string> or /post/<some other string> as well.

Upvotes: 3

Related Questions