For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

BigJay's avatar
BigJay
Icon for Nimbostratus rankNimbostratus
Dec 20, 2023

Nginx is only redirecting to port 8080

I have a .net 8 solution multiple APIs and I'm using docker and Nginx to host the application. please find below the full details:

Dockerfile

 

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 8080


FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
...

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "xxx.Api/xxx.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "xxx.Api.dll"]

 

launchsettings.json

 

"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"publishAllPorts": true,
"useSSL": true,
"sslPort": 4430,
"httpPort": 8080
}

 

nginx.conf

 

worker_processes auto;

events {
worker_connections 1024;
}

http{

server {
listen 80;
server_name domain;
port_in_redirect off;

location /api1 {
rewrite /api1(.*) $1 break;
proxy_pass http://api1:8080;
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 /api2 {
rewrite /api2(.*) $1 break;
proxy_pass http://api2:8081;
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;
}
}

 

docker-compose

 

version: '3.4'

services:

nginx:
image: nginx
ports:
- 80:80
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- api1
- api2


api1:
image: ${DOCKER_REGISTRY-}api1:latest
container_name: api1
build:
context: .
dockerfile: api1.Api/Dockerfile
ports:
- "8080:8080"


api2:
image: ${DOCKER_REGISTRY-}api2:latest
container_name: api2
build:
context: .
dockerfile: api2.API/Dockerfile
ports:
- "8081:8081"

 

API1 that uses port 8080 loads normally but API2 that uses 8081 get error 502 gateway error.
If I switch the port on those same projects than API2 loads normally and API1 stops loading.
I've been trying all kinds of stuff over last 2 days and nothing seems to work.
Those same projects with the same configuration were working perfectly when I was using .net 6 with the same nginx version, but when I upgraded the project to .net 8 it broke.
I need your help and suggestions. Anything will be helpfull.

2 Replies