【问题标题】:How to redirect HTTP URL with IP address to HTTPS URL with domain name?如何将带有 IP 地址的 HTTP URL 重定向到带有域名的 HTTPS URL?
【发布时间】:2019-05-28 05:35:21
【问题描述】:

如何在 Nginx 中将 http://192.0.2.4 等 URL 重定向到 https://example.com 而不是 https://192.0.2.4

就像 Google 所做的那样,将 https://172.217.7.206 重定向到 https://google.com

我的 nginx 配置:

#Redirect all traffic to HTTPS

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

 server {

    listen 443 ssl http2;
    server_name  www.example.com;

我不想要任何重写,因为它会消耗更多资源。

【问题讨论】:

  • 当您重定向到 $host 时,您会重定向到已到达的同一主机。如果要重定向到自己的主机,返回 301 example.com$request_uri;重定向到您想要的主机。
  • 对不起,由于ssl不接受ip地址,nginx没有重定向HTTPS请求。
  • 我刚才写的答案不对吗?
  • 我刚刚在另一个问题上找到了答案,实际上通过nginx是不可能的。 stackoverflow.com/questions/50258202/…
  • 我以为你问过这个问题:serverfault.com/questions/629045/… 关闭,这个问题是否已经被回答,你找到了很好,谢谢你的信息:D

标签: http nginx https url-redirection


【解决方案1】:

将所有流量重定向到 HTTPS

如果您在服务器中接收的唯一域是 www.example.com,则此解决方案应该适合您。这会监听“ip”并将其重定向到 www.example.com。

server {
    server_name 192.0.2.4;
    return 301 https://www.example.com$request_uri;
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-29
    • 2018-05-14
    • 2015-02-13
    • 2016-01-09
    • 2020-08-07
    • 1970-01-01
    • 2019-05-31
    相关资源
    最近更新 更多