为 acme 挑战在 heroku 上重写 nginx

nginx rewrite on heroku for acme-challenge

我正在尝试使用 https://github.com/dmathieu/sabayon 为我在 heroku 上的 php 应用程序设置 lets-encrypt。
Sabayon 提供了一个使用 Apache 从 lets-encrypt 重定向 acme-challenge 调用的示例:https://github.com/dmathieu/sabayon#php-apps

我试图将其转换为 Nginx,但无法在 Heroku 上运行。
在本地它工作正常。

我试过了:

location ~ ^/.well-known/acme-challenge/(.*)$ {
    if (!-e $request_filename){
      rewrite ^(.*)$ /.well-known/acme-challenge/index.php?q= last;
      break;
    }
}

location ~ \.php$ {
    try_files @heroku-fcgi @heroku-fcgi;
}

但这会导致 PHP 代码作为下载文件。

我也试过:

location /.well-known/acme-challenge/ {
    # try to serve file directly, fallback to rewrite
    try_files $uri @rewriteapp;
}

location @rewriteapp {
    rewrite ^(.*)$ /.well-known/acme-challenge/index.php/ last;
} 

location ~ \.php$ {
    try_files @heroku-fcgi @heroku-fcgi;
}

这会导致 403。

更新 我刚刚发现 403 是由 .well-known/acme-challenge.

中的点引起的

如何才能做到这一点?

有几件事需要处理:

  • 重定向
  • 使隐藏目录.well-known可访问
  • 作为php
  • 执行php文件

所以这最终对我有用:

location ^~ /.well-known/acme-challenge/ {
    allow all;
    # try to serve file directly, fallback to rewrite
    try_files $uri @rewriteacme;
}

location @rewriteacme {
    rewrite ^(.*)$ /.well-known/acme-challenge/index.php/ last;
}

location ^~ /.well-known/acme-challenge/index.php {
    try_files @heroku-fcgi @heroku-fcgi;
    internal;
}