我正在尝试将server_name的所有非http和www流量重定向到https://example.com。我有以下问题:
我尝试使用不同的服务器块进行多种变化,而我最接近的就是让它全部正常工作,除了重定向循环,无论我做什么都会困扰我。非常感谢这里的一些建议和指示。代码如下:
server {
listen 443;
listen 80;
server_name www.example.com example.com;
root /home/example/public_html;
index index.html index.htm index.php;
#SSL Stuff here
if ($scheme = http) { return 301 https://example.com$request_uri; }
if ($server_name = www.example.com) { return 301 https://example.com$request_uri; }
include conf.d/wp/restrictions.conf;
include conf.d/wp/wordpress.conf;
}
编辑
所以我尝试了下面的内容,除了http://www.example.com允许传递没有重定向到SSL之外,它都没有301循环。我不明白这是怎么可能的,因为它应该被端口80规则捕获?更新了以下配置:
server {
listen 80;
server_name example.com;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
################# SECURE #################
server {
listen 443;
server_name example.com;
access_log /var/log/nginx/example-ssl.access.log;
error_log /var/log/nginx/example-ssl.error.log;
root /home/example/public_html;
index index.html index.htm index.php;
# SSL Stuff here
#include conf.d/wp/restrictions.conf;
#include conf.d/wp/wordpress.conf;
}
答案 0 :(得分:0)
您的配置存在一些问题。
$server_name
替换为$host
或对域进行硬编码
您想要的,例如“ secondexample.com$request_uri
” ssl
行中缺少server 443
标签。配置:
server {
listen 80;
server_name example.com;
server_name www.example.com;
return 301 https://$host$request_uri;
}
################# SECURE #################
server {
listen 443 ssl;
server_name example.com;
access_log /var/log/nginx/example-ssl.access.log;
error_log /var/log/nginx/example-ssl.error.log;
root /home/example/public_html;
index index.html index.htm index.php;
# SSL Stuff here
#include conf.d/wp/restrictions.conf;
#include conf.d/wp/wordpress.conf;
}