Nginx重定向配置

所有非www二级域名都跳转到www.c n.com下去。


  1.  

    #所有非www. cn.com开始的域名,都重定向到www.c n.com下去。
  2.  

    if ($http_host !~ "^www.cn .com$") {
  3.  

    rewrite ^(.*) https://www. cn.com$1 permanent;
  4.  

    }

一级域名跳转到www二级域名下面去。


  1.  

    #只是一级域名“c n.com” 跳转到www.c n.com去。
  2.  

    if ($http_host ~ "^c n.com$") {
  3.  

        rewrite  ^(.*)    https://www.c n.com$1 permanent;
  4.  

    }

Nginx config配置位置参考:


  1.  
     
  2.  

    server{
  3.  

    listen 80;
  4.  

    server_name www.c n.com admin.c n.com csdn.com;
  5.  

    index index.html;
  6.  

    access_log syslog:server=127.0.0.1,facility=local6,tag=nginxlog_access,severity=notice main;
  7.  

    error_log syslog:server=127.0.0.1,facility=local6,tag=nginxlog_error;
  8.  
     
  9.  

    #我在这里
  10.  

    if ($http_host ~ "^csdn.com$") {
  11.  

    rewrite ^(.*) https://www.c n.com$1 permanent;
  12.  

    }
  13.  
     
  14.  

    location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
  15.  

    access_log off;
  16.  

    expires 7d;
  17.  

    }
  18.  

    location ~* \/(sitemap|robots)\.(html|txt|xml|xsl)$ {
  19.  

    root /data/www/c n.com/sitemap;
  20.  

    }
  21.  

    location / {
  22.  

    proxy_set_header Host $host;
  23.  

    proxy_set_header X-Real-IP $remote_addr;
  24.  

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  25.  
     
  26.  

    if ($host ~* www\.csdn\.com) {
  27.  

    proxy_pass https://www.c n.com:8888;
  28.  

    }
  29.  

    if ($host ~* admin\.c n\.com) {
  30.  

    proxy_pass http://admin.c n.com:8888;
  31.  

    }
  32.  
     
  33.  

    }