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.