https on a specific directory only?

Mikhail Mazursky ash2kk at gmail.com
Mon Dec 6 11:00:16 MSK 2010


2010/12/6 TECK <nginx-forum at nginx.us>:
> Hi all,
>
> I recently purchased a SSL certificate and I would like to use it only
> on a specific location:
>
> [code]
> server {
>        listen                          10.30.1.50:80 default_server backlog=1024 rcvbuf=32k
> sndbuf=8k;
>        listen                          10.30.1.50:443 ssl;
>        server_name                     www.domain.com;
>        ssl_certificate                 domain.com.crt;
>        ssl_certificate_key             domain.com.key;
> ...
>        location / {
>                try_files               $uri $uri/ /index.php?$uri&$args;
>        }
>
>        location /dir/ {
>                auth_basic              "Restricted Access";
>                auth_basic_user_file    htpasswd;
>                rewrite         ^       https://www.domain.com/dir$request_uri? permanent;
>        }
> ...
> }
> [/code]
>
> In other words, if you access the scheme with a http value, it redirects
> you to the https scheme.
> I really don't want to use any IF's as conditionals.
> Right now, the rewrite creates a redirect loop. How can I fix that?

You can create separate servers for HTTPS and HTTP or use a rewrite
like this (not tested this config):

       location /dir/ {
               auth_basic              "Restricted Access";
               auth_basic_user_file    htpasswd;
               if ($server_port = 80) {
                 rewrite         ^
https://www.domain.com/dir$request_uri? permanent;
               }
       }

Also, it seems that you should remove extra '/dir' from rewrite rule
and write it like this:
rewrite         ^       https://www.domain.com$request_uri? permanent;

Hope it helps.



More information about the nginx mailing list