Я хочу перенаправить domain.com на www.domain.com на моем сервере Nginx. Но моя конфигурация не работает и отображает сообщение об ошибке в интернет-браузере:
Страница перенаправляется неправильно
Я добавил эту строку, чтобы выполнить перенаправление:
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;
}
Ваша строка «return 301» выполняется независимо от того, осуществляется ли доступ к сайту с использованием www или нет, что приводит к бесконечному перенаправлению браузера. Чтобы вернуть 301 только для site.com, удалите «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;
listen 443;
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;
}