簡體   English   中英

Nginx反向代理和路徑位置

[英]Nginx reverse proxy and path location

您好,我是Docker世界的新手,所以我嘗試了使用NGINX反向代理(jwilder映像)和Docker應用程序進行安裝。 為了方便起見,我都安裝了兩個都沒有SSL的情況。 由於Docker應用似乎安裝在根路徑中,因此我想將NGINX Web服務器和Docker應用分開。

upstream example.com {
        server 172.29.12.2:4040;
}
server {
 server_name example.com;
 listen 80 ;
 access_log /var/log/nginx/access.log vhost;

 location / {
  proxy_pass http://example.com;
  root /usr/share/nginx/html;
  index index.html index.htm;
  }
 location /app {
  proxy_pass http://example.com:4040;
  }
}

所以我想將http://example.com重定向到index.html,並將http://example.com/app重定向到docker應用。

此外,在構建安裝程序時,我在docker-composepose中使用:“ 4040”,因此當我使用nginx -s reload加載NGINX配置文件時,它警告我我的端口4040沒有打開。

使用我在任何路徑上方發布的配置文件,將我引導至docker應用。

我找不到我的問題的簡單解決方案。

據我所知,您的邏輯是正確的,docker被設計為對單個容器運行單個服務。 為了實現您的目標,您仍然需要注意一些事情,如果在您的Docker文件中聲明了EXPOSE 4040,那還不足以使服務可達。 在docker-compose文件中,您還必須聲明端口,即nginx的IE,您可以通過添加以下內容使主機系統在所有接口上進行監聽

...
ports:
  - 80:80
...

這是第一件事,您還必須考慮從同一節點上的容器網絡通過哪種方式使代理到達“應用程序”? 如果是,則可以添加作曲家文件:

...
depends_on:
  - app
...

其中app是在docker-compose文件中聲明的服務名稱,例如nginx能夠使用名稱app到達您的應用程序,因此重定向將指向app:

location /app {
    proxy_pass http://app:4040;
}

如果您想通過主機網絡訪問“應用程序”,可能是因為有一天將在另一台主機上運行,​​則可以在運行nginx的容器的主機文件中添加以下條目:

...
extra_hosts:
  - "app:10.10.10.10"
  - "appb:10.10.10.11"
...

等等

參考: https : //docs.docker.com/compose/compose-file/


編輯01/01/2019 !!!!! 新年快樂!!

使用“巨大”泊塢窗撰寫文件的示例:

version: '3'

services:
  app:
    build: "./app" # in case you docker file is in a app dir
    image: "some image name"
    restart: always
    command: "command to start your app"
  nginx:
    build: "./nginx" # in case you docker file is in a nginx dir
    image: "some image name"
    restart: always
    ports:
      - "80:80"
      - "443:443"
    depends_on:
     - app

在上面的示例中,nginx可以僅使用“ app”名稱訪問您的應用,因此重定向將指向http:// app:4040

systemctl(直接從docker開始-沒有撰寫)

[Unit]
Description=app dockerized service
Requires=docker.service
After=docker.service
[Service]
ExecStartPre=/usr/bin/sleep 1
ExecStartPre=/usr/bin/docker pull mariadb:10.4
ExecStart=/usr/bin/docker run --restart=always --name=app -p 4040:4040 python:3.6-alpine # or your own builded image
ExecStop=/usr/bin/docker stop app
ExecStopPost=/usr/bin/docker rm -f app
ExecReload=/usr/bin/docker restart app
[Install]
WantedBy=multi-user.target

像上面的示例一樣,您可以在系統主機上的端口4040上訪問應用程序(該端口正在偵聽所有接口在端口4040上的連接),以提供特定的接口:-p 10.10.10.10:4040:4040像這樣,它將監聽地址10.10.10.10上的端口4040(主機)

docker-compose with extra_host:

version: '3'

services:
  app:
    build: "./app" # in case you docker file is in a app dir
    image: "some image name"
    restart: always
    command: "command to start your app"
  nginx:
    build: "./nginx" # in case you docker file is in a nginx dir
    image: "some image name"
    restart: always
    ports:
      - "80:80"
      - "443:443"
    extra_hosts:
     - "app:10.10.10.10"

就像上面的示例一樣,nginx定義的服務可以在10.10.10.10到達名稱為app的名稱

至少但不是最后一次擴展撰寫文件上的服務:

泊塢窗,compose.yml:

version: '2.1'

services:
  app:
    extends:
      file: /path/to/app-service.yml
      service: app
  nginx:
    extends: /path/to/nginx-service.yml
    service: nginx

APP-service.yml:

version: "2.1"

service:
  app:
    build: "./app" # in case you docker file is in a app dir
    image: "some image name"
    restart: always
    command: "command to start your app"

nginx的-service.yml

version: "2.1"

service:
  nginx:
    build: "./nginx" # in case you docker file is in a nginx dir
    image: "some image name"
    restart: always
    ports:
      - "80:80"
      - "443:443"
    extra_hosts:
     - "app:10.10.10.10"

真的希望上面發布的例子足夠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM