docker运行nginx作为另一个Web服务器的代理(在pi上) [英] docker running nginx as proxy to another webserver (on pi)

查看:65
本文介绍了docker运行nginx作为另一个Web服务器的代理(在pi上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在的小项目让我有些烦恼. (尽管不应该那么难)

I am kind of in over my head with my current small project. (although it should not be that hard)

我正尝试在我的Pi上使用docker运行docker的多个网页(出于测试目的),但应该都可以使用PI的IP来访问所有网页.

I am trying to run multiple webpages using docker on my Pi (for testing purposes) wich should all be reachable using the PI's IP.

我目前运行的是minimL LIGHTTPD :(基于树脂/rpi-raspbian图像)

I currently run a minimL LIGHTTPD: (based on the resin/rpi-raspbian image)

docker run -d -v <testconfig>:/etc/lighttpd -p <pi-ip>:8080:80 <image name>

(可以使用pi和网络中其他计算机上的浏览器访问此服务器)

(this server is reachable using the browser on pi and on other computers in the network)

对于nginx,我使用一个简单的配置运行另一个容器 (从 http://nginx.org/en/docs/beginners_guide.html 开始) , 包含用于测试容器配置的网页和图像. 可以使用:80

For nginx i run another container with with a simple config (starting with http://nginx.org/en/docs/beginners_guide.html), containing a webpage and images to test the container config. this container is reachable using :80

然后我尝试将代理添加到以下位置: (我玩过,所以现在有3个位置可以进行相同的重定向)

then i tried to add a proxy to the locations: (i played around so now there are 3 locations for the same redirect)

location /prox1/{
  proxy_pass http://<pi-ip>:8080
}
location /prox2/{
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_pass http://<pi-ip>:8080
}
location /prox3/{
  fastcgi_pass <pi-ip>:8080;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param QUERY_STRING $query_string
}

版本1和版本2提供了404(我尝试添加重写,但是由于/prox1/被剪切,我nginx对其自身进行了重定向). 版本3会产生超时.

Version 1&2 give a 404 (i triedadding a rewrite, but then i ´nginx redirected on itself due to the /prox1/ being cut). Version 3 yields a timeout.

现在,我不确定是否仍需要在nginx端进行挖掘,还是必须在容器之间的docker端添加连接.

Now i am not sure if i still have to dig on the nginx side, or i have to add a connection on the docker side between the containers.

PS:Pi正在运行ArchForArm(使用Xfce作为桌面),因为我无法在树莓派存储库中找到docker-compose.

PS: the Pi is running ArchForArm (using Xfce as desctop) because i couldent find docker-compose in the raspberian repository.

-编辑---: 我目前正在手动启动所有内容. (因此没有撰写文件)

-- EDIT ---: I currently start everything manually. (so no compose file)

LIGHTTPD以以下内容开头:

the LIGHTTPD is started with:

docker run -d --name mylighttpd -v <testconfig>:/etc/lighttpd -p <pi-ip>:8080:80 <image name>

如果我正确理解它,它现在正在侦听本地网络(在范围内)端口8080,该端口表示测试Web服务器端口80.(我添加了..name,因此更容易停止它. )

if i understood it correctly it is now listening on the local network (in the range of ) port 8080, which represents the test web-servers port 80. (i have added ..name so it is easier to stop it.)

nginx的启动方式如下:

the nginx is started like:

docker run --name mynginx --rm -p <pi-ip>:80:80 -v <config>:/data <image name>

8080已添加到Docker文件的公开中.

The 8080 was added in the expose in the Docker file.

我目前认为我误解了同一台计算机上两个客户端的连接,并且应该添加一个虚拟网络,我目前正在尝试在那里找到一些扩展坞.

I current think i misunderstood the connection for two clients on the same machine, and schould add a Virtual network, i am currently trying to find some docks there.

PS:我没有从存储库中使用已经存在的nginx-zeroconf,因为它告诉我它无法读取已安装的docker版本. (并且使用composer的唯一示例还需要另一个容器,该容器对于我的体系结构似乎不可用.)

PS: i am not using the already exsisting nginx-zeroconf from the repo because it tells me it cant read the installed docker version. (and the only example for using that with composer also needs another container which seems unavailable for my architecture.)

-编辑2-: 对于简单的proxy_pass,问题可能是网址. 我在"www"文件夹中添加了一个更深的文件夹"prox1",其中包含一个索引文件,当我请求该页面时,该文件夹就被销毁了.

-- edit2 --: For the simple proxy_pass the problem could be the url. I added a deeper folder "prox1" in the "www" folder, containing an index file, and that one is schown when i ask for the page.

好像是:80/prox1/ 重定向到:8080/prox1/ 但是,如果我尝试重写它(在"location/prox1/"内部),似乎首先要删除prox1,然后确定它现在是原始位置的一部分. :80/

It seems like :80/prox1/ is redirected to :8080/prox1/ but if i try rewrite it (inside "location /prox1/") it seems to first delete the prox1, and then decides it now is part of the original location. :80/

PS:我知道将系统放置在另一个连接中而不是网桥"并且只公开代理可能是一个更好的设计,但是我试图通过一些简单的步骤来学习这些知识.

PS: I am aware that it might be a better design to place the system inside another connection than "bridge" and only expose the proxy, but i am trying to learn this stuff in small steps.

-编辑3-: 现在尝试撰写,但似乎我遇到了我不明白的另一部分(为什么我要先不撰写就让它工作).

-- edit3 --: Trying compose now, but it seems i have encounters another part i dot understand (why i wanted to get it work without compose first).

我尝试遵循 http://docs .master.dockerproject.org/compose/compose-file/#ipv4-address-ipv6-address

networks:
  backbone:
    driver: bridge
    ipam:
      driver: default
      config:
       - subnet: 172.16.238.0/16
         gateway: 172.16.238.1
services:
  nginx:
    image: <nginx-image>
    ports: 80:80
    volumes:
     - <config>:/data
    depends_on:
     - lighttpd
    networks:
      backbone:
        ipv4_address: 172.16.238.2
 lighttpd:
    image: <lighttpd-image>
    ports: 8080:80
    volumes:
     - <testconfig>:/etc/lighttpd
    networks:
      backbone:
        ipv4_address: 172.16.238.3

现在,我必须弄清为什么我得到仅当连接到具有用户配置的子网的网络时才支持用户特定的IP地址"的原因,我假设主网络模块创建了一个称为骨干"的网络.

Now i have to find out why i get "User specific IP adress is supported only when connecting to networks with user configured subnets", i asume the main networks block creates a network called "backbone".

-编辑4-: 似乎ip块的写法必须与我所见过的所有扩展坞都不同,正确的格式是:

-- edit4 --: It seems ip blocks have to be written different to all the docks i have seen, the correct form is:

...
    networks:
      backbone:
        ipv4_address: 172.16.0.2/16
...

现在我必须弄清楚如何删除网址的一部分,我很好.

now i have to figure out how to drop the part of the url, and i am good to go.

推荐答案

核心问题似乎是缺少nginx参数 proxy_redirect ,我发现在码头上乱逛,当前的nginx.conf是: (/data/www包含index.html,其中包含指向/data/images中某些图像的相对链接)

The core problem seems to have been missing nginx parameter proxy_redirect, i found rambling trough the docks, the current nginx.conf is: (/data/www contains a index.html with a relative link to some images in /data/images)

worker_processes auto;
events {
  worker_connections 1024;
}
http {
  server {
    listen 80;
    location / {
      root /data/www;
    }
    location /images/ {
      root /data;
    }
    location /prox0/{
      proxy_pass http://lighttpd:80;
      proxy_redirect default;
      proxy_buffering off;
    }
  }
}

手动在本地Ip上启动似乎有效,但是docker-compose更容易: (如果未使用compose,请用用于启动服务器的ip&端口替换 lighttpd:80 .)

manual starting on local Ip seems to work, but docker-compose is easyer: (if compose is not used replace lighttpd:80 with the ip & port used for starting the server.)

networks:
  backbone:
    driver: bridge
    ipam:
      driver: default
      config:
       - subnet: 172.16.238.0/16
         gateway: 172.16.238.1
services:
  nginx:
    image: <nginx-image>
    ports: 80:80
    volumes:
     - <config>:/data
    depends_on:
     - lighttpd
    networks:
      backbone:
        ipv4_address: 172.16.0.2
 lighttpd:
    image: <lighttpd-image>
    ports: 8080:80
    volumes:
     - <testconfig>:/etc/lighttpd
    networks:
      backbone:
        ipv4_address: 172.16.0.3

这篇关于docker运行nginx作为另一个Web服务器的代理(在pi上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆