Mutliple Node app with Nginx - node.js

I'd like to run multiple node app throught a Nginx server for the reverse proxy. I actually have something like that in my config file :
server {
listen 80;
server_name http://my.server.url;
location / {
root /absolute/path/to/index/;
index index.html;
}
location /foo/ {
proxy_pass http://127.0.0.1:3000; # here is an nodejs app runing
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 : In my nodejs app, I receive url with the '/foo/' before.
I try to monkey arround with rewrite but honestly I'm confuse, so I'd like to get '/bar/' instead of '/foo/bar/', how should i proceed?

What you need is called rewrite in nginx
location /foo/ {
rewrite ^/foo(.*)$ /$1 last;
proxy_pass http://127.0.0.1:3000; # here is an nodejs app runing
}

Related

How can I route to multiple NodeJS applications using Nginx?

I have 2 NodeJS applications running locally on port 3000 and 3001. I'm trying to route to them individually by using my_ip/tilbudsfinneren for one of them, and my_ip/hms for the other one.
My sites-available file looks like this:
server {
listen 80;
listen [::]:80;
root /var/www/my_ip/html;
index index.html index.htm index.nginx-debian.html;
server_name my_ip www.my_ip;
location /tilbudsfinneren {
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;
}
location /hms {
proxy_pass http://localhost:3001;
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;
}
}
Whenever I try to reach my_ip/hms or my_ip/hms/ping it does not reach the NodeJS application, I get get "Cannot GET /hms". Is there an issue with my nginx configuration, or is there maybe something else wrong?
If you want to serve both apps as reverse proxy you must use rewrite methods in both locations.
server {
listen 80;
index index.html index.htm index.nginx-debian.html;
location /tilbudsfinneren/ {
rewrite ^/tilbudsfinneren/(.*)$ /$1 break;
proxy_pass http://localhost:3000;
#other config...
}
location /hms/ {
rewrite ^/hms/(.*)$ /$1 break;
proxy_pass http://localhost:3001;
}
}

Nginx screen appering on first request to the domain

The first requests in the browser to this following API's are appering the Nginx screen.
Goeat Api GympointApi
Idk what is wrong on my proxy configuration
server {
listen 80;
server_name gympoint.lauradeveloper.com.br;
location / {
proxy_pass http://localhost:3333;
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;
}
}
server {
listen 80;
server_name goeat.lauradeveloper.com.br;
location / {
proxy_pass http://localhost:4000;
}
}
It's working completely fine but on the first request to the browser, shows this nginx screen. Looking my proxy configuration, does anyone know what i'm did wrong?
I kind solved this. I had to put these same options related to proxy on the other server config.

hosting nodejs applications using nginx in ec2 AWS

/etc/nginx/nginx.conf file
in http section, given the below informaiton.
include /etc/nginx/conf.d/*.conf;
/etc/nginx/conf.d/myapp.conf file content
server {
listen 80;
listen [::]:80;
server_name _;
index index.html index.htm;
location / {
proxy_pass 'http://XXXXX:3005/';
}
location /admin/ {
proxy_pass 'http://XXXX:3000/';
}
}
http://XXXX is redirecting to the main page.
http://xxxxx/admin/ is not redirecting. Instead it is throwing below errors in chrome console.
error image
The same site is working fine http://xxxx:3000 it is working fine. No issues.
Whatelse am I missing?
Note: OS is Amazon Linux & I have tested the same configuration with sample help world node js program & it is working fine.
Make sure that your routing is correct and that you have restarted nginx after configuration.
Try the following configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3005;
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 /admin {
proxy_pass http://APP_PRIVATE_IP_ADDRESS: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;
}
}

Host multiple meteor application on DigitalOcean

I need to run two meteor applications at the same Digital Ocean droplet. Following this tutorial I successfully get both apps to work on my-domain.com:3000 and my-domain-1.com:3001, but I can't get reversing proxy using Nginx done. Following the tutorial I came up with the following:
First app configuration file:
server {
listen 80;
server_name http://saveting.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Second app configuration:
server {
listen 80;
server_name http://downloadinstagramvideo.com;
location / {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
EDIT1 :
The problem was that nginx has to be restarted before implementing changes. Works after using:
sudo service nginx start
The server_name directive should not include the scheme name, use:
server_name saveting.com;
and
server_name downloadinstagramvideo.com;

node & express + nginx with multiple locations

I am planning to run 2 node apps with express under one sub domain, with nginx serving the static files. In both node apps I use:
app.use(express.static(path.join(__dirname)));
I have the following nginx config:
server {
listen 80;
server_name sub.domain.com;
index index.html;
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 / {
proxy_pass http://localhost:232;
}
location ~ ^/(dist/|img/|app/|css/) {
root /var/www/app/main/;
}
location /admin {
proxy_pass http://localhost:233;
rewrite ^/admin /$1 break;
}
location ~ ^/admin/(dist/|img/|app/|css/) {
root /var/www/app/admin/;
access_log off;
expires max;
}
}
With this setup everything is great for the 'main' app (accessed at sub.domain.com), but for the 'admin' app (sub.domain.com/admin) the same static files are served as for the 'main' app. How should I modify my setup to achieve the correct behavior?
Check using your dev-tools to make sure that the static assets are attempting to be loaded from the correct URL; that is, http://sub.domain.com/admin/css/ or /admin/css/ etc. It is possible that the paths are being generated with respect to their projects root and appear as /css/, so when the browser tries to retrieve them, it goes to http://sub.domain.com/css/.
I think the order of location blocks in your configuration may be the issue. For more detailed info, check out this link. Try using this refactored configuration -
server {
listen 80;
server_name sub.domain.com;
index index.html;
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 ~ ^/admin/(dist/|img/|app/|css/) {
root /var/www/app/admin/;
access_log off;
expires max;
}
location /admin {
proxy_pass http://localhost:233;
rewrite ^/admin /$1 break;
}
location ~ ^/(dist/|img/|app/|css/) {
root /var/www/app/main/;
}
location / {
proxy_pass http://localhost:232;
}
}

Resources