Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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'm trying to redirect anyone who access the admin section of my site to the HTTPS version of it. The current rewrite rule is as follows:

server {
        listen          80;

        location / {
                index index.php index.html;
                root  /home/;

        #Redirect Admin requests to secure server
        location /www/admin/  {
                rewrite ^/(.*)$1 permanent;

The problem with this rule is it is only forwarding to HTTPS - going to , for example, does not redirect. Any idea how to correct this so that anything BELOW /www/admin also gets redirected? Thanks!

share|improve this question
up vote 4 down vote accepted

Read the four rules in the wiki entry linked by AlexD. Regular expression locations (like your php location) take precedence over plain static locations. To prevent that, you want to use the ^~ flag on your static location:

location ^~ /www/admin/ {
    rewrite ^$request_uri? permanent;

EDIT: Also, you shouldn't set your root in location /. Set it in the server and let all your locations inherit the setting. See 1.2

share|improve this answer
This appears to have been the problem - Thanks for the help! – Professor Frink Apr 29 '11 at 11:45

Check nginx documentation for description of location directive. You should use location instead location =

share|improve this answer
Tried that already, doesn't help :( – Professor Frink Apr 28 '11 at 16:05
There is another error in rewrite statement - you seems to be redirecting to domain.comwww/admin (note missing slash). – AlexD Apr 28 '11 at 16:15
That doesn't seem to be causing any issues - when I go to (http), it correctly redirects to *(https) - it's just (http) or similar which doesn't redirect – – Professor Frink Apr 28 '11 at 17:08
You probably have some other rules using regexps like location ~* *.php$ which has precedence over location /www/admin. See @kolbyjack answer how to fix it. – AlexD Apr 29 '11 at 6:22

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.