10

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?

hndr
  • 412
  • 1
  • 5
  • 10
  • that_guy - please review http://serverfault.com/help/how-to-ask - make sure to do your due diligence before posting. While we like people asking questions, we also like people to do their homework. – ETL Feb 13 '14 at 22:47

3 Answers3

18

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.

hndr
  • 412
  • 1
  • 5
  • 10
2

A server can't have two different software listening on the same port/IP address. Otherwise, how would the traffic be routed to Application XYZ if multiple applications could be binding to the same port?

You can learn more about ports from Port (computer networking)

What you need to do is either one of the following options:

  1. Use a different port for one of the two application and allow that through your firewall.
  2. Use two different IP addresses - and bind each application to their respective IP addresses.

Your firewall just need to be configured according to what you set up.

mpromonet
  • 134
  • 1
  • 12
ETL
  • 6,513
  • 1
  • 28
  • 48
  • can you provide more information? would that cause problem with firewall? isn't one of the advantage of websocket is to avoid that? – hndr Feb 13 '14 at 22:33
  • 6
    **[Sharing port is possible](http://stackoverflow.com/questions/5969607/is-it-possible-to-use-port-80-for-both-http-and-web-socket-traffic#comment-35135305)** since You can have both the HTTP server and the Websocket server in one process. – Pacerier Mar 15 '15 at 21:02
0

You have two options:

  1. Bind one to a different port (e.g. 8000)
  2. (if possible) add an additional IP address to the server and bind the webserver and websocket server to different IP's
Teun Vink
  • 1,837
  • 11
  • 14