Running a websocket server and a http server on the same server

portport-forwardingsocket

I am using nginx to to serve an instance of PHP application, and also at the same time running a Ratchet app to listen to a websocket connection. Problem is, both of these instance are using the same port (80).

How do I set up the server so that I can serve both?

Edit: Just came back to saw the question has been downvoted several times, understandably, I didn't really ask the question in a clear way. I apologize for that. I did do my due diligence though. I'll try clarify what I was asking, I may have misunderstood some aspects of the websocket protocol, in which case, please do correct me:

I know well that there can only be one process listening to a particular port, what I am actually having some problem undertand is the whole websocket thing, from what I understand, a websocket request begins with a HTTP handshake, after which there will be mechanism to 'upgrade' that session to the specific port.

From what I read around, it seems that this upgrade mechanism is handled by the webserver. So, how do I configure nginx to deal with this process?

All in all, the one thing that I am concerned about running the websocket server on port other than 80, is the possibility of the port being blocked. Is this concern unfounded? Any advice on how I should be setting this up?

Best Answer

A terribly written question, but the term I was looking for was reverse proxy.

Basically, just like how nginx can forward different request to several instances of the web app based on the path or hostname, it can also be configured to "forward" the request to a websocket server instance. e.g.:

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

upstream websocket{
    server localhost:8000;
}

map $http_upgrade $connection_upgrade {
    default Upgrade;
    '' close;
}

HTTP requests to http://hostname will be served the usual html page, while websocket connection to wss://hostname/socket will be forwarded to the websocket instance on the same server listening on port 8000.