NGINX反向代理不工作的.NET核心的WebAPI在泊坞运行(NGINX reverse prox

2019-09-28 18:23发布

我在.NET的核心一个简单的例子的WebAPI,在泊坞窗容器中运行。 我运行Nginx的也是一个泊坞窗容器为HTTPS重定向的反向代理。 该的WebAPI是HTTP访问,但访问HTTPS URL时,该API没有响应。

我在nginx.conf文件中尝试了许多不同的配置。 我使用本地主机,0.0.0.0,127.0.0.1尝试。 我已经使用如5000,8000,和80。我已经使用上游和也直接指定对proxy_pass线链接尝试了几种不同的端口尝试。

泊坞窗,compose.yml:

version: '3.4'

networks:
  blogapi-dev:
    driver: bridge

services:
  blogapi:
    image: blogapi:latest
    depends_on:
      - "postgres_image"
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:80"
    expose:
      - "8000"
    environment:
      DB_CONNECTION_STRING: "host=postgres_image;port=5432;database=blogdb;username=bloguser;password=bloguser"
      ASPNETCORE_ENVIRONMENT: development
      #REMOTE_DEBUGGING: ${REMOTE_DEBUGGING}
    networks:
      - blogapi-dev
    tty: true
    stdin_open: true

  postgres_image:
    image: postgres:latest
    ports:
      - "5000:80"
    restart: always
    volumes:
      - db_volume:/var/lib/postgresql/data
      - ./BlogApi/dbscripts/seed.sql:/docker-entrypoint-initdb.d/seed.sql
    environment:
      POSTGRES_USER: "bloguser"
      POSTGRES_PASSWORD: "bloguser"
      POSTGRES_DB: blogdb
    networks:
      - blogapi-dev

  nginx-proxy:
    image: nginx:latest
    container_name: nginx-proxy
    ports:
      - 80:80
      - 443:443
    networks:
      - blogapi-dev
    depends_on:
      - "blogapi"
    volumes:
      - ./nginx-proxy/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx-proxy/error.log:/etc/nginx/error_log.log
      - ./nginx-proxy/cache/:/etc/nginx/cache
      - /etc/letsencrypt/:/etc/letsencrypt/
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./:/etc/nginx/

networks:
  blogapi-dev:
    driver: bridge

volumes:
  db_volume:

nginx.conf:

events {}
http {
    upstream backend {
        server 127.0.0.1:8000;
    }
    server {
        server_name local.website.dev;
        rewrite ^(.*) https://local.website.dev$1 permanent;
    }
    server {
        listen 443 ssl;
        ssl_certificate      localhost.crt;
        ssl_certificate_key  localhost.key;
        ssl_ciphers          HIGH:!aNULL:!MD5;
        server_name          local.website.dev;
        location / {
            proxy_pass  http://backend;
        }
    }
}

Startup.cs:

namespace BlogApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");
                    services.AddDbContext<BlogContext>(options =>
                    options.UseNpgsql(
                    connectionString));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
    }

当我去http://127.0.0.1:8000/api/blog ,浏览器返回从我的API JSON响应。 这告诉我,在App美国并在端口8000上运行,但它不应该通过HTTP accessable:


[{"id":1,"title":"Title 1","body":"Body 1","timeStamp":"1999-01-08T04:05:06"},{"id":2,"title":"Title 2","body":"Body 2","timeStamp":"2000-01-08T04:05:06"},{"id":3,"title":"Title 3","body":"Body 3","timeStamp":"2001-01-08T04:05:06"},{"id":4,"title":"Title 4","body":"Body 4","timeStamp":"2002-01-08T04:05:06"}]

当我去到127.0.0.1,在浏览器中正确重定向到https://local.website.dev/ ,但我没有得到任何来自API响应,只是local.website.dev拒绝连接的镶边。 ERR_CONNECTION_REFUSED。 我得到的时候去同样的反应https://local.website.dev/api/blog 。

此外,这是从输出搬运工-撰写了:

Starting blog_postgres_image_1 ... done
Starting blog_blogapi_1        ... done
Starting nginx-proxy           ... done
Attaching to blog_postgres_image_1, blog_blogapi_1, nginx-proxy
blogapi_1         | Hosting environment: development
blogapi_1         | Content root path: /app
blogapi_1         | Now listening on: http://[::]:80
blogapi_1         | Application started. Press Ctrl+C to shut down.
postgres_image_1  | 2019-06-27 11:20:49.441 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_image_1  | 2019-06-27 11:20:49.441 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_image_1  | 2019-06-27 11:20:49.577 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_image_1  | 2019-06-27 11:20:49.826 UTC [25] LOG:  database system was shut down at 2019-06-27 10:26:12 UTC
postgres_image_1  | 2019-06-27 11:20:49.893 UTC [1] LOG:  database system is ready to accept connections

Answer 1:

我得到它的工作。 有一对夫妇的问题。 首先,我是缺少在nginx.conf文件的顶部一些样板。 其次,我需要的proxy_pass设置为泊坞窗容器外壳的名字,我想的路线,在我的情况下,服务的http:// blogapi / ,而不是本地主机。

nginx.conf

worker_processes 1;

events {
    worker_connections 1024;
}

http {
    proxy_set_header Host $host;
    proxy_pass_request_headers on;

    gzip on;
    gzip_proxied any;

    map $sent_http_content_type $expires {
        default off;
        ~image/ 1M;
    }

    server {
        listen 80;
        listen [::]:80;
        server_name localhost; 
        return 301 https://172.24.0.1$request_uri;
    }

    server {
        listen 443 ssl;
        server_name localhost;
        ssl_certificate      localhost.crt;
        ssl_certificate_key  localhost.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!MD5;
        location / {
            proxy_pass http://blogapi/;
        }
    }
}

:根据上面的配置,我可以访问的WebAPI https://172.24.0.1/api/blog/如果HTTP://本地主机/ API /博客进入,浏览器会重定向到https://172.24.0.1/ API /博客/ IP地址为如下所示的blogapi-dev的桥网络网关的地址。

搬运工检查20B

    "Networks": {
        "blog_blogapi-dev": {
            "IPAMConfig": null,
            "Links": null,
            "Aliases": [
                "20bd90d1a80a",
                "blogapi"
            ],
            "NetworkID": "1edd39000ac3545f9a738a5df33b4ea90e082a2be86752e7aa6cd9744b72d6f0",
            "EndpointID": "9201d8a1131a4179c7e0198701db2835e3a15f4cbfdac2a4a4af18804573fea9",
            "Gateway": "172.24.0.1",
            "IPAddress": "172.24.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "02:42:ac:18:00:03",
            "DriverOpts": null
        }
    }


文章来源: NGINX reverse proxy not working for .NET core webAPI running in Docker