In this article I will tell you what problems will have to be solved in order to get a VPS with a really working node.js service. These are all basic things, but maybe someone else will come in handy.
Install node
Problem: the repository may not have a package for the correct node version. Alternatively, you may have two projects that require different versions of the node.
Solution: on development machines in such cases use nvm. On the combat server, it can also be easily used, just have to write a special script to start your server.
Access to port 80
Problem: the application must have superuser rights in order to read port 80 (for details, see CAP_NET_BIND_SERVICE,
man capabilities ). But launching a node with such rights is considered unsafe.
Solutions:
- use a proxy server like Nginx or HAProxy .
- redirect requests from port 80 to any one with a large number using
iptables
. Practically done with one command: iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000
- start with the rights of the superuser, but immediately after connecting to the socket the process will programmatically lower its privileges. Read more here .
Load balancing
Problem: node is single-threaded and cannot effectively use multi-core processors.
Solutions:
')
To use node directly or through Nginx?
The question was
discussed a lot where, but there is no consensus On the one hand, nginx is a reliable, proven solution over the years; on the other hand, node itself is able to do the same.
As a rule, one of those options is offered:
- Node does everything at once, right up to distribution of static files.
- node is behind haproxy.
- The node is behind Nginx and is used only for generating pages, everything else (static distribution, balancing, https operation) does Nginx
Restart of the fallen process
Problem: how to restart the server if it suddenly falls?
Solutions:
Updating a service with git
Described
here