nginx配置daxiangshouche.com重定向到www.daxiangshouche.com和https

nginx配置daxiangshouche.com重定向到www.daxiangshouche.com和https

通过nginx配置两个server,分别指定server_name daxiangshouche.com,server_name www.daxiangshouche.com
在server_name daxiangshouche.com中,通过301重定向将请求重定向到https://www.daxiangshouche.com

另外可通过判断$http_x_forwarded_proto协议,把http请求重定向到https

效果如下:

大象收车-报废车,就上大象收车

server {
    listen 80;
    server_name daxiangshouche.com;
 
    if ($http_x_forwarded_proto = http){
        return 301 http://www.daxiangshouche.com;
    }
    return 301 https://www.daxiangshouche.com;
 
}
 
server {
    listen 80;
    server_name www.daxiangshouche.com;
    root /data/web;
 
    client_max_body_size 5m;
 
    if ($http_x_forwarded_proto = http){
        return 301 https://$server_name$request_uri;
    }
 
    if (-f $request_filename/index.html){
        rewrite (.*) $1/index.html break;
    }
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff"
 
    set $temp_request_id $http_x_request_id;
    if ($temp_request_id = "") {
        set $temp_request_id $request_id;
    }
    add_header X-Request-ID $temp_request_id;
    proxy_set_header X-Request-ID $temp_request_id;
 
    # Log files for Debugging
    access_log /data/log/nginx/web-access.log;
    error_log /data/log/nginx/web-error.log;
 
 
    index index.html index.htm;
 
    charset utf-8;
 
 
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
 
    error_page 404 /404.html;
    location ~ /\.(?!well-known).* {
        deny all;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值