Nginx: Redirect http request to tcp?

2019-04-01 11:10发布

问题:

I currently have the following setup:

  • nginx: port 80, act as a webserver and a proxy
  • nodejs: port 8888, running socket.io (which utilizes websockets)

So I tried using the newest development branch for nginx but it doesn't support websocket.

I gave tcp_proxy a try and it works great. (Although I tried it with another port for the connection and it didn't involve this reverse proxying based on urls)

My problem right now is that I am unable to route http requests that starts with /socket.io to the tcp_proxy.

tcp { 
  upstream nodejs {
     server  127.0.0.1:8888;
  }
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    tcp_nopush on; 
    tcp_nodelay off; 

    server {
        listen  80;
        server_name  www.mysite.com;
        root someroot;

        location /socket.io { 
          # How do I redirect to upstream nodejs? 
        }
    }
}

Thanks for the help!

标签: nginx