2017-10-11 37 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;