nginx try_files rewrite if exists proxy if notes';T

nginx try_files rewrite if exists proxy if notes';T,nginx,reverse-proxy,jwilder-nginx-proxy,Nginx,Reverse Proxy,Jwilder Nginx Proxy,我正在使用一个nginxdocker容器()将请求代理到在其他docker容器中运行的apache实例。每个子域都有自己的apache docker容器。其中一个子域(sub1)需要一些特殊规则: 如果URL以“foo”开头,那么我希望nginx检查文件是否存在。如果文件存在,那么它应该将URL重写为访问控制脚本(imageControl.php),并将其传递给常用的apache容器。如果文件不存在,那么我希望将请求代理到远程服务器 我认为我的设置是正确的,但出于某种原因,nginx总是将请求发

我正在使用一个
nginx
docker
容器()将请求代理到在其他docker容器中运行的
apache
实例。每个子域都有自己的apache docker容器。其中一个子域(sub1)需要一些特殊规则:

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

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

以下是docker容器中nginx配置文件的相关部分:

default.conf(包含在nginx.conf中):

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

请注意,我不希望nginx尝试运行imageControl.php(我没有使用
php fpm
)-我希望nginx将其传递给上游apache容器

期望的行为示例:

  • (本部分有效)

    • 请求是
    • /path/to/dockerized/webroot/foo/bar.png不存在,因此请求被代理到远程ip
  • (本部分不起作用)

    • 请求是
    • /path/to/dockerized/webroot/foo/baz.png存在,因此请求被重写为
    • 请求被发送到上游apache容器

  • 非常感谢您的帮助。

    如果使用
    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;
    }
    
    更多信息,请参阅

    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;
    }
    
    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;
    }