如何在共享 80 / 443 ssl nginx 服务器配置中检测 ssl?

myk*_*hal 7 configuration nginx

当nginx服务器这样配置时:

server {
  listen 80;
  listen 443 ssl;
  server_name www.example.org;
  ...
}
Run Code Online (Sandbox Code Playgroud)

如何检测请求是通过 HTTP 还是 HTTPS 发出的?

(例如$http_port不可用,因为端口没有明确指定,因此它是空的)

VBa*_*art 12

http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

  • $https — 如果连接在 SSL 模式下运行,则为“on”,否则为空字符串
  • $scheme — 请求方案,“http”或“https”

例如,如果您想将一个位置限制为 https 并且仅:

location /admin {
    if ($https = "") {
        return 404;
    }
    # ... and so on
}
Run Code Online (Sandbox Code Playgroud)