Nginx 位置别名 + 重定向 [英] 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.
如果请求是别的东西,我想把它重定向到https://
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;
}
问题是在访问别名路径中存在的 my_example.com/.well-known/acme-challenge/12345
时,我仍然重定向到 https://浏览器不下载文件.
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 ?
谢谢
推荐答案
将 return
语句放在 location
块中:
Place the return
statement inside a location
block:
例如:
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;
}
}
另外,简化您的其他 location
块.
Also, simplified your other location
block.
这篇关于Nginx 位置别名 + 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!