M.Green
M.Green

Reputation: 21

NGINX Redirect to HTTPS Minus a Subdirectory?

Nginx is new to me. I'm currently redirecting http to https using the following.

if ($scheme = http) {
    return 301 https://$server_name$request_uri;
}

How would I do the same except not redirect anything going to subfolder /abcd/ ? I've tried using location blocks, but it gives me a "too many redirects" error when loading the site.

Thanks for any help you can give.

Upvotes: 2

Views: 306

Answers (1)

Richard Smith
Richard Smith

Reputation: 49772

The if statement suggests that you are processing http and https requests in the same server block. You need to use two server blocks. This will remove the if block and allow you to use simple location blocks to handle the excluded directories.

For example:

server {
    listen 80;

    location /abcd/ {
        ...
    }
    location / {
        return 301 https://$host$request_uri;
    }
}
server {
    listen 443 ssl;
    ...
}

Upvotes: 1

Related Questions