-출처: https://sungwoncho.io/run-multiple-apps-in-one-droplet/
It is possible to run more than one apps in a single virtual private server.
Having used Platform-as-a-Service products like Heroku most of the time, this basic idea was so foreign to me. Now that I am running several apps in my server, I would like to share how I am doing so.
I am using Nginx as a HTTP server. I am also using Digital Ocean, but this post can be generalized to any infrastructure providers.
Our goal
We want to run two apps in our server: a NodeJS and a Meteor app. We want to make it so that users can go to node_app.com
to use NodeJS app, and meteor_app.com
to use Meteor app.
Reverse proxy
What we want to do in this situation is to set up a reverse proxy using Nginx. A reverse proxy is like a traffic light that lets users connect to multiple applications or servers.
In our case, we can have Nginx listen on port 80 using node_app.com
and meteor_app.com
as server_name
.
This way, when a request comes in for node_app.com
, Nginx routes it to the NodeJS app. A request for meteor_app.com
will be routed to Meteor app.
Here are the steps to set it up:
1. Run apps on different ports
Using forever, we run the NodeJS app by executing the following in the app root directory.
NODE_ENV=production forever start [app_file_name]
We could do the same with the Meteor app.
forever start [app_file_name]
Let us say that the node app was running on the port 2368, and Meteor app on the port 3000.
By the way, forever is a handy tool to keep the script running, well… forever.
2. Make Nginx configuration files
In /etc/nginx/sites-available
, we make two configuration files.
node_app.conf
server {
listen 80;
server_name node_app.com
location / {
proxy_pass http://127.0.0.1:2368;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}
}
meteor_app.conf
server {
listen 80;
server_name meteor_app.com
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}
}
All we are doing is making Nginx listen on the port 80 using different server_name
, and routing the requests to either port 2368 or 3000 depending on the server_name
that request is hitting.
server_name
is an Nginx directive. The official doc says:
Server names are defined using the server_name directive and determine which server block is used for a given request.
3. Restart Nginx
To apply the changes, you need to restart Nginx by running:
sudo service nginx restart
Before you do that, you can test if all the configurations are valid by running:
nginx -t
If Nginx fails to restart for some reason, you can always check the log at /var/log/nginx/error.log
. It is the default path for error logs unless you specify one in the config.
Conclusion
You can set up a reverse proxy using Nginx to listen on port 80 and route requests to other ports on which different apps are running. All this can be done in one server.