1

So I am trying to set up an nginx default.conf and I'm having trouble using variables. I want to capture the subdomain as the $subdomain variable and use it a few times in the default.conf.

Here is my config:

server {
     listen 80;
     server_name  ~^(?<subdomain>.+)\.example\.com$;
     # To allow special characters in headers
     ignore_invalid_headers off;
     # Allow any size file to be uploaded.  
     # Set to a value such as 1000m; to restrict file size to a specific value
     client_max_body_size 0;
     # To disable buffering
     proxy_buffering off;
     location / {
       rewrite ^/$ /$subdomain/index.html break;
       proxy_set_header Host $http_host;
       proxy_pass http://minio-server:9000/$subdomain/;
       #health_check uri=/minio/health/ready;
     }
}

Unfortunately the presence of the $subdomain variable in the location block fails nginx entirely every time. If I were to replace $subdomain in the location block with tester as a static value then everything works.

How to correctly use the $subdomain variable here???

This question is a somewhat of a followup on this issue: k8s-ingress-minio-and-a-static-site. In that issue I was trying to use Ingress to reverse proxy to a minio bucket, but to no avail. Now I'm just trying to go through Nginx directly but my vars ain't workin.

Updates

So it seems the proxy_pass will not resolve the host correctly if there is a variable in the URL.

Tried two things:

  1. set the resolver like so: resolver default.cluster.local. I tried a bunch of combos for the fqdn of the kube-dns but to no avail and kept getting minio-server can't be found.

  2. Simply don't use variables like Richard Smith mentions below. Instead rewrite everything then proxy pass. However I don't understand how this would work and I get very useless errors like so: 10.244.1.1 - - [07/Feb/2019:18:13:53 +0000] "GET / HTTP/1.1" 405 291 "-" "kube-probe/1.10" "-"

mr haven
  • 1,494
  • 1
  • 17
  • 28
  • What URI are you attempting to passing upstream? – Richard Smith Feb 07 '19 at 09:39
  • I don't understand what you mean by upstream? I just want to redirect a few subdomains to some subpaths on nginx. e.g. `mysite.com/tester` `-->` `tester.mystite.com` – mr haven Feb 07 '19 at 14:08
  • What is being passed to the `minio-server:9000` in your question. For example: Is `foo.example.com/bar` passed to `server:9000/foo/bar` or server:9000/foo/`? – Richard Smith Feb 07 '19 at 14:18
  • Oh I see. Like this: `foo.example.com/bar` `-->` `server:9000/foo/bar`. – mr haven Feb 07 '19 at 16:30

1 Answers1

1

According to the manual page:

When variables are used in proxy_pass: ... In this case, if URI is specified in the directive, it is passed to the server as is, replacing the original request URI.

So you need to construct the complete URI for the upstream server.

For example:

location = / {
    rewrite ^ /index.html last;
}
location / {
    proxy_set_header Host $http_host;
    proxy_pass http://minio-server:9000/$subdomain$request_uri;
}

It may be better to use rewrite...break and use proxy_pass without a URI.

For example:

location / {
    rewrite ^/$ /$subdomain/index.html break;
    rewrite ^ /$subdomain$uri break;
    proxy_set_header Host $http_host;
    proxy_pass http://minio-server:9000;
}
Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • Whoa, very close. Now I keep getting an error which says it can't resolve my `minio-server` host. [I found this article](https://stackoverflow.com/questions/17685674/nginx-proxy-pass-with-remote-addr) which says the proxy_pass host will just work without any variables, but with variables then I might need a `resolver` which points to my DNS. This is all deployed on k8s so I attempted to add the `kube-dns` host which I thought was either at `default.svc.cluster.local` or `10.96.0.10`. Yet I just get `minio-server not found on default.svc.cluster.local`. Any ideas? – mr haven Feb 07 '19 at 17:52
  • Oh dear, with the edits, I just get this error: `10.244.1.1 - - [07/Feb/2019:18:13:53 +0000] "GET / HTTP/1.1" 405 291 "-" "kube-probe/1.10" "-"` which is super vague. – mr haven Feb 07 '19 at 18:16