1

I'm looking to setup an nginx reverse proxy to make some upcoming server moves and load balanced implementations much easier within our apps. Since our servers are all IIS case sensitivity hasn't been an issue, but now with nginx it's becoming one for me. I am simply looking to do a rewrite regardless of case.

Infrastructure notes:

  • All backend servers are IIS
  • Most services are WCF services
  • I am trying to simplify the URLs so I can move services around as we continue to build out

I can't set my location to case insensitive due to the following error:

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/sites-enabled/test.conf:101

The main part of my conf file where I am trying to handle the rewrite is as follows

location /svc_test {
    proxy_set_header x-real-ip              $remote_addr;
    proxy_set_header x-forwarded-for        $proxy_add_x_forwarded_for;
    proxy_set_header host                   $http_host;

    proxy_pass  http://backend/serviceSite/WFCService.svc;
}

location ~* /test {
    rewrite ^/(.*)/$ /svc_test/$1 last;
}

It's the /test location that I can't get figured out. If I call http://nginxserver/svc_test/help I get the WCF help page to display correctly and I can make all available REST calls. This HAS to be a boneheaded regex issue on my part, but I have tried several variations and all I can get are 404 or 500 errors from nginx. This is NOT rocket science so can someone point me in the right direction so I can look like an idiot and just move on?

Maxim Dounin
  • 3,596
  • 19
  • 23
BrianM
  • 185
  • 1
  • 2
  • 8
  • What uri do you use for real tests? If `/test/help`, then your `rewrite` won't match due to `/$` in it. – Maxim Dounin Nov 10 '12 at 10:20
  • There are several valid links to the service. /test goes to the WCF info page, /test/help goes to the WCF method documentation page, and /test/contacts/1 would be a potential REST call to the contacts API. – BrianM Nov 10 '12 at 14:27
  • Regular expression `^/(.*)/$` won't match `/test`, `/test/help`, and `/test/contacts/1`. Use `^/(.*)` instead. – Maxim Dounin Nov 10 '12 at 18:03
  • You got me going in the right direction and it seems the final working version of my faulty regex is `rewrite ^/([^/]*)(.*)$ /svc_test$2;` and this handled every iteration of the REST call to my service including /test which took me to the WCF info page. ALWAYS blame the regex. – BrianM Nov 10 '12 at 23:26

1 Answers1

1

The final correct regex that got me where I wanted to go is

rewrite ^/([^/]*)(.*)$ /svc_test$2;

and this had the following results:

  • /test -> WCF service info page
  • /test/ -> error (correct)
  • /test/help -> WCF service endpoint help page
  • /test/contacts/1 -> Pulled from the get contact interface
  • /test/contacts -> Pulled the full contact list

I am (obviously) far from a fluent regex guy, so this may not be the optimal way to handle the case insensitive rewrite, but it does do everything I was looking to accomplish. The only thing I would like to add at some point is rewrite /test/ to /test to prevent any error pages, but that's not going to impact the service calls.

masegaloeh
  • 18,236
  • 10
  • 57
  • 106
BrianM
  • 185
  • 1
  • 2
  • 8