[英]Redirect https from non www to www
請在您的nginx.conf文件中使用以下配置。 它對我來說很好。
server {
listen 80;
server_name www.example.com example.com;
location / {
index index.html;
root /usr/share/nginx/html; #the location of your app folder
}
if ($http_x_forwarded_proto = 'http') {
return 301 https://www.example.com$request_uri;
}
}
如果您只想重定向GET請求並為非GET請求拋出405(不允許使用方法):
server {
listen 443 ssl;
server_name example.com;
if ($request_method = GET) {
return 301 https://www.example.com$request_uri;
}
return 405;
}
server {
listen 443 ssl;
server_name www.example.com;
# locations
}
如果您希望所有請求(GET,POST,...)都被重定向,這是不可能的,那么我建議您嘗試使用Akshath的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.