Serve express dynamic URL using NGINX with Nodejs reverse proxy - node.js

I am using Express for serving static files and dynamic URLs on my nodejs app. I am generating a URL based on the :id in Mongodb and then on NGINX I am using Nodejs reverse proxy to serve this path to the user but it doesn't work whatever I try. It always says "CANNOT GET.."
I am running NGINX 1.14.2, Node 8.14.0. I have tried NGINX rewrite directive and various wild cards ( ~, ~^, ., etc.) but none of them have worked. I have included the current express code and NGINX config below. Any help is hugely appreciated
Express
router.get('/dynamic-uri/:fooId', function (req, res) {
res.render('index');
});
The resultant URL with :fooId is
https://example.com/myNodeApp/dynamic-uri/5c35e51228122504b57126d7
NGINX Config
location /myNodeApp/dynamic-uri {
proxy_pass http://x.x.x.x:3000;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
}
Of course the :fooId doesn't exist as a static path and to serve static files i have the following block which works fine
location ~* /myNodeApp/(static|img|media|app|css|js) {
proxy_pass http://x.x.x.x:3000;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
}

Related

nginx serving express js static folder with files server-index

i am new in nginx and i cant access my public files please guide me i am very new on doing this.
this is my code on express js
app.use('/p',auth, express.static('public/uploads'),
serveIndex('public/uploads', {'icons': true}))
and this is the code on nginx
location =/file/{
proxy_pass http://localhost:8081/p/;
}
it does work like this on my page when i go to http://mywebsite.com/file/ but when we click a file here which will show that directory. it will error out or stay white screen
here is the full code of the location
#this is the file system
location =/file/{
proxy_pass http://localhost:8081/p/;
}
#this is the express api
location /v1
{
rewrite ^/v1/(.*)?$ /$1 break;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8081;
}
#this is the front end
location /
{
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:5000;
}

nginx doesn't redirect backend

I've nginx config that route frontend app to specific port, and backend to specific route
here's my configs
server {
listen 80;
server_name test.com www.test.com;
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;
proxy_redirect off;
}
location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
}
}
when trying to access test.com directly it works, but when trying to access test.com/api , it doesn't work, then if return to main path test.com it also doesn't work, it seems like nginx stopped working after accessing the /api

Nginx reverse proxy only xhr/ajax requests

i have a laravel app and i want all requests to domain.test/api to be proxied to nodeJs but only if it is an xhr request. meaning that if a user types in a browser domain.test/api i want to give him a 404 but if the request is made with ajax i want to give him the response.
the following configuration proxies all:
location ~* ^\/api(.*)$ {
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300;
proxy_pass http://localhost:8081;
}
Is what i want to do possible using nginx? if so, please do suggest your solutions?

NGINX server with 2 NodeJS Apps

I will make it short without introductions ..
I'm having a serious issues with NGINX configuration (on google cloud) to make 2 nodejs apps working on the same domain with different PORTS
let's say app1 is working on port 3002, app2 working on port 3003
app1
location / {
root /home/bitnami/project_name;
proxy_pass http://127.0.0.1:3002;
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 X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
app2
location /app2 {
root /home/bitnami/project_name;
proxy_pass http://127.0.0.1:3003;
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 X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
when I surf www.example.com/app2, I get 404 page
I know some of u will say that this Q has been asked before, believe me I've tried all possible solutions on stackoverflow .. non has worked with me
Note: app1 location it has to be the main domain so (/) the main domain URL without path
I believe your code does not use relative paths, thats why you are getting this error, add this line:
rewrite ^/app2(.*) /$1 break;
and no root required for proxy pass, your new code shall look like this:
location /app2 {
#root /home/bitnami/project_name;
proxy_pass http://127.0.0.1:3003;
#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 X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
#proxy_set_header X-NginX-Proxy true;
#proxy_redirect off;
rewrite ^/app2(.*) /$1 break;
}
The first location block captures requests for all requests of your domain, leaving the second block never used. Put the second block before the first one and it should work.

Hosting two Node.JS apps on same domain

I have two node js applications I am running on the same box and I would like for it to run the first node js app for all routing except if the url is www.domain.com/blog to go to the other node js application. Is this even possible with this setup or do I have to setup subdomains and use nginx or something?
You can achieve this using nginx as a reverse proxy.
Assuming you have your blog node process running on port 3000 and another node process on 3001 a simple config would look like;
upstream blog {
server 127.0.0.1:3000;
}
upstream other {
server 127.0.0.1:3001;
}
server {
listen 80;
server_name www.domain.com;
location /blog {
proxy_pass http://blog;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto tcp;
proxy_set_header X-NginX-Proxy true;
}
location / {
proxy_pass http://other;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto tcp;
proxy_set_header X-NginX-Proxy true;
}
}

Resources