Nginx只重定向到端口8080

dw1jzc5e  于 5个月前  发布在  Nginx
关注(0)|答案(1)|浏览(47)

我有一个.net 8解决方案多个API,我使用Docker和Nginx来托管应用程序。请在下面找到完整的详细信息:
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"


使用端口8080的API 1正常加载,但使用8081的API 2得到错误502网关错误。如果我在这些相同的项目上切换端口,则API 2正常加载,API 1停止加载。我在过去的2天里尝试了各种各样的东西,似乎都没有工作。当我使用.net 6和相同的nginx时,具有相同配置的相同项目可以完美地工作版本,但当我升级到项目。net 8它打破了。我需要你的帮助和建议。任何将是有益的。

mepcadol

mepcadol1#

我们的服务在一个环境中工作。Nginx解决app1和app2直接需要使用直接端口而不是外部端口。

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

字符串

相关问题