I have configured Nginx to proxy requests to an Apache server for handling WebDAV, as Nginx’s WebDAV support is limited when dealing with Windows clients. Currently, all requests, including GET requests, are proxied to Apache, which introduces unnecessary overhead for GET requests that should be served directly by Nginx.
I want to conditionally use try_files
for GET requests and proxy_pass
for all other methods. For example, I’d like something like this:
if ($request_method = GET) {
alias / try_files ....;
}
However, this approach isn’t working. The if
directive doesn’t support try_files
, proxy_pass
, or alias
directives. I’ve also explored using map
but haven’t found a viable solution.
Here’s my current configuration, which does not include conditional handling based on $request_method
:
set $dest $http_destination;
if ($http_destination ~ "^https://(?<myvar>(.+))") {
set $dest http://$myvar;
}
location /data/test {
root /webdav;
# Proxy requests to Apache
proxy_pass http://localhost:8084/data/test;
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-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Connection "";
proxy_set_header Destination $dest;
proxy_http_version 1.1;
limit_except GET PROPFIND OPTIONS {
auth_basic "restricted";
auth_basic_user_file /etc/nginx/.htpasswd_test;
}
allow all;
}
Does anyone have a solution for conditionally using try_files
or proxy_pass
based on the request method?