2

I am using an nginx docker container (https://github.com/jwilder/nginx-proxy) to proxy requests to instances of apache running in other docker containers. Each subdomain gets its own apache docker container. One of these subdomains (sub1) requires some special rules:

If the URL begins with "foo" then I would like nginx to check if the file exists. If the file exists, then it should rewrite the URL to an access control script (imageControl.php), and pass it to the usual apache container. If the file does not exist then I want the request to be proxied to a remote server.

I thought I set things up correctly, but for some reason nginx is always sending the request to the remote server - even if the file exists locally.

Here is the relevant portion of the nginx config file from within the docker container:

default.conf (included from 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;
}

Please note that I do not want nginx to try to run imageControl.php (I am not using php-fpm) - I want nginx to pass it to the upstream apache container.

Desired behavior example:

  1. (this part works)

  2. (this part does not work)

I'd appreciate some help.

Mike Furlender
  • 3,869
  • 5
  • 47
  • 75

1 Answers1

2

You may have more luck using an if -e expression:

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

See this document for more.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81