Nginx
如何使用 Nginx 將 domain.com 重定向到 www.domain.com?
我想將 domain.com 重定向到我的 Nginx 伺服器上的 www.domain.com。但我的配置不起作用,並在 Internet 瀏覽器中顯示錯誤消息:
頁面未正確重定向
我添加了這一行來進行重定向:
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; }
無論是否使用 www 訪問該站點,您的“return 301”行都在執行,導致瀏覽器無休止地重定向。要只為 site.com 返回 301,請從主伺服器塊中刪除“返回 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; }