我想在我的 Nginx 服务器上将 domain.com 重定向到 www.domain.com。但是我的配置不起作用,并在互联网浏览器中显示错误消息:
页面未正确重定向
我添加了此行来执行重定向:
return 301 $scheme://www.s1biose.com$request_uri;
如何纠正这个错误?这是我的配置:
server {
#listen 80;
#listen [::]:80 ipv6only=on;
server_name s1biose.com www.s1biose.com;
return 301 $scheme://www.s1biose.com$request_uri;
root /var/www/www-s1biose-com/web;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(txt|log)$ {
allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
location ~ ^/sites/[^/]+/files/.*\.php$ {
deny all;
}
location ~* ^/.well-known/ {
allow all;
}
location ~ (^|/)\. {
return 403;
}
location / {
try_files $uri /index.php?$query_string;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ /vendor/.*\.php$ {
deny all;
return 404;
}
location ~ '\.php$|^/update.php' {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
location ~ ^/sites/.*/files/styles/ {
try_files $uri @rewrite;
}
location ~ ^(/[a-z\-]+)?/system/files/ {
try_files $uri /index.php?$query_string;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
try_files $uri @rewrite;
expires max;
log_not_found off;
}
listen [::]:443 ssl http2 ipv6only=on;
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/s1biose.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/s1biose.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
if ($host = www.s1biose.com) {
return 301 https://$host$request_uri;
}
if ($host = s1biose.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
server_name s1biose.com www.s1biose.com;
return 404;
}
答案1
无论是否使用 www 访问网站,您的“return 301”行都会运行,导致浏览器无限重定向。要仅为 site.com 返回 301,请从主服务器块中删除“return 301”,只让它监听www.site.com,并为 site.com 创建单独的服务器块。
server {
#listen 80;
#listen [::]:80 ipv6only=on;
server_name www.s1biose.com;
root /var/www/www-s1biose-com/web;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(txt|log)$ {
allow 192.168.0.0/16;
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
location ~ ^/sites/[^/]+/files/.*\.php$ {
deny all;
}
location ~* ^/.well-known/ {
allow all;
}
location ~ (^|/)\. {
return 403;
}
location / {
try_files $uri /index.php?$query_string;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ /vendor/.*\.php$ {
deny all;
return 404;
}
location ~ '\.php$|^/update.php' {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
location ~ ^/sites/.*/files/styles/ {
try_files $uri @rewrite;
}
location ~ ^(/[a-z\-]+)?/system/files/ {
try_files $uri /index.php?$query_string;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
try_files $uri @rewrite;
expires max;
log_not_found off;
}
listen [::]:443 ssl http2 ipv6only=on;
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/s1biose.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/s1biose.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
# redirect to www
listen [::]:443 ssl;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/s1biose.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/s1biose.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
server_name s1biose.com;
return 301 https://www.s1biose.com$request_uri;
}
server {
# force https
listen [::]:80 ipv6only=on;
listen 80;
server_name s1biose.com www.s1biose.com;
return 301 https://www.s1biose.com$request_uri;
}