在同一网络上的容器之间进行Dockerized Nginx反向代理吗?

时间:2019-09-23 15:06:06

标签: docker nginx networking containers reverse-proxy

我正在运行2个相同的docker堆栈,除了端口和root中的php文件(仅说明它是什么服务器)。堆栈具有nginx和php,它们可以使用“ site1:8080 / php.php”和“ site2:8090 / php.php”加载网站

这是我在主机上的/ etc / hosts文件

127.0.0.1 site1
127.0.0.1 site2

它们正在我的机器上运行(设置了单节点),因此我想在它们之间进行反向代理,因此用户只需输入“ site1”或“ site2”即可访问php.php文件。 这是“ site1”的docker compose文件:

version: '3'

services:
  web:
      image: nginx:latest
      deploy:
       replicas: 1
      ports:
          - "8080:80"
      volumes:
          - "./code:/code"
          - "./site.conf:/etc/nginx/conf.d/site.conf"
      links:
          - php
  php:
      image: php:7-fpm
      deploy:
        replicas: 1
      volumes:
          - "./code:/code"

,对于“ site2”,仅端口已更改为8090。

对于site.conf,它是:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    index index.php index.html;
    server_name site1;

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

我安装了用于反向代理的nginx,还创建了一个名为“ proxy”的docker网络,并向其中添加了3个nginx。

docker service create --name=reverse-proxy --publish=80:80/tcp nginx:latest

我进入了容器,并在启用了网站的站点中进行了所有这些配置,并且进行了很多改动,但没有任何效果:

#upstream site1 {
# server        site1:80;
#}

#server {
#  listen        80;
#  server_name   site1;

#  location / {
#    proxy_pass  http://site1:8080/php.php;
#  }
#}


#server {
#    listen 80;
#    server_name site1;

#    location / {
#        proxy_pass http://172.19.0.3:80/php.php;
#        proxy_set_header Host $host;
#        proxy_set_header X-Real-IP $remote_addr;
#        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#        proxy_set_header X-Forwarded-Proto $scheme;
#    }
#}


server {
listen 80;
listen [::]:80;

server_name site1;

location / {
proxy_pass http://172.19.0.3:80;
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
}
}

0 个答案:

没有答案