簡體   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