Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx:重写规则以重定向到https页面_Nginx_Rewrite - Fatal编程技术网

Nginx:重写规则以重定向到https页面

Nginx:重写规则以重定向到https页面,nginx,rewrite,Nginx,Rewrite,我想将我的登录页面重定向到https,这样用户就不会无意中通过未加密的网络输入他们的凭据。我如何做下面这样的事情 # nginx.conf server { server_name example.org; rewrite http://*.example.org/login https://example.org/login; } 这适用于,但不适用于 重写的规则输出 rewrite http://.*\.?example.org/login https://example.org/

我想将我的登录页面重定向到https,这样用户就不会无意中通过未加密的网络输入他们的凭据。我如何做下面这样的事情

# nginx.conf
server {
  server_name example.org;
  rewrite http://*.example.org/login https://example.org/login;
}
这适用于,但不适用于

重写的规则输出

rewrite http://.*\.?example.org/login https://example.org/login;
重写调试输出:

*2 "http://.*\.?example.org/login" does not match "/login", client: XXX.XXX.XX.72, server: example.org, request: "GET /user HTTP/1.1", host: "example.org"

我删除了我原来的答案,因为它要简单得多。您需要一个位置块和一个if才能使其工作,同时还需要一个catchall服务器域。结构如下:

 server {
   server_name example.org;
   server_name *.example.org;
   location /login {
       if ( $scheme = http ) {
          rewrite ^ https://example.org/login last;
       }
   }
 }

虽然我很讨厌在nginx中使用ifs,但这一次,它是必要的。

我删除了我原来的答案,因为它要简单得多。您需要一个位置块和一个if才能使其工作,同时还需要一个catchall服务器域。结构如下:

 server {
   server_name example.org;
   server_name *.example.org;
   location /login {
       if ( $scheme = http ) {
          rewrite ^ https://example.org/login last;
       }
   }
 }

尽管我不喜欢在nginx中使用ifs,但这次是必要的。

如果您不想硬编码主机名(example.org),可以使用$server\u name变量(根据),如果您不想硬编码主机名(example.org),可以使用$server\u name变量(根据)