2017-10-11 40 views
0

遇到麻烦试图让下面的泊坞Nginx的反向代理到.NET核心API在泊坞窗

工作,我想要的是,当用户请求http://localhost/api然后NGINX反向代理我对.NET核心在另一个容器中运行的API。

集装箱主机:视窗

容器1:NGINX

dockerfile

FROM nginx 

COPY ./nginx.conf /etc/nginx/nginx.conf 

nginx.conf

user nginx; 
worker_processes 1; 

error_log /var/log/nginx/error.log warn; 
pid  /var/run/nginx.pid; 

events { 
    worker_connections 1024; 
} 

http { 

    server { 
     location /api1 { 
      proxy_pass http://api; 
      proxy_http_version 1.1; 
      proxy_set_header Upgrade $http_upgrade; 
      proxy_set_header Connection keep-alive; 
      proxy_set_header Host $host; 
      proxy_cache_bypass $http_upgrade; 
     } 
    } 

    include  /etc/nginx/mime.types; 
    default_type application/octet-stream; 

    log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
         '$status $body_bytes_sent "$http_referer" ' 
         '"$http_user_agent" "$http_x_forwarded_for"'; 

    access_log /var/log/nginx/access.log main; 

    sendfile  on; 
    #tcp_nopush  on; 

    keepalive_timeout 65; 

    #gzip on; 

    include /etc/nginx/conf.d/*.conf; 
} 

容器2:净Ç矿石API

死简单 - 在所述容器暴露在端口80 API

然后是搬运工-compose.yml

搬运工-compose.yml

version: '3' 

services: 
    api1: 
    image: api1 
    build: 
     context: ./Api1 
     dockerfile: Dockerfile 
    ports: 
     - "5010:80" 

    nginx: 
    image: vc-nginx 
    build: 
     context: ./infra/nginx 
     dockerfile: Dockerfile 
    ports: 
     - "5000:80" 

读它指出的Docker文档:

链接允许您定义额外的别名,通过该别名可以从另一个服务访问服务 。它们不需要启用 服务进行通信 - 默认情况下,任何服务都可以以该服务的名称到达任何其他 服务。

所以我的API调用服务api1,我只是在nginx.conf文件中引用此作为反向代理配置的一部分:

proxy_pass http://api1;

有些事情不对,当我进入http:\\localhost\api为我收到一个404错误。

有没有办法解决这个问题?

回答

0

问题是nginx location配置。

404错误是正确的,因为您的配置代理从http://localhost/api/some-resource请求到缺少的资源,因为您的映射是为/api1路径,而您要求/api

所以你应该只改变位置/api它会工作。

请记住,对http://localhost/api的请求将代理到http://api1/api(路径保留)。如果您的后端配置为使用前缀路径公开api,则无此问题,否则您将收到另一个404(这次来自您的服务)。 为了避免这种情况,您应该在代理请求之前用如下规则重写路径:

# transform /api/some-resource/1 to /some-resource/1 
rewrite /api/(.*) /$1 break;