2017-05-29 381 views
0

我在嘗試讓Nginx代理另一個也在Docker中運行的服務器的路徑時遇到問題。Docker + Nginx:讓proxy_pass工作

爲了說明,我以Nexus服務器爲例。

這是我第一次嘗試......

docker-compose.yml: -

version: '2' 
services: 
    nexus: 
    image: "sonatype/nexus3" 
    ports: 
    - "8081:8081" 
    volumes: 
    - ./nexus:/nexus-data 

    nginx: 
    image: "nginx" 
    ports: 
    - "80:80" 
    volumes: 
    - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro 

nginx.conf: -

worker_processes 4; 
events { worker_connections 1024; } 
http { 
     server { 
       listen 80; 

       location /nexus/ { 
       proxy_pass http://localhost:8081/; 
       } 
     } 
} 

當我打http://localhost/nexus/,我得到502錯誤網關支持以下日誌: -

nginx_1 | 2017/05/29 02:20:50 [error] 7#7: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /nexus/ HTTP/1.1", upstream: "http://[::1]:8081/", host: "localhost" 
nginx_1 | 2017/05/29 02:20:50 [error] 7#7: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /nexus/ HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "localhost" 
nginx_1 | 172.18.0.1 - - [29/May/2017:02:20:50 +0000] "GET /nexus/ HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" 

在我的第二次嘗試......,

docker-compose.yml - 我加links到Nginx的配置: -

version: '2' 
services: 
    nexus: 
    image: "sonatype/nexus3" 
    ports: 
    - "8081:8081" 
    volumes: 
    - ./nexus:/nexus-data 

    nginx: 
    image: "nginx" 
    ports: 
    - "80:80" 
    volumes: 
    - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro 
    links: 
    - nexus:nexus 

nginx.conf ...而不是使用http://localhost:8081/的,我用http://nexus:8081/: -

worker_processes 4; 
events { worker_connections 1024; } 
http { 
     server { 
       listen 80; 

       location /nexus/ { 
       proxy_pass http://nexus:8081/; 
       } 
     } 
} 

現在,當我點擊http://localhost/nexus/時,它會正確代理,但Web內容已部分呈現。當檢查該頁面的HTML源代碼時,javascript,樣式表和圖像鏈接指向http://nexus:8081/[path] ...因此,404。

我該如何改變以使其正常工作?

非常感謝。

回答

1

以下的附加選項是什麼我已經使用

http { 
    server { 
      listen 80; 

      location /{ 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header Host $http_host; 
      server_name_in_redirect on; 
      proxy_pass  http://nexus:8081; 

      } 

      location /nexus/ { 
      proxy_pass   http://nexus:8081/; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header Host $http_host; 
      server_name_in_redirect on; 
      } 
    } 

}

我的解決方案,包括在nginx的配置的「/」路徑重定向。 Nexus應用程序將向「/」發出無法工作的資源請求。

但是,這並不理想,並且不適用於服務於多個應用程序的Nginx配置。

docs 覆蓋此配置並表明您需要配置Nexus以便在/nexus上提供服務。這將使您能夠按如下方式配置Nginx(從文檔)減去上面的黑客。

location /nexus { 
    proxy_pass http://localhost:8081/nexus; 
    proxy_set_header Host $host; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
} 

我會推薦使用該配置。

+0

它不適合我......'http:// localhost/nexus'重定向到'http:// nexus',我在那個頁面上得到了404。 – limc

+0

這會發生,因爲您沒有位置指令需要匹配的尾部「/」。這本身並不能解決問題。我正在用解決方案更新答案。 –

+0

將Nexus配置爲具有自定義上下文路徑'/ nexus'對我來說是個訣竅。然後,我配置proxy_pass指向'http:// nexus:8081/nexus /'。非常感謝您的幫助。 – limc