sf_tristanb
sf_tristanb

Reputation: 8855

Nginx location alias + redirection

I want to be able, if the request tries to access <host>/.well-known/acme-challenge/ to serve the matching file if found.

If the request is something else, I want to redirect it to https://

server {
    listen 8080;
    server_name my_example.com;

    location /.well-known/acme-challenge/ {
        alias /var/www/encrypt/.well-known/acme-challenge/;
        try_files $uri =404;
    }

    return 301 https://$server_name$request_uri;
}

Problem is when accessing to my_example.com/.well-known/acme-challenge/12345 which exist in alias path, i'm still redirected to https:// and the browser doesn't download the file.

How can I just serve the file and not apply the redirection in this case ?

Thanks

Upvotes: 4

Views: 3411

Answers (1)

Richard Smith
Richard Smith

Reputation: 49722

Place the return statement inside a location block:

For example:

server {
    listen 8080;
    server_name my_example.com;

    location /.well-known/acme-challenge/ {
        root /var/www/encrypt;
    }
    location / {
        return 301 https://$server_name$request_uri;
    }
}

Also, simplified your other location block.

Upvotes: 5

Related Questions