nginx try_files rewrite if exists proxy if doesn't -
i using nginx docker container (https://github.com/jwilder/nginx-proxy) proxy requests instances of apache running in other docker containers. each subdomain gets own apache docker container. 1 of these subdomains (sub1) requires special rules:
if url begins "foo" nginx check if file exists. if file exists, should rewrite url access control script (imagecontrol.php), , pass usual apache container. if file not exist want request proxied remote server.
i thought set things correctly, reason nginx sending request remote server - if file exists locally.
here relevant portion of nginx config file within docker container:
default.conf (included nginx.conf):
upstream sub1.mydomain.com { ## can connect "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 not want nginx try run imagecontrol.php (i not using php-fpm) - want nginx pass upstream apache container.
desired behavior example:
(this part works)
- request http://sub1.mydomain.com/foo/bar.png
- /path/to/dockerized/webroot/foo/bar.png not exist, request proxied remote ip.
(this part not work)
- request http://sub1.mydomain.com/foo/baz.png
- /path/to/dockerized/webroot/foo/baz.png exists, request rewritten http://sub1.mydomain.com/imagecontrol.php?file=foo/baz.png
- the request sent upstream apache container
i'd appreciate help.
you may have more luck using 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 more.
Comments
Post a Comment