Nginx 跳转到www二级域名,域名重定向配置方法。
阿里(万网)支持显性URL配置,也就是:
将域名指向一个http(s)协议地址,访问域名时,自动跳转至目标地址(例如:将sojson.com显性转发到www.sojson.com后,访问sojson.com时,地址栏显示的地址为:www.sojson.com)。
其实就是重定向,我以前觉得阿里的挺好用的,今天阿里给我说服务器给攻击了,不提供服务。
下面是我和阿里的客服对话,我对阿里的服务表示有点失望。
我们使用自己的Linux中用Nginx实现还是比较靠谱。我们来看看Nginx配置。
Nginx重定向配置
所有非www二级域名都跳转到www.sojson.com下去。
#所有非www.sojson.com开始的域名,都重定向到www.sosjon.com下去。
if ($http_host !~ "^www.sojson.com$") {
rewrite ^(.*) https://www.sojson.com$1 permanent;
}
一级域名跳转到www二级域名下面去。
#只是一级域名“sojson.com” 跳转到www.sojson.com去。
if ($http_host ~ "^sojson.com$") {
rewrite ^(.*) https://www.sojson.com$1 permanent;
}
Nginx config配置位置参考:
server{
listen 80;
server_name www.sojson.com admin.sojson.com sojson.com;
index index.html;
access_log syslog:server=127.0.0.1,facility=local6,tag=nginxlog_access,severity=notice main;
error_log syslog:server=127.0.0.1,facility=local6,tag=nginxlog_error;
#我在这里
if ($http_host ~ "^sojson.com$") {
rewrite ^(.*) https://www.sojson.com$1 permanent;
}
location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
access_log off;
expires 7d;
}
location ~* \/(sitemap|robots)\.(html|txt|xml|xsl)$ {
root /data/www/sojson.com/sitemap;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
if ($host ~* www\.sojson\.com) {
proxy_pass https://www.sojson.com:8888;
}
if ($host ~* admin\.sojson\.com) {
proxy_pass http://admin.sojson.com:8888;
}
}
}
版权所属:SO JSON在线解析
原文地址:https://www.sojson.com/blog/178.html
转载时必须以链接形式注明原始出处及本声明。
本文主题:
如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。