Nginx位置别名+重定向

sf_*_*anb 4 redirect nginx

我希望能够在请求尝试访问时<host>/.well-known/acme-challenge/提供匹配的文件(如果找到)。

如果请求是其他内容,我想将其重定向到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;
}
Run Code Online (Sandbox Code Playgroud)

问题是当访问my_example.com/.well-known/acme-challenge/12345别名路径中存在的文件时,我仍然被重定向到 https:// 并且浏览器不会下载该文件。

在这种情况下,我怎样才能只提供文件而不应用重定向?

谢谢

Ric*_*ith 5

return语句放在块内location

例如:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,简化了您的其他location块。