![](/img/trans.png)
[英]Redirect https://example.com to https://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.