Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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     ;
    rewrite ^/([a-zA-Z]{2})/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/ --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.)

share|improve this question

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"$1/certificate ;

should work.

share|improve this answer
up vote 1 down vote accepted

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$1/certificate ;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.