2017-04-07 81 views
0

我使用的是nginxdocker容器(https://github.com/jwilder/nginx-proxy)代理请求到其他搬运工容器中运行的apache实例存在代理。每个子域都有自己的apache docker容器。其中一个子域(sub1)需要一些特殊规则:nginx的try_files改写如果要是不

如果URL以“foo”开头,那么我希望nginx检查文件是否存在。如果该文件存在,那么它应该在URL重写访问控制脚本(imageControl.php),并把它传递到通常阿帕奇容器。如果该文件不存在,我想请求被代理到远程服务器。

我认为我正确设置好了,但由于某种原因nginx的总是将请求发送到远程服务器 - 即使该文件存在本地。

这里是nginx的配置文件从搬运工容器内的相关部分:

default.conf(包括来自nginx.conf):

upstream sub1.mydomain.com { 
      ## Can be connect with "dockerized_default" network 
      # dockerized_sub1 
      server 172.18.0.2:80; 
} 
server { 
    server_name sub1.mydomain.com; 
    listen 80 ; 
    access_log /var/log/nginx/access.log vhost; 
    include /etc/nginx/vhost.d/sub1.mydomain.com; 
    location/{ 
     proxy_pass http://sub1.mydomain.com; 
    } 
} 

的/ etc/nginx的/ vhost.d/sub1.mydomain.com:

location ^~ /foo/ { 
    root /path/to/dockerized/webroot; 
    try_files $uri @rewrite @remoteproxy; 
} 

location @remoteproxy { 
    include vhost.d/remoteproxy.inc; 
    proxy_pass http://remote.ip.address.here; 
} 

location @rewrite { 
    rewrite ^/(.*)$ /imageControl.php?file=$1 break; 
} 

请注意,我不想nginx的到t RY运行imageControl.php(我不使用php-fpm) - 我想nginx的将它传递给上游apache的容器。

期望的行为例如:

  1. 这部分的工作原理

  2. 这部分不工作

我会很感激一些帮助。

回答

0

您可以使用if -e表达有更多的运气:

location ^~ /foo/ { 
    root /path/to/dockerized/webroot; 
    if (-e $request_filename) { 
     rewrite ^/(.*)$ /imageControl.php?file=$1 last; 
    } 
    include vhost.d/remoteproxy.inc; 
    proxy_pass http://remote.ip.address.here; 
} 

更多见this document