Score:0

Proxy all requests to backend server

cn flag

I currently have an Angular app that gets its data from a Symfony backend. When developing, I used the ng serve development server proxy system for redirecting the /api, /oauth-token and /media routes to the backend server, and this worked fine.

Now that I want to deploy the app to a web server, I set up an Nginx server that serves the static files generated by the built Angular app. Since the development server isn't there to proxy the requests anymore, I have to do this through Nginx's conf files so that I have a functional reverse proxy.

What I simply need is that if I request something like GET http://localhost/api/products/1, this request gets proxied to GET http://localhost:81/api/products/1. This should be the same for all other request methods : POST, PUT, PATCH, etc.

The issue is that I can't seem to be able to write a conf file that suits my needs for this use case. The best I get is a 302 or 405 response.

Here's the conf file I currently have :

server {
    server_name localhost;
    root /var/www/webapp;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location ~ ^/(?:oauth-token|api|media)/ {
        proxy_pass http://localhost:81;
    }
}

The backend is hosted on the same server and instance of nginx than the frontend, but on a different port (port 81 instead of 80).

I tried separating my location block into 3 separate blocks for each route in case my regex or the proxy pass was wrong, but I get the same results :

    location /api/ {
        proxy_pass http://localhost:81/api/;
    }

    location /oauth-token/ {
        proxy_pass http://localhost:81/oauth-token/;
    }

I also attempted to remove the trailing slashes at the end of the proxy pass URL to see if that was the issue, to no avail.

I tried following the advices I found on other posts on Server Fault, but sadly none worked for me.

Can you help me configure this file so that these API requests get proxied to my backend server? Thanks.

Michael Hampton avatar
cz flag
Your _first_ config snippet is the good one and you should use it as a baseline for troubleshooting, and discard the other config. That said, you need to specify exactly what errors you received with it. Reproduce the problem again and then look in the nginx error log.
cn flag
The problem is that the error log tells nothing. I only have the `gracefully shutting down` messages I get when restarting my nginx docker so my conf files get reloaded, that's the reason why I'm pretty stumped. The access logs simply show the same 302 and 405 response codes I wrote about before.
Michael Hampton avatar
cz flag
Then the problem is with your application, and you need to look there.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.