Nginx + nodejs app location - node.js

it's very short question, but i'm overwhelmed. I need nodejs app run under domain.com/nodeapp/.
The problem is - it works correct if i write domain name like: domain.name/nodeapp/ so when i'm go to domain.name/nodeapp - corrupted version loads.
I need nginx to redirect correctly to location with /nodeapp/
Now i'm using next config:
location /nodeapp {
proxy_pass http://localhost:20100;
rewrite ^/nodeapp/?(.*)$ /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
How can i do it correct? Thanks!

This will do
location /nodeapp {
proxy_pass http://localhost:20100;
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 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 map routes by docker host, for each route, call a different container

I'm trying to map routes by docker host, for each route, call a different container.
I have a docker-compose with 2 services, and that services is in :5000 port. My nginx.conf is mapped following below code:
location /template-api {
rewrite ^/template-api/?(.*) /$1 break;
proxy_pass http://template-api:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
location /api-plan {
rewrite ^/api-plan/?(.*) /$1 break;
proxy_pass http://api-plan:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
When, I call localhost:8000/api-template/documentation, the route that calls a static file, or route that returns a static file. He is returning error in localhost:8000/swaggerui, with file is not found.
That erros happens because the swagger ui folder is in localhost:8000/api-template/swaggerui and localhost:8000/api-plan/swaggerui
To fix that error, I'm add to ngix, the conf to map / route:
server {
listen 8000;
access_log /var/log/nginx/api.log;
error_log /var/log/nginx/api_error.log;
server_name localhost;
location / {
proxy_pass http://api-plan:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
Now when I call localhost:8000/api-plan/documentation, results in success, but, when I call localhost:8000/template-api/documentation, the API redirects to localhost:8000/api-plan/ resulting in wrong route.
Try this:
server {
listen 8000;
access_log /var/log/nginx/api.log;
error_log /var/log/nginx/api_error.log;
server_name localhost;
location / {
proxy_pass http://$server_name:8080/swaggerUi;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
location /template-api {
rewrite ^/template-api/?(.*) /$1 break;
proxy_pass http://template-api:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
location /api-plan {
rewrite ^/api-plan/?(.*) /$1 break;
proxy_pass http://api-plan:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
}
You can use cookies to detect which backend should receive the /swaggerui request. This is highly insecure since users can edit the cookie and try to reach other hosts but since it's a local test environment it just works.
server {
listen 8000;
access_log /var/log/nginx/api.log;
error_log /var/log/nginx/api_error.log;
server_name localhost;
location ~ ^/swaggerui {
resolver 127.0.0.11 ipv6=off;
proxy_pass http://$cookie_origin:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
}
location /template-api {
rewrite ^/template-api/?(.*) /$1 break;
proxy_pass http://template-api:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
add_header Set-Cookie "origin=template-api;Domain=localhost;Path=/;Max-Age=100000";
}
location /api-plan {
rewrite ^/api-plan/?(.*) /$1 break;
proxy_pass http://api-plan:5000;
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;
proxy_set_header X-Forwarded-Host $server_name;
add_header Set-Cookie "origin=plan-api;Domain=localhost;Path=/;Max-Age=100000";
}
}

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.

I want to configure nginx server with multi applications (nodejs, django) in one domain

I have nodejs app (techkids.vn) that run on port 8080 and Django application running on port 8000 of the same server. Then I want to map the Django application on domain techkids.vn/djangoapp. This is my current configuration on Nginx (default.config), but its not work
Code:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
try_files $uri $uri/ =404;
}
location /djangoapp/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8000;
}

Removing start of path from nginx proxy_pass

To remove the use of ports on several of the applications running on this server, I've been using nginx's proxy_pass to do this. However, for some reason the actual url is being passed to the application. Is there a way so that it thinks /panel is really just /?
location /panel {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8082/;
}
You need to add the trailing slash
location /panel/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8082/;
}

Resources