Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
将HTTP重定向到HTTPS时如何处理Nginx中的400错误_Http_Redirect_Ssl_Nginx_Https - Fatal编程技术网

将HTTP重定向到HTTPS时如何处理Nginx中的400错误

将HTTP重定向到HTTPS时如何处理Nginx中的400错误,http,redirect,ssl,nginx,https,Http,Redirect,Ssl,Nginx,Https,我拥有一个网站,比如HTTP的example.com。考虑到安全性,现在我想将HTTP更改为HTTPS。我希望所有老客户都能访问我的网站,即使他们使用example.com,它将通过Nginx重定向到https 当然,我搜索了很多,然后我的解决方案是: upstream www { server 127.0.0.1:4000; } server { listen 80; listen 443 ssl; server_name localhost www exa

我拥有一个网站,比如HTTP的example.com。考虑到安全性,现在我想将HTTP更改为HTTPS。我希望所有老客户都能访问我的网站,即使他们使用example.com,它将通过Nginx重定向到https

当然,我搜索了很多,然后我的解决方案是:

upstream www {
  server 127.0.0.1:4000;
}
server {
  listen      80;
  listen      443 ssl;
  server_name localhost www example.com;

  ssl on;
  ssl_certificate  /usr/local/etc/docs/example.crt;
  ssl_certificate_key  /usr/local/etc/docs/example.key;

  if ($ssl_protocol = "") {
    rewrite ^ https://$host$request_uri? permanent;
  }

  # below are some other stuff
  # ...
}
但当我访问example.com时,我发现:

400错误请求普通HTTP请求已发送到HTTPS端口

然后,我在读取后更改nginx.conf,并通过497配置错误页面:

upstream www {
  server 127.0.0.1:4000;
}
server {
  listen      80;
  listen      443 ssl;
  server_name localhost www example.com;

  ssl on;
  ssl_certificate  /usr/local/etc/docs/example.crt;
  ssl_certificate_key  /usr/local/etc/docs/example.key;

  error_page 497  https://$host$request_uri;

  # below are some other stuff
  # ...
}
然后它工作了,一切都很好。但我只是不知道为什么,错误页面的解决方案似乎是错误的。所以 阅读之后,我添加了默认值并删除了上的ssl

upstream www {
  server 127.0.0.1:4000;
}
server {
  listen      80;
  listen      443 default ssl;
  server_name localhost www example.com;

  ssl on;
  ssl_certificate  /usr/local/etc/docs/example.crt;
  ssl_certificate_key  /usr/local/etc/docs/example.key;

  if ($ssl_protocol = "") {
    rewrite ^ https://$host$request_uri? permanent;
  }

  # below are some other stuff
  # ...
}
太好了!它又起作用了。但我不确定:

  • 哪种解决方案是正确的?
  • 如果两者都正确,哪一个对SEO更友好?

解决方案1是真正有线的,从中可以发现永久重定向更友好

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


server {

  listen      443 default ssl;
  server_name  www example.com;

  ssl on;
  ssl_certificate  /usr/local/etc/docs/example.crt;
  ssl_certificate_key  /usr/local/etc/docs/example.key;



  # below are some other stuff
  # ...
}