I have a docker environment with 3 containers: frontend (angular), backend (dotnet) and nginx.
I'm trying to configure nginx with a proxy_pass to direct the /api location to my API from one of the containers.
This is my nginx configuration:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://sitr-app:80;
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;
}
location /api {
proxy_pass http://sitr-api:80;
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;
}
}
These are my Dockerfiles:
API dotnet:
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY . .
EXPOSE 80
ENTRYPOINT ["dotnet", "SITR.Web.Host.dll", "--environment=Staging"]
Frontend angular:
FROM nginx
COPY . /usr/share/nginx/html
COPY default.conf /etc/nginx/conf.d
EXPOSE 80
nginx
FROM nginx
COPY default_nginx.conf /etc/nginx/conf.d/default.conf
My docker-compose file
version: '3.0'
services:
sitr-api:
image: sitr-api
container_name: sitr-api
environment:
ASPNETCORE_ENVIRONMENT: Staging
ports:
- "9901:80"
volumes:
- "./Host-Logs:/app/App_Data/Logs"
sitr-app:
image: sitr-app
container_name: sitr-app
ports:
- "9902:80"
nginx:
image: sitr-nginx
container_name: sitr-nginx
depends_on:
- sitr-app
- sitr-api
ports:
- "81:80"
The containers are working because I was able to access localhost:9901 (backend) and localhost:9902 (frontend).
My frontend accessing through nginx on localhost:81 is also working, but the proxy_pass to my localhost:81/api backend is not working (http 404).
What is wrong with my nginx configuration?
CodePudding user response:
I'm posting response based on @super's comment. According to his comment, it was only necessary to do a rewrite to remove /api when he redirects.
The specific line I needed to add with the regex to remove /api is this:
rewrite ^/api(/.*)$ $1 break;
This is the complete nginx configuration:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://sitr-app:80;
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;
}
location /api {
rewrite ^/api(/.*)$ $1 break;
proxy_pass http://sitr-api:80;
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;
}
}