gpt4 book ai didi

ubuntu - 如何在 Digital Ocean Ubuntu 服务器上将非 www 重定向到 www?

转载 作者:行者123 更新时间:2023-12-04 19:25:07 24 4
gpt4 key购买 nike

我托管在 Digital Ocean Ubuntu 服务器上。
我想将我的非 www 流量重定向到 www。全部使用https。
这是我的/etc/nginx/sites-available/default

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


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


listen 80 default_server;
listen [::]:80 default_server;

server_name test.com www.test.com;
return 404; # managed by Certbot
}
但是,它不起作用。有人可以帮忙吗?

最佳答案

你不应该使用'if'来监听请求。 https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/
Nginx 设置服务器手册:https://docs.nginx.com/nginx/admin-guide/web-server/web-server/
这是一个与您的设置相匹配的示例:

# catch all traffic to https://www.test.com
server {
listen 443 ssl http2;
server_name www.test.com;

#change this to your web root
root /home/test;

# specify index files
index index.php index.html index.htm;

#change this to your certificate paths and so on
ssl_certificate /etc/letsencrypt/live/test.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/test.com/privkey.pem;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

#this will look for a file, then a directory, finally for an index file, leave as is
location / {
try_files $uri $uri/ /index.php?$query_string;
}

#change this to match your php setup
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
}
}

# redirect all https traffic from non www tot www, you need a certificate here
server {
listen 443 ssl;
server_name test.com;
return 301 https://www.test.com$request_uri;

#change this to your certificate paths
ssl_certificate /etc/letsencrypt/live/test.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/test.com/privkey.pem;
}

# redirect all traffic from http to https
server {
listen 80;
server_name www.test.com test.com;
return 301 https://$host$request_uri;
}

关于ubuntu - 如何在 Digital Ocean Ubuntu 服务器上将非 www 重定向到 www?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71971151/

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