Nginx – Debugging nginx URL rewrite: How to figure out where the problem is

djangonginxregexrewrite

I have a specific URL pattern on a site which needs to be redirected to the HTTPS version. This is a Django site; Nginx checks each URL in memcached, and if it doesn't find a cached version it proxies the request to Apache/mod_python for Django to render the page.

The relevant configuration block is

    rewrite ^/certificate              https://mysite.com/certificate ;
    rewrite ^/([a-zA-Z]{2})/certificate   https://mysite.com/certificate ;

…and it doesn't appear to be working at all.

Nginx is:

$ nginx -V
nginx version: nginx/0.7.65
built by gcc 4.2.4 (Ubuntu 4.2.4-1ubuntu4)
TLS SNI support disabled
configure arguments: --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --with-http_gzip_static_module --with-http_ssl_module

How can I figure out if the problem is my patterns not matching, or a more obscure configuration problem?

(The site is localized to three languages, and the localization is in the URL string, e.g. /US/news/, /DE/about, etc. It tracks localization in the session as well, defaulting to US, so if you just requested /news Django will rewrite to /US/news unless the user has a cookie indicating they're using a different localization. Django handles this, though, not Nginx.)

Best Answer

nginx's config regex is fussy about curly braces, but you can use them if you quote your regex...

rewrite "^/([a-zA-Z]{2})/certificate"   https://mysite.com/$1/certificate ;

should work.