nginx 如果主机为 true,有位置和 root

nginx if host true with location and root

我将 dockernginx 一起使用,这是我的 app 配置文件:

server {
  listen 80;
  server_name app.me.site;
  return 308 https://$host$uri;
  location .well-known {
    root /var/www/.well-known;
    try_files /$uri /$uri/ /index.html;
  }
  # other configs
}

路径是/var/www/app.

我还为 Let's Encrypt 创建了 /var/www/.well-known,它可以访问,但只有 https.

可以访问

我需要一个 if 引语:如果 URL 是 app.me.site/.well-known,请不要使用 https

我试图达到这一点,但没有找到任何线索。

您的配置不可用,因为 return 指令在 NGX_HTTP_SERVER_REWRITE_PHASE 执行,而适当的位置选择稍后将在 NGX_HTTP_FIND_CONFIG_PHASE 完成(请求处理阶段在development guide)。要修复它,您应该将 return 指令移动到 location 块(我还建议使用 $request_uri 变量而不是规范化的 $uri 变量):

location / {
    # everything not started with the '/.well-known/' prefix will be processed here
    return 308 https://$host$request_uri;
}
location /.well-known/ {
    # do not append '/.well-known' suffix here!
    # (see the difference between 'root' and 'alias' directives)
    root /var/www;
    try_files $uri =404;
}