Can I upload multiple node.js apps under one domain? - node.js

I use DreamHost as my provider, and I have a VPS plan, allowing me to upload nodejs apps. However I wish to be able to have multiple small apps under one domain, for example
www.example.com/app1
www.example.com/app2
www.example.com/app3
Is this possible with Node.js?

You cant run multiple apps on a single port.
But we can run apps on different port and redirect the url to correct app using nginx e.g
server {
listen 80;
root /var/www;
server_name www.example.com;
location /app1 {
proxy_pass http://localhost:{port}; // app1 server Comment 1
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /app2 {
proxy_pass http://localhost:{port}; // app2 server Comment 2
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
This will redirect www.example.com/app1 to app1 server and www.example.com/app2 to app2 server. See comment 1 and 2.

You can't run multiple apps on single port but you can run them on different ports and can access them using those ports like
www.example.com //app1 (default http port is 80 and default https port is 443 hence you don't need to specify port for the apps using port 80)
www.example.com:3000 //app2

Related

nginx with two node aplications with socket IO

I have two applications running on the same machine. One listens on port 8080 and the other on 11180. SSL connection to the application on port 8080 works but i am having trouble setting up the other one.
To separate the requests heading to each of the applications, one is available at https://example.com and the other at https://example.com/v2
As I said, going to https://example.com works as intended, but going to https://example.com/v2 serves the correct html files but connects to the same server as going to https://example.com
I honestly have no idea what i am doing with nginx, but my config looks like this.
server {
listen 443 ssl;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /v2/ {
proxy_pass http://127.0.0.1:11180/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
}
location /socket.io {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
#proxy_redirect off;
}
}
It is worth mentioning that the first app listens on 8080 and its socket io on 8081, as for the second app, everything listens on 11180
Thanks a bunch in advance
I ended up adding another server block to the nginx config, and accessing it via www.example.com:port.
As the accepted answer say you could create a new server block... or you can even create a new conf file in the sites-available folder for the new domain with a new server block. Dont forget to link them to sites-enabled.

Nginx reverse proxy mapping does not redirect

I recently started tinkering to solve this problem I was having this problem. So I installed NGINX and set it up so that it forwards the incoming requests on port 80 to port 300 by creating a .conf file in /etc/nginx/conf.d/ location. Here is the configuration file.
server {
listen 80;
server_name xyz.xyz;
location / {
proxy_pass http://127.0.0.1:300;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
But when I hit xyz.xyz I get the default page and when I go to xyz.xyz:300 I get my NodeJS app. Any suggestion?
Seems like it was an issue with SELinux.
I just ran this command and it worked.
setsebool -P httpd_can_network_connect 1

Configure nginx for both Vue app front end and node.js backend

I have been searching for this answer for months and have never been able to resolve this. I am using nginx as my web server and using node.js for my backend apis and vue as my main front end webpage that uses webpack. I would like to be able to go to my Vue page by going to http://ip and then get access to the api services by going to http://ip/api. I dont have a domain name set up so I use IP address for the urls.
So far I built my Vue app and is sitting in the /var/www/html/Web/dist folder and it works if you go to the http://ip url but I can not access my node.js APIs. My node.js server is running on localhost port 3000. My nginx config looks like this:
server {
listen 80;
root /var/www/html/Web/dist;
}
server {
listen 80;
location /api/ {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
You are duplicating server configuration. Simply keep one server properties set :
server {
listen 80;
root /var/www/html/Web/dist;
location /api/ {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
That means :
listens to all request for all ip's on port 80
Evaluate relative path from root /var/www/html/Web/dist;
if there's a matching '/api/' in url pass it to localhost:3000
It should work as expected now

How to configure nginx to redirect to same app from multiple ports?

I have a node.js app and I am running nginx as a reverse proxy server. I want to access the same app through multiple ways.
Let the ip address of machine be 12.16.12.113.
Then I want the same app to be accessible through:
12.16.12.113:3000
test.example.com
I have 2 Configuration files for nginx in sites-available directory:
file1:
server {
listen 80;
server_name test.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
file2:
server {
listen 3000;
server_name default_server;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
The problem is that if I link both the files in sites-enabled directory, then only the app is accessible only through 12.16.12.113:3000. If I remove file2, then it is accessible through test.example.com.
But I want both the methods to work.
Edit 1:
Based on suggestion from this answer, I did the following test:
It appears that nginx is not listening to port 80. Why is this happening?
The are few ways to achieve that.
You can make your Node app listen on 12.16.12.113:3000 instead of on localhost:3000 and then you will need nginx only for port 80.
You can make your Node app listen on localhost:3000 (but only on localhost) and then proxy 12.16.12.113:3000 and port 80 by nginx.
You can make your Node app listen on localhost:4000 or 0.0.0.0:4000 on any interface and use nginx to proxy requests to port 80 and 3000.
But you will not be able to make them both listen on the same port and the same interface without problems.
This is the only advice that could be given considering the fact that you didn't include any code example of how your Node app is binding to the ports, which would be the only relevant information here.

ignore nginx port to add in url? When i change nginx port

I want to use nginx on different port. If i am running nginx on default port(80) and trying to use by the url (100.100.7.60) this is working fine.
server {
listen 80;
server_name 100.100.7.60;
location / {
proxy_pass http://100.100.7.60:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
But when i am changing nginx port 80 to 8080 or to any port. If now i am trying by (100.100.7.60). I am unable to run node.js application. Now if i want to run application my url should be like (100.100.7.60:8080).
server {
listen 8080;
server_name 100.100.7.60;
location / {
proxy_pass http://100.100.7.60:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Can anyone explain me what is problem? I want to change port number. But i do not want to add (:8080) in url. I want to make same url after changing port number
If you want to listen on a different port, you cannot exclude the port number from the URL. When you enter 100.100.7.60 into a browser, the protocol is HTTP and the port is 80 by default.
There is a (very bad) workaround to this:
server {
listen 80;
server_name 100.100.7.60;
return 301 http://100.100.7.60:8080;
}
This will redirect ALL traffic on port 80 to port 8080, which is (I assume) not what you want.
Instead, you should configure your DNS to point to this server using a subdomain eg myapp.example.com and then listen on 80 port with that server name to serve your app.

Resources