I have 3 apps written in node js using the express module. Every app supports http and https. I created that for three different ports. I want to run all apps in a single pc with single IP but different domains. I have a linux server. Also, I heard nginx may be able to do it. I want like following example
Localhost:3000 -> www.domain
Localhost:3001 ->subdomain.domain
Localhost:3002 -> www.anotherdomain
How can i do it with it in the linux environment? Please guide me using suitable examples.
I have 3 apps written in node js using the express module. Every app supports http and https. I created that for three different ports. I want to run all apps in a single pc with single IP but different domains. I have a linux server. Also, I heard nginx may be able to do it. I want like following example
Localhost:3000 -> www.domain.
Localhost:3001 ->subdomain.domain.
Localhost:3002 -> www.anotherdomain.
How can i do it with it in the linux environment? Please guide me using suitable examples.
Share Improve this question asked Jul 13, 2021 at 15:41 GayanthaGayantha 4985 silver badges11 bronze badges 1- I voted to close this question because it is not a programming question and it is off-topic on Stack Overflow. Non-programming questions about your website should be asked on Webmasters and questions about managing your servers should be asked on Server Fault. In the future, please ask questions like this in one of those two places. – Stephen Ostermiller ♦ Commented Dec 14, 2021 at 14:51
2 Answers
Reset to default 4- Firstly, install NGINX https://docs.nginx./nginx/admin-guide/installing-nginx/installing-nginx-open-source/.
- Edit the config file of nginx in the path like
/etc/nginx/
It is good for you to learn editing the config file first. https://docs.nginx./nginx/admin-guide/web-server/web-server/
- Here are some examples:
server {
listen 80;
server_name www.domain.;
location / {
proxy_pass http://Localhost:3000;
}
}
server {
listen 80;
server_name subdomain.domain.;
location / {
proxy_pass http://Localhost:3001;
}
}
server {
listen 80;
server_name www.anotherdomain.;
location / {
proxy_pass http://Localhost:3002;
}
}
When you use domains to visit your page, the configuration above may help you. Whats more you should specify one server as your default server when you visit with ip address.
Assuming you've already installed Nginx (apt install Nginx).
Just create a separate virtual host file per each server.
For example, it can be like this for www.domain.
/etc/nginx/sites-enabled/domain..conf
server {
listen 80;
server_name www.domain.;
location / {
proxy_pass localhost:3000;
}
}
and like this for subdomain.domain.
/etc/nginx/sites-enabled/subdomain.domain..conf
server {
listen 80;
server_name subdomain.domain.;
location / {
proxy_pass localhost:3001;
}
}
I'm avoiding such things as SSL, root file, etc as this can be found elsewhere.
https://hackprogramming./how-to-setup-subdomain-or-host-multiple-domains-using-nginx-in-linux-server/
Don't forget to test nginx sudo nginx -t
and restart it to pick up changes (sudo service nginx restart
)