我想通过使用位置块的 nginx 配置在我的应用程序中强制使用 HTTPS 和顶点域(例如https://example.com)。我目前有以下 nginx_app.conf 文件(适用于顶点和 www 子域,以及 http 和 https):
location / {
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/(app|config)\.php(/|$) {
# fastcgi_pass directives go here...
}
Run Code Online (Sandbox Code Playgroud)
为了强制使用顶点域和 https,我尝试使用 if 语句如下,检查 $scheme 和 $host 变量,但我收到一个错误,表明页面没有正确重定向。我还添加了一个 HSTS 指令。
location / {
if ($scheme = http) {
rewrite ^/(.*) https://$host/$1 permanent;
}
if ($host = www.example.com) {
rewrite ^/(.*) https://example.com/$1 permanent;
}
try_files $uri @rewriteapp;
}
location @rewriteapp …
Run Code Online (Sandbox Code Playgroud)