gpt4 book ai didi

ubuntu - Nginx 将 www 域重定向到非 www

转载 作者:行者123 更新时间:2023-12-04 18:33:21 27 4
gpt4 key购买 nike

我要www.example.app去 example.app 进行 SEO,我遵循了几个教程和问题解决方案,但似乎无法让它工作。
我添加了一个单独的服务器 block ,它重定向到非 www 域并多次重新启动 nginx,还从一个隐身窗口访问我的站点。
这是我的 /etc/nginx/sites-available/default配置文件。
任何帮助,将不胜感激!

server {
server_name www.example.app;
return 301 $scheme://example.app$request_uri;
}

server {
listen 80;
listen [::]:80;


root /var/www/html/;

index index.php index.html index.htm index.nginx-debian.html;

server_name exampe.app;

location / {
try_files $uri $uri/ =404;
}

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}

location ~* \.(pdf)$ {
expires 30d;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}

location ~ /\.ht {
deny all;
}

error_page 404 /customerror_404.html;
location = /customerror_404.html {
root /usr/shar/nginx/html;
internal;
}

}


server {
root /var/www/html/;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.app; # managed by Certbot


location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}

location ~ /\.ht {
deny all;
}


listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.app/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.app/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server {
if ($host = example.app) {
return 301 https://$host$request_uri;
} # managed by Certbot


listen 80 ;
listen [::]:80 ;
server_name example.app;
return 404; # managed by Certbot

}

最佳答案

server {
if ($host = www.example.app) {
return 301 https://$host$request_uri;
} # managed by Certbot


listen 80 ;
listen [::]:80 ;
server_name example.app;
return 404; # managed by Certbot

}
我使用它从 www 域重定向到非 www 域。
在这种情况下,您需要告诉 nginx 是否要从 www 重定向的主机。
如果这不起作用。您可能需要将服务器名称设置为这样
server_name www.example.app example.app;
这样主机就被认出来了。

关于ubuntu - Nginx 将 www 域重定向到非 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63881188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com