如何在kubernetes中部署.net和Nginx?

时间:2019-12-12 13:50:22

标签: asp.net .net docker nginx

我已经在docker中部署了.net + nginx。 当我尝试转到http://myhost时,我看到了 nginx default page,而不是my project page on .net

我该如何解决?

docker-compose.yaml

version: '3'
services:
  app:
    build: ./app
    ports:
      - "80:80"

  nginx:
    build: ./nginx
    ports:
      - "8080:8080"
    links:
      - app

.net Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS final
WORKDIR /app
COPY . .
EXPOSE 5000
ENTRYPOINT ["dotnet", "WebApplication4.dll"]

nginx

FROM nginx
COPY nginx.conf /etc/nginx/

nginx.conf

worker_processes 4;

events { worker_connections 1024; }

http {
  # Nginx will handle gzip compression of responses from the app server
  gzip on;
  gzip_proxied any;
  gzip_types text/plain application/json;
  gzip_min_length 1000;

  server {
    listen 80;

    # Nginx will reject anything not matching /api
    location /api {
      # Reject requests with unsupported HTTP method
      if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE)$) {
        return 405;
      }

      # Only requests matching the whitelist expectations will
      # get sent to the application server
      proxy_pass http://172.28.0.3:5000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_cache_bypass $http_upgrade;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

感谢evreybody。我解决了我的问题。在docker-compose中需要添加network_mode:'host'

nginx:

network_mode:“主机”