0

I'm currently trying to host both an angular app and a reverse proxy to a node backend. Using Lets Encrypt, I've been able to set up the Angular app without any problems. However, I'm lost on how to configure Nginx to also act as a reverse proxy to my node app running on a specific port. I find examples of reverse proxies for nginx, but nothing that incorporates both.

Here is my config that is working with angular:

server {

    server_name example.com;

    root /var/www/example.com;
    index index.html index.htm;

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot

    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.example.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

}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;
    return 404; # managed by Certbot
}

What I'm trying to add:

server{
    server_name api.example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

I've tried several combinations of server blocks and location blocks, but I simply get a 404 when I go to api.example.com.

1 Answer 1

1

I usually solve that kind of issue like this. Endpoints of all back-end APIs are started with "v1". I serve the angular project using pm2.

location / {
   proxy_pass http://localhost:3000;
   ...
}
location ~ ^/(v1)/ {
   proxy_pass http://localhost:3001;
...
}

Sign up to request clarification or add additional context in comments.

3 Comments

Which server block would that go in?
right above listen [::]:443 ssl ipv6only=on; # managed by Certbot
or one of the best thing is to serve angular front-end using node js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.