Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'd like to rewrite specific locations to https like here:

server {
    listen       80;
    server_name  mysite.com www.mysite.com;
    root   /var/www/$server_name/public;
    #error_log /error.logg debug;
    error_page 403 /403.html;
    location  = /403.html {
        allow all;
    }
    location / {
        #allow xxx.xxx.xxx;
        #deny all;
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location /register {
        rewrite ^ https://$http_host$request_uri?$args permanent;
    }
    location /login {
        rewrite ^ https://$http_host$request_uri?$args permanent;
    }
}

/register and /login are not the only places i'll need to write, so how would I deal with multiple locations without having the repeat the location block for each place I want to rewrite to https?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can use a regular expression location match, like this:

location ~ ^/(login|register) {
    rewrite ^ https://$http_host$request_uri?$args permanent;
}
share|improve this answer
add comment

Your Answer

 
discard

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.