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;
}
}