从“目录”反向代理到“服务器不工作

时间:2018-04-20 20:17:48

标签: docker nginx reverse-proxy

我有一个docker-compose文件,可以创建3个apache容器和一个NGINX容器。 apache容器都是具有自己的docker容器的不同应用程序。

我想使用NGINX将代理从根nginx容器URL的目录反转到3个apache实例的主父。

在下面的配置位置/工作。 / parts和/ wiki没有。

我已经确认所有3个apache容器都可以通过交换/ location上的proxy_pass来工作,所以我知道问题在于nginx配置。但是我是nginx的新手,我不知道如何解决这个问题。

worker_processes 1;

events { worker_connections 1024; }

http {

  sendfile on;

  upstream docker-apache-php {
      server ths_apache_php:80;
  }

  upstream docker-apache-php-two {
      server ths_apache_php_two:80;
  }

  upstream docker-apache-php-three {
      server ths_apache_php_three:80;
  }

  server {
    listen 8080;

    location / {
        proxy_pass         http://docker-apache-php;
        proxy_redirect     off;
        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-Host $server_name;
    }

    location /parts {
        proxy_pass         http://docker-apache-php-two;
        proxy_redirect     off;
        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-Host $server_name;
    }

    location /wiki {
        proxy_pass         http://docker-apache-php-three;
        proxy_redirect     off;
        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-Host $server_name;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,显然,proxy_pass上的URL需要以/结尾。现在这也正常,因为我也需要它。

worker_processes 1;

events { worker_connections 1024; }

http {

  sendfile on;

  upstream docker-apache-php {
      server ths_apache_php:80;
  }

  upstream docker-apache-php-two {
      server ths_apache_php_two:80;
  }

  upstream docker-apache-php-three {
      server ths_apache_php_three:80;
  }

  server {
    listen 8080;

    location / {
        proxy_pass         http://docker-apache-php/;
        proxy_redirect     off;
        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-Host $server_name;
    }

    location /parts {
        proxy_pass         http://docker-apache-php-two/;
        proxy_redirect     off;
        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-Host $server_name;
    }

    location /wiki {
        proxy_pass         http://docker-apache-php-three/;
        proxy_redirect     off;
        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-Host $server_name;
    }
  }
}