Make Node.js + socket.io + Angular + Nginx + SSL work
Recently, I built a messaging application with Angular, socket.io and express.js. It worked properly on localhost on my local machine. But after I deployed it to the production server with SSL installed, all functions are working except that socket.io connection could not be established anymore. I finally solved this problem and in this article I will talk about my solution.
The application uses express.js as the backend server which can be accessed via localhost:3000 internally. The express.js server also serves the APIs and is mounted on by the socket.io server.
In order to tell if an HTTP request sent from the web browser needs to be responded by Angular, socket.io server or the API server, we need to structure the base URLs of these requests differently. Below is my solution:
- socket.io: https://yourdomain.com/chat
- APIs: https://yourdomain.com/api
- Angular: https://yourdomain.com
The Angular official website has talked about how to configure the Nginx server to serve an Angular application here.
For URLs of requests made to APIs and socket.io, we can set up Nginx as a reverse proxy server to direct requests for both APIs and socket.io to server.js file which contains the Node.js server, listening on…