繁体   English   中英

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

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

我需要使用NGINX进行以下重定向示例的帮助:

我已经阅读了相关 问题,但仍然无法使两个重定向一起工作。

我已经尝试了这种配置,但是在尝试加载站点时出现错误: 连接中断

# 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;
  }
 }

您没有在server{}块中为https://example.com定义SSL证书,这就是导致您出现问题的原因。 您必须在此处添加证书。 (顺便说一句,此信息应该在您的错误日志中。如果出现问题,最好一直查看此信息。)

通常,它与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;
}

请注意, ssl on; 不需要,因为您正在使用listen ... ssl; ,请参阅此处了解详细信息。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM