Nginx配置PC端和移动端自动跳转
一、域名准备阶段
客户端 | 域名 | 描述 |
---|---|---|
pc端 | www.yxf.com | 用于pc端访问的域名 |
移动端 | m.yxf.com | 用于移动端访问的域名 |
问题描述:pc端不管是访问www.yxf.com域名还是m.yxf.com域名都需要跳转到www.yxf.com域名下。
移动端不管是访问 m.yxf.com还是www.yxf.com下都需要要跳转到 m.yxf.com域名下
二、下面我们就来配置nginx
- pc端nginx的conf配置
server {
listen 443;
server_name www.yxf.com;
ssl on;
ssl_certificate cert/common.pem;
ssl_certificate_key cert/common.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
rewrite ^(.*) http://m.yxf.com$1 permanent;
}
location / {
root /home/yxf/pc;
index index.html;
}
}
- 移动端的nginx的conf配置
server {
listen 443;
server_name m.yxf.com;
if ($http_user_agent !~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
rewrite ^(.*) https://www.yxf.com$1 redirect;
}
ssl on;
ssl_certificate cert/common.pem;
ssl_certificate_key cert/common.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
location / {
root /home/yxf/mobile;
index index.html;
}
}
上述需要注意的是,如果想让pc 跳转到移动 或者移动跳转到 pc 是302 临时重定向,可以修改 permanent 为 redirect
- redirect – 返回临时重定向的HTTP状态302
- permanent – 返回永久重定向的HTTP状态301