Node Application structure for subdomains - node.js

I am looking at building a website using node however my idea would be best with the use of subdomains. What i wanted to know is do i need to run each subdomain as a separate app and point to them with nginx?
Current Structure
- client
-- assets
--- js
--- css
--- img
- server
-- app
--- home
--- subapp1
--- subapp2
-- node_modules
-- server.js
-- config.js
The above structure is the thought that each app will run ny itself however they have common views that should be shared. Server.js will run them all. I just need to know if this is correct structure and if there is any cleaner or better alternative. Thanks

I'd suggest setting up Nginx to reverse proxy to 2 NodeJS apps. Run 1 app on port 8081 for example (service1) and the other on 8082 (service2) like this:
upstream service1 {
ip_hash;
server localhost:8081;
}
upstream service2 {
ip_hash;
server localhost:8082;
}
server {
listen 0.0.0.0:80;
server_name service1.mydomain.com;
location / {
index index.html index.htm;
proxy_pass http://service1;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 0.0.0.0:80;
server_name service2.mydomain.com;
location / {
index index.html index.htm;
proxy_pass http://service2;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

Related

nginx throws 404 on redirecting through proxy_pass to nodejs app

I'm serving multiple nodejs apps on a single server through pm2 and using nginx to manage reverse proxies. Right now if I use the server's ip and app port to reach the apps directly it all works fine. But if I try to navigate to my apps through the location paths set in the nginx config then I get 404 errors.
Below is my nginx default config:
upstream frontend {
server localhost:3000;
}
upstream backend {
server localhost:8000;
}
server {
listen 443 ssl;
server_name <redacted>;
ssl_certificate <redacted>.cer;
ssl_certificate_key <redacted>.key;
error page 497 301 =307 https://$host:$server_port$request_uri;
location /app/frontend {
proxy_pass http://frontend;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
}
location /api {
proxy pass http://backend;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
}
}
server {
listen 80;
server_name <redacted>;
return 301 https://$server_name$request_uri;
}
Now when I try to go to https://<server ip>:3000, the frontend loads just fine but if I go to https://<server ip>/app/frontend, I get the following 404 error:
Although the index.html loads up, it tries to find the static assets on https://<server ip>/ but rather should try to find them on https://<server ip>:3000. This is the exact behaviour that I'm trying to achieve.
What I have tried so far:
Using rewrites
Adding trailing slashes to both location path and proxy_pass
I know this can be solved by changing the app's base url or the build directory but that is not what I'm looking for.
Any help would be highly appreciated.

Nginx cache some location(static files) from another reverse proxy location

I have backend web-application working on 8081 port. It is Java spring boot application like uber-jar. So, static files placed into jar file. I have nginx as frontend. And I want to setting up cache static files on frontend. I thought what it is something like this:
proxy_cache_path /tmp levels=1:2 keys_zone=my_cache:10m max_size=10g
inactive=60m use_temp_path=off;
server {
listen 80;
server_name site.ru;
location /my_app {
proxy_pass http://127.0.0.1:8081;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /my_app/.+\.css {
proxy_pass http://127.0.0.1:8081;
proxy_cache my_cache;
proxy_cache_valid 200 1d;
}
}
... but it is not working. I believe what I on right way, though a little bit wrong

How do you serve static files from an nginx server acting as a reverse proxy for a nodejs server?

My current nginx config is this:
upstream nodejs {
server 127.0.0.1:3000;
}
server {
listen 8080;
server_name localhost;
root ~/workspace/test/app;
index index.html;
location / {
proxy_pass http://nodejs;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I'm very very new to nginx, but at the very least I know that nginx is better than node/express at serving static files. How can I configure the server so that nginx serves the static files?
I solved it using this new configuration:
upstream nodejs {
server localhost:3000;
}
server {
listen 8080;
server_name localhost;
root ~/workspace/test/app;
location / {
try_files $uri #nodejs;
}
location #nodejs {
proxy_redirect off;
proxy_http_version 1.1;
proxy_pass http://nodejs;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Thanks to the following Stack Overflow post:
How to serve all existing static files directly with NGINX, but proxy the rest to a backend server.
You'll probably want another location block within your server for the static files.
location /static {
alias /path/to/static/files;
}
This uses the alias directive.
Then you can hit files at localhost:8080/static/some_file.css
P.S. You don't need the root or index that you have set currently.
(root is similar to alias with a slight difference in usage)

Nginx, Node, Angular - Subfolder API/URL configuration

I currently have a node/angular app that runs as expected when pointed directly to the port configured (8081 for the purposes of explaining my situation). I'm able to post,get,put,delete as expected.
My goal is to have the node application running at mydomain.com/subfolder. When nginx is configured with the location of '/', everything works as expected. Config below:
upstream app_yourdomain {
server 127.0.0.1:8081;
}
server {
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_yourdomain/;
proxy_redirect off;
}
}
As soon as I change the location to /subfolder, however, my get,post,put,delete requests return 404 responses. The index.html configured in the node application is returned though. Configuration below:
upstream app_yourdomain {
server 127.0.0.1:8081;
}
server {
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
location /subfolder {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_yourdomain/;
proxy_redirect off;
}
}
In my angular factory, I have my requests structured like return $http.get('/subfolder'); or return $http.post('/subfolder', {data: data});.
And, within my node application, I have the routes defined like app.get('/subfolder', somefunction); or app.post('/subfolder', somefunction);
Again, when I have the application running from the root of the domain, it works fine. When I have it configured to be in a subfolder of the domain, however, the requests no longer work.
My end goal would be to have multiple node applications running from sub-folders of a main domain. I've been fighting with this for a while, and found several articles for hosting mutliple node apps on a single server, but they seem geared toward having separate domains. I'd like (if possible) for these to run as separate apps for the same domain.
Any thoughts/tricks/pointers? Thanks!
Modify Your Nginx File to look like this:
upstream node{
server 127.0.0.1:3000;
}
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
location /node {
rewrite /node(.*) $1 break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://node;
proxy_redirect http://node/ /node;
}
I got the above from here: http://skovalyov.blogspot.com/2012/07/deploy-multiple-node-applications-on.html and it works for me

using nginx to serve multiple apps from unique IPs

I've got two IPs associated with my VPS, and am trying to set this up to serve two node apps. Here's my configuration:
In /etc/nginx/sites-enabled/domain1:
upstream app_domain1 {
server 127.0.0.1:4000;
}
server {
listen 0.0.0.0:80;
server_name IP1.xxx.xxx.xxx;
access_log /var/log/nginx/domain1.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nninx-Proxy true;
proxy_pass http://app_domain1/;
proxy_redirect off;
}
}
And in /etc/nginx/sites-enabled/domain2
upstream app_domain2 {
server 127.0.0.1:3000;
}
server {
listen 0.0.0.0:80;
server_name IP2.xxx.xxx.xxx;
access_log /var/log/nginx/domain2.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nninx-Proxy true;
proxy_pass http://app_domain2/;
proxy_redirect off;
}
}
And in /etc/nginx/sites-enabled, I ran:
ln -s /etc/nginx/sites-available/domain1 domain1
ln -s /etc/nginx/sites-available/domain2 domain2
Now, when I go to /var/www/domain1 and run "node app.js" on the correct port, I can visit the relevant IP address and see the app running, but the same is not true for domain2 (I checked that it's running on the correct port for this config. The request just times out - no response at all.
So how can I troubleshoot this?
Update:
If I go to the ports directly, I see both apps available on both IPs, so:
IP1.xxx.xxx.xxx:4000 gives me the app for domain1
IP1.xxx.xxx.xxx:3000 gives me the app for domain2
and
IP2.xxx.xxx.xxx:4000 gives me the app for domain1
IP2.xxx.xxx.xxx:3000 gives me the app for domain2
So it's treating each IP address as the same.
server_name accepts only domains but not IP addresses.
You have typo mistake in proxy headers, change this:
proxy_set_header X-Nninx-Proxy true;
with this:
proxy_set_header X-NginX-Proxy true;

Resources