Nginx SSL /子域/ IP重定向

时间:2015-07-21 00:26:48

标签: nginx centos7

目前我正在运营一个网站 centOS7

上的 nginx / 1.6.3

除了一些重定向外,一切都顺利进行。

这就是我的.conf文件:

server {
  listen 443 ssl spdy default deferred;
  server_name .example.com;

  ... more configs

  }


server {
  listen 80;
  server_name .example.com;
  return 301 https://example.com$request_uri;
}

我想要完成的是以下场景:

user visits in browser              | this should happen
------------------------------------|-------------------------------------
https://example.com$request_uri     | Just deliver content
https://*.example.com$request_uri   | 301 https://example.com$request_uri
https://123.123.123.123$request_uri | 301 https://example.com$request_uri
http://example.com$request_uri      | 301 https://example.com$request_uri
http://*.example.com$request_uri    | 301 https://example.com$request_uri
http://123.123.123.123$request_uri  | 301 https://example.com$request_uri

2 个答案:

答案 0 :(得分:1)

请检查以下配置运行,这应该有效。

#This would serve all your content.
server {
  listen 443 ssl spdy default deferred;
  server_name example.com;

  ... more configs

}

#https calls to anything except example.com would be redirected here    
server {
  listen 443 ssl spdy default deferred; #(Can also use only : "listen 443;")
  server_name *.example.com 123.123.123.123;
  return 301 https://example.com$request_uri;
}

#All port 80 redirection to https://example.com
server {
  listen 80;
  server_name example.com *.example.com 123.123.123.123;
  return 301 https://example.com$request_uri;
}

答案 1 :(得分:0)

我用这种模式解决了类似的问题:

server_name   ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
if ($domain != 'example.com') {
        rewrite ^/(.*)$  http://${subdomains}example.com/$1  permanent;
}

等等