[英]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.