HTTPS在nginx中redirect

我正在尝试将所有HTTP通信redirect到HTTPS。 我使用的Web服务器是nginx。 这是我用来做redirect的服务器块。

server { listen 80; rewrite ^ https://$server_name$request_uri? permanent; } 

这成功地将URLredirect到http://localhosthttps://localhost 。 但是,对于像http://localhost/table/这样的URL,我将redirect到https://table ,这是不正确的。 我希望它重新指向https://localhost/table/

任何帮助将非常感激。

更新:似乎重写scheme有一个跟踪斜线问题。 例如, http://localhost/table被正确redirect,但http://localhost/table/不是。

 server { listen 80; rewrite 301 https://$host$request_uri; } 

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