简体   繁体   English

如何从http重定向到https和example.com重定向到www.example.com

[英]How to redirect from http to https and example.com to www.example.com

I need help with the following redirect example, using NGINX: 我需要使用NGINX进行以下重定向示例的帮助:

I've read related questions but still can't get both redirects to work together. 我已经阅读了相关 问题,但仍然无法使两个重定向一起工作。

I've tried this configuration, but I get an error when trying to load the site: The connection was interrupted . 我已经尝试了这种配置,但是在尝试加载站点时出现错误: 连接中断

# Redirect any http:// request to https://www.example.com
server {
  listen 80;
  return 301 https://www.example.com$request_uri;
}

# Redirect http://example.com to https://www.example.com
server {
  listen 443 ssl;
  server_name example.com
  return 301 https://www.example.com$request_uri;
}

server {
  listen  443;
  server_name www.example.com;

  ssl on;
  ssl_certificate /foo.crt;
  ssl_certificate_key /foo.key;

  root /foo/;
  index index.php index.html index.htm;

  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

  # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
  location ~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
 }

You have no SSL certificate defined in the server{} block for https://example.com , and this is what causes your problems. 您没有在server{}块中为https://example.com定义SSL证书,这就是导致您出现问题的原因。 You have to add a certificate there. 您必须在此处添加证书。 (And, BTW, this information should be in your error log. It's always a good idea to look into it if something goes wrong.) (顺便说一句,此信息应该在您的错误日志中。如果出现问题,最好一直查看此信息。)

Usually it's the same cert as for www.example.com, so you have to use: 通常,它与www.example.com的证书相同,因此您必须使用:

server {
  listen 443 ssl;
  server_name example.com;
  ssl_certificate /foo.crt;
  ssl_certificate_key /foo.key;
  return 301 https://www.example.com$request_uri;
}

Note well that ssl on; 请注意, ssl on; is not needed as you are using listen ... ssl; 不需要,因为您正在使用listen ... ssl; , see here for details. ,请参阅此处了解详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将https://example.com重定向到https://www.example.com - Redirect https://example.com to https://www.example.com htaccess将http://example.com重写为http://www.example.com,但将https://www.example.com重写为https://example.com - htaccess rewrite http://example.com to http://www.example.com but https://www.example.com to https://example.com http://example.com、http://www.example.com和https://example.com到https://www.example.com - http://example.com, http://www.example.com and https://example.com to https://www.example.com 在example.com上没有有效证书的情况下从https://example.com重定向到https://www.example.com - Redirect from https://example.com to https://www.example.com without a valid certificate on example.com http://example.com不会重定向到https://www.example.com - http://example.com would not redirect to https://www.example.com 如何在NginX中将www.example.com重定向到example.com? - How to redirect www.example.com to example.com in NginX? how to redirect https://www.example.com to https://example.com with .htaccess? - how to redirect https://www.example.com to https://example.com with .htaccess? 如何在没有htaccess文件的情况下使https://www.example.com重定向到https://example.com - How to get https://www.example.com to redirect to https://example.com without a htaccess file 如何将https://www.example.com重定向到https://example.com - How to redirect https://www.example.com to https://example.com 如何将https://example.com/重定向到https://www.example.com/ - How to Redirect https://example.com/ to https://www.example.com/
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM