问题描述
我想重定向我的所有http
通信量以重定向到https
.我正在使用letsencrypt
.我在线阅读了return 301 https://$server_name$request_uri;
会将所有访问我的网站的流量重定向到https
,但是却导致了ERR_TOO_MANY_REDIRECTS
.
I want to redirect all my http
traffic to redirect to https
. I am using letsencrypt
. I read online that return 301 https://$server_name$request_uri;
would redirect all the traffic to my website over to https
but instead it results in ERR_TOO_MANY_REDIRECTS
.
没有上面提到的语句,一切都可以正常工作,但是随后我必须在URL中专门指定https
.这是我的/etc/nginx/sites-available/default
文件:
Everything works fine without the above mention statement, but then I have to specifically specify https
in the URL. Here's my /etc/nginx/sites-available/default
file:
server {
listen 80 default_server;
listen 443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/mywebsite.me/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.me/privkey.pem;
root /home/website/mywebsite/public;
index index.html index.htm index.php;
server_name mywebsite.me www.mywebsite.me;
return 301 https://$server_name$request_uri;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
}
我要去哪里错了?
推荐答案
将配置更改为以下
server {
listen 80 default_server;
server_name mywebsite.me www.mywebsite.me;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/mywebsite.me/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.me/privkey.pem;
root /home/website/mywebsite/public;
index index.html index.htm index.php;
server_name mywebsite.me www.mywebsite.me;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
}
您当前的配置在http和https上都重定向到https.因此,由于return语句,它变成了无限循环.仅在连接为http时才需要return语句.因此,您将其分为两个服务器块
Your current config redirects on both http and https to https. So it becomes a infinite loop because of the return statement. You want return statement only when connection is http. So you split it into two server blocks
这篇关于带有nginx的ERR_TOO_MANY_REDIRECTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!