2

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.)

2 Answers 2

4

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.

1

On a restart, Nginx started throwing errors about that rewrite line:

 [emerg]: directive "rewrite" is not terminated by ";" in /etc/nginx/sites-enabled/production:14

That gave me a hunch the syntax was wrong. I removed the {2} and replaced it with a +, which is a less-precise but also more compatible regex, and now it works. The corrected regex is

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .