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.

How can I set up the SSL config for NGINX so that anything except for a few defined paths redirects to the non-SSL location?

My current config looks like this. It's set up for Django.

# Django Configuration
upstream prod {
    server 127.0.0.1:8080;
}

server {
    listen 80;
    server_name www.mysite.org;
    rewrite ^/(.*) http://mysite.org/$1 permanent;
}

server {
    listen 80;
    server_name mysite.org;
    root /var/www/mysite.org/cache/;

    access_log /var/log/nginx/mysite.org.access.log;
    error_log /var/log/nginx/mysite.org.error.log;

     client_max_body_size 100m;

    #Static files location
    location ~ ^/(media)/ {
        root /var/www/mysite.org/html/;
        expires 30d;
    }

    # Send everyone to django initially
    set $django 1;

    if ($is_args = "?") {
        set $args_old ?$args;
    }
    if ($is_args = "") {
        set $args_old "";
    }

    default_type text/html;

    location / {
        include /etc/nginx/proxy.conf;

        if (-f $request_filename/index.html$args_old) {
            set $django 0;
        }
          if ($request_method != GET) {
               set $django 1;
          }

        # For authorized users
        # if ($http_cookie ~* "sessionid=([^;]+)(>:;|$)") {
        #     set $django 1;
        # }

        # Give back cache
        if ($django = 0) {
            rewrite (.*) $1/index.html$args_old break;
        }
        # Send to django
        if ($django) {
            proxy_pass http://prod;
            break;
        }
    }
}

server {
    listen 443;
    server_name mysite.org;

     ssl on;
     ssl_certificate /etc/ssl/certs/mysite-final.crt;
     ssl_certificate_key /etc/ssl/private/mysite.key;
     ssl_prefer_server_ciphers on;

    # access_log /var/log/nginx/ssl.mysite.access.log;
    # error_log /var/log/nginx/ssl.mysite.error.log;

     client_max_body_size 100m;

    #Static files location
    location ~ ^/(media)/ {
        root /var/www/mysite.org/html/;
        expires 30d;
    }

    default_type text/html;

    location / {
        include /etc/nginx/proxy.conf;
          proxy_set_header X-Forwarded-Protocol https;
          proxy_pass http://prod;
    }
}
share|improve this question
1  
Did you ever figure this out? I cannot find instructions for this anywhere. –  pbreitenbach May 16 '11 at 20:06
add comment

1 Answer

up vote 1 down vote accepted

You just need to add this:

rewrite ^(.*) https://$server_name$1 permanent;

Towards the end of the location / section in your config.

share|improve this answer
    
Actually, he wanted it the other way: non-SSL for everything except a few paths. This is actually a bit tricky to do in nginx. –  cjc Aug 27 '11 at 0:48
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.