如何将https://请求重定向到NGINX中的http://?

deb*_*deb 42 ssl https redirect nginx

有没有办法通过在域的vhost文件中添加规则将https://请求重定向到http://?我正在使用NGINX.

Sri*_*aju 36

为什么这样的东西有用?乍一看,我不确定是否可以完成.但它提出了一个有趣的问题.

您可以尝试在配置文件中放置重定向语句并重新启动服务器.有两种可能性:

  1. 服务器将发出重定向 - 您似乎想要的.
  2. 服务器将首先进行https交换,然后发出重定向,在这种情况下,重点是什么?

如果我想出更具体的东西,会增加更多.

更新:( 几小时后) 您可以试试这个.你需要把它放在你的nginx.conf文件中 -

server {
       listen 443;
       server_name _ *;
       rewrite ^(.*) http://$host$1 permanent;
 }
Run Code Online (Sandbox Code Playgroud)

向客户端发送永久重定向.我假设你使用端口443(默认)为https.

server {
    listen      80;
    server_name _ *;
    ...
}
Run Code Online (Sandbox Code Playgroud)

添加此项以便端口80上的正常http请求不受干扰.

更新: 201612月18日 - server_name _应该使用而不是server_name _ *nginx版本> 0.6.25(感谢@Luca Steeb)

  • 我认为解决方案不对.没有`ssl on`重定向443到80,与https不匹配,它不应该工作.但是如果添加`ssl on`,它将需要ssl_certificates,你必须提供正确的证书.所以我认为它无法将https重定向到没有证书的http.相关问题:http://stackoverflow.com/questions/3470290/nginx-redirect-https-to-http (7认同)
  • 它有用的另一个原因是:我有一个托管多个域的服务器.其中一个拥有SSL,但其他域没有SSL证书.当有人通过HTTPS访问其他域时,需要重定向到HTTP (5认同)
  • 这对我不起作用,访问我的服务器时仍然遇到问题:`OpenSSL:错误:140770FC:SSL例程:SSL23_GET_SERVER_HELLO:未知协议无法建立SSL连接. (3认同)
  • 在nginx版本> 0.6.25中应该使用`server_name _`而不是`server_name _*` (3认同)
  • 旧的答案,但只是想说它是有用的,因为现在的"不安全的内容"规则.例如,使用iframe时. (2认同)
  • 我认为这不是正确的答案。您需要一个证书来满足 ssl/https 要求,然后您可以重定向到 http/80 端口。正如当前第二个流行答案所做的那样。 (2认同)

Zen*_*xer 35

rewrite并且if应该避免使用Nginx.着名的一句是"Nginx不是Apache":换句话说,Nginx有更好的方法来处理URL而不是重写. return从技术上讲,它仍然是重写模块的一部分,但它并没有带来开销rewrite,而且并不像它那样需要注意if.

Nginx已经对整个页面,为什么if是"邪恶".它还提供了一个建设性的页面,解释了为什么rewriteif不好,以及如何解决它.下面是页面有说关于rewriteif:

这是一种错误,繁琐且无效的方式.

您可以妥善解决这个问题,使用return:

server {
    listen 443 ssl;

    # You will need a wildcard certificate if you want to specify multiple
    # hostnames here.
    server_name domain.example www.domain.example;

    # If you have a certificate that is shared among several servers, you
    # can move these outside the `server` block.
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/cert.key;

    # 301          indicates a permanent redirect.  If your redirect is
    #              temporary, you can change it to 302 or omit the number
    #              altogether.
    # $http_host   is the hostname and, if applicable, port--unlike $host,
    #              which will break on non-standard ports
    # $request_uri is the raw URI requested by the client, including any
    #              querystring
    return 301 http://$http_host$request_uri;
}
Run Code Online (Sandbox Code Playgroud)

如果你期望很多机器人不发送Host标题,你可以使用$host而不是$http_host只要你坚持端口80和443.否则,你需要动态填充$http_host替代品.尽管使用了代码,但只要它出现在server(而不是location块中)的根中,该代码就是高效且安全的if.但是,您需要使用默认服务器才能应用此功能,应使用https避免使用.

set $request_host $server_name:$server_port;
if ($http_host) {
    set $request_host $http_host;
}
Run Code Online (Sandbox Code Playgroud)

如果要为特定路径强制执行SSL/TLS,则禁止其他方式:

server {
    listen 443 ssl;
    server_name domain.example;

    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/cert.key;

    location / {
        return 301 http://$host$request_uri;
    }

    location /secure/ {
        try_files $uri =404;
    }
}

server {
    listen 80;
    server_name domain.example;

    location / {
        try_files $uri =404;
    }

    location /secure/ {
        return 301 https://$http_host$request_uri;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您的服务器没有与客户端直接通信 - 例如,如果您正在使用CloudFlare - 事情会变得复杂一些.您需要确保与客户端直接通信的任何服务器X-Forwarded-Proto都为请求添加了适当的标头.

使用这个是一个混乱的主张; 有关完整说明,请参阅IfIsEvil.为了使其有用,由于各种复杂的原因,if块不能在location块内.这会强制使用rewriteURI测试. 简而言之,如果你必须在生产服务器上使用它...不要.可以这样想:如果你已经超越了Apache,那么你已经超越了这个解决方案.

/ secure,/ secure /,/ secure /中的任何内容都将强制执行https,而所有其他URI将强制执行http.该(?! )PCRE构造是式断言. (?: )是一个非捕获组.

server {
    # If you're using https between servers, you'll need to modify the listen
    # block and ensure that proper ssl_* statements are either present or
    # inherited.
    listen 80;
    server_name domain.example;

    if ($http_x_forwarded_proto = https) {
        rewrite ^(?!/secure)/ http://$http_host$request_uri? permanent;
    }
    if ($http_x_forwarded_proto != https) {
        rewrite ^/secure(?:/|$) https://$http_host$request_uri? permanent;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 好的,所以如果我理解正确的话,没有办法通过http向请求https的客户端提供服务而不先建立https连接.如果你想将人们从https移到http,你需要证书吗? (3认同)
  • @Freek是的,尽管你应该始终支持HTTPS;这是一个非常基本但有效的安全功能,任何关心密码的用户都会需要它。您可以从 [Let's Encrypt](https://letsencrypt.org/) 获取免费证书。即使您的网站不包含任何敏感信息,它也可能被用作攻击媒介。 (2认同)
  • @jmcollin92 如果安全性是最重要的,那么麻烦是值得的。这使得黑客更难访问您的 VPC 中的服务器来获取有用信息。黑客通常会瞄准旧的、被遗忘的服务器,然后通过网络进行攻击。 (2认同)

ans*_*art 6

location / {
    if ($scheme = https) {
        rewrite ^(.*)? http://$http_host$1 permanent;
    }
}
Run Code Online (Sandbox Code Playgroud)


mc0*_*c0e 5

这个问题更适合 serverfault.com 站点。

重定向到 http 的更好方法:

server {
   listen 443;
   return 301 http://$host$request_uri;
}
Run Code Online (Sandbox Code Playgroud)

这避免了重写中的“if”子句和正则表达式,这些都是迄今为止其他解决方案的特征。两者都有性能影响,但在实践中你必须有相当多的流量才重要。

根据您的设置,您可能还想在 listen 子句中指定一个 ip,也许还需要在上面的 servername 子句中指定。照原样,它将适用于所有域名的所有端口 443 请求。您通常希望每个域都有一个带有 https 的 IP,因此将上述内容绑定到 IP 比将其绑定到域名更重要,但也有一些变化,例如所有域都是一个域的子域。

编辑:TLS 现在接近于通用,并且有了它的服务器名称标识 (SNI),它允许多个域上的 HTTPS 站点共享一个 IP。有一个很好的写了这里