繁体   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