Reputation: 1053
I have a docker compose file comprising of two microservices that I want to leverage URI routing for in order to handle CORS. Here is my compose file:
version: "3.1"
services:
auth-api:
image: xxxx/auth-api:latest
restart: always
depends_on:
- "user-api"
user-api:
image: xxxx/user-api:latest
restart: always
nginx:
image: nginx
restart: always
ports:
- "80:80"
links:
- "auth-api"
- "user-api"
volumes:
- ./nginx:/etc/nginx
I want to use NGINX for the routing but am running into issues with 404's or 405's when trying to access the resources. I've tried several different configurations and for a while NGINX was saying it couldn't find the api endpoints when it started which I resolved so I think my issue is mostly around routing configuration. I want it to be /auth
and /user
for those requests.
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream auth-target {
server auth-api:8080;
}
upstream user-target {
server user-api:8080;
}
server {
listen 80;
location /auth {
proxy_pass http://auth-target;
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 /user {
proxy_pass http://user-target;
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;
}
}
}
I'm pretty new to NGINX so I'm not sure if what I'm doing is right. I should also note that both APIs have 8080 exposed as part of the container build.
Upvotes: 2
Views: 705
Reputation: 1053
So here was my issue. The routing was working but I was returning 404's from my services. This was because the Location route was being passed along with the request. So in the end I found this answer:
https://serverfault.com/questions/562756/how-to-remove-the-path-with-an-nginx-proxy-pass
Where it points that you need a trailing slash in order to remove the location so my config file ended up looking something like this:
location /auth/ {
proxy_pass http://auth-target/;
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;
}
Upvotes: 1