Apache https://domain.com重定向到https://www
我的nginx配置主要使用https,所有从http到https的重定向都正常工作,除了从哪个需要重定向到哪个可以有人告诉我哪里出了问题 我曾尝试在服务器块中添加重定向,以侦听443 for example.com重定向到www.example.com,但我遇到了相同的问题Apache https://domain.com重定向到https://www,apache,nginx,configuration,Apache,Nginx,Configuration,我的nginx配置主要使用https,所有从http到https的重定向都正常工作,除了从哪个需要重定向到哪个可以有人告诉我哪里出了问题 我曾尝试在服务器块中添加重定向,以侦听443 for example.com重定向到www.example.com,但我遇到了相同的问题 server { server_name www.example.com; index index.php index.html index.htm; root /var/www/qc/public;
server {
server_name www.example.com;
index index.php index.html index.htm;
root /var/www/qc/public;
error_log /var/www/qc/error.log debug;
access_log /var/www/qc/access.log;
location / {
include /etc/nginx/mime.types;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
listen 443 ssl; # managed by Certbot
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt$
root /usr/share/;
}
}
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # m$
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; #$
}
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name example.com;
return 404; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # m$
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; #$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
server_name www.example.com example.com;
return 301 https://www.example.com$request_uri;
listen 80;
return 404; # managed by Certbot
}
我希望url重定向到example.com括号中的,您不需要“if($host=example.com){” 由于您已经通过设置“server_name example.com;”对进入此括号的主机进行了分隔。此外,当您重定向到https://$host$request_uri;,您将重定向到example.com,而不是www.example.com,从而创建了一个无限的重定向循环 这有帮助,这是example.com配置,应该可以工作:
server {
location / {
return 301 https://www.example.com$request_uri;
}
server_name example.com;
return 404; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # m$
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; #$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
这将创建一个循环:
if($host=example.com){return301 https://$host$request_uri;}
谢谢,我用return301替换了if块https://www.example.com$request_uri;server_name example.com;return 404;#由Certbot管理