The simplest way to do this is with an if
directive. If there is a better way, please let me know, as people say the if
directive is inefficient. Nginx converts dashes to underscores in headers, so X-Forwarded-Proto
becomes $http_x_forwarded_proto
.
server {
listen 80;
server_name example.com; # Replace this with your own hostname
if ($http_x_forwarded_proto = "http") {
return 301 https://example.com$request_uri;
}
# Rest of configuration goes here...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…