Welcome! Log In Create A New Profile

Advanced

Nginx conditional proxy pass

Posted by chrisa 
Nginx conditional proxy pass
February 01, 2021 01:48AM
I have 2 different ( Node & Angular ) applications running under a domain. I am trying to configure nginx in way to serve like, whenever anyone access "domain.com" it should serve the node app running on port 4200 and if someone access "domain.com/admin" it should serve the contents from Angular app. Below is my nginx configuration.

server {
listen 80;
listen [::]:80;

server_name domain.com

}


server {
listen 443 default_server ssl;
ssl on;
server_name domain.com;
root /home/domain/public_html/angular/dist;
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


location / {
proxy_pass http://127.0.0.1:4200;
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 /admin {
try_files $uri /index.html;
}
}
The problem I am facing now is all the requests including /admin is getting proxy passed. Is there any way I can restrict the /admin requests to not get proxy passed? Below is the nginx log.

connect() failed (111: Connection refused) while connecting to upstream, client: x.x.x.x, server: domain.com, request: "GET /admin HTTP/1.1", upstream: "http://127.0.0.1:4200/admin", host: "domain.com"
Can someone please shed some light to get this resolved?
Re: Nginx conditional proxy pass
February 01, 2021 04:16AM
Swap the Location blocks around.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Nginx conditional proxy pass
February 01, 2021 04:34AM
Thank you for the suggestion. I've tried changing the priority of blocks by moving the "/admin" block to up but no luck. Still getting the same error. It's trying to connect with the port number.

[error] 18852#18852: *1 connect() failed (111: Connection refused) while connecting to upstream, client: x.x.x.x, server: domain.com, request: "GET /admin/login HTTP/1.1", upstream: "http://127.0.0.1:4200/index.html",
Re: Nginx conditional proxy pass
February 01, 2021 08:07AM
Have a look in the backend logs as 'Connection refused' is a thing from the backend.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Nginx conditional proxy pass
February 01, 2021 08:18AM
Hi,

What I am trying to achieve is whenever someone access domain.com/admin it should load " "http://127.0.0.1/index.html" instead of "http://127.0.0.1:4200/index.html". Port 4200 is to serve from the Node application.
Re: Nginx conditional proxy pass
February 01, 2021 09:43AM
What about;
location /admin/ {

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Nginx conditional proxy pass
February 01, 2021 11:13AM
No luck. Still the same error.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 148
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready