这是我的场景:
两个网站:
https://example.com http://dev.example.com 站点1有一个ssl证书。
网站2没有。
(换句话说,我没有通配符ssl证书。)
通过Nginx我试图build立必要的www和HTTPredirect。
网站1的目标是指导
example.com , www.example.com和https://www.example.com
…至
https://example.com
站点2的目标是简单地让http://dev.example.com直接指向自己。
我为目标1尝试了什么
# SSL Redirect server { listen 80; server_name example.com; return 301 https://filethemes.com$request.com; } # WWW Redirect server { listen 80; listen 443 ssl; server_name www.example.com ssl_certificate <my-path-here>.crt; ssl_certificate_key <my-path-here>.key; return 301 https://example.com$request_uri } # Site's Primary Server Block server { listen 443 ssl; root /var/www/example.com; index index.html; server_name example.com; ssl_certificate <my-path-here>.crt; ssl_certificate_key <my-path-here>.key; }
通过上面的代码, example.com转到“ https://example.com ”,但就是这样。 wwwredirect不起作用。 任何想法,为什么?
另外,对于站点2(子域dev.example.com )的简单服务器块,它将redirect到https://example.com 。 所以,我似乎无意中将我的子域redirect到主域。 想法?
我知道已经有一大堆Nginx的问题和答案了,但是我找不到任何解决scheme来帮助我按照需要进行工作。
对于这样一个看起来很简单的目标来说,这令人吃惊,这是多么具有挑战性。
我很感激任何人的洞察力可以提供。
PS:是的,我的SSL证书支持www和非www。
更新:
子域问题(上面的网站2)不再是一个问题…我没有重新链接适当的文件从网站 – 可用到网站启用。
仍然有与站点1相同的问题不redirect从www – 裸体, http和https 。
解决了(!)
问题? 没有DNS CNAME条目(面对手掌!)。 非常感谢@ BE77Y帮助我find问题。 另外,万一它帮助别人,这是我目前和完全工作的nginxconfiguration,由两个服务器块组成,一个负责所有redirect到非www和https(并使用NGINX最佳实践,写和否if语句):
# MAIN SERVER BLOCK server { listen 443 ssl; root /var/www/example.com; index index.html; server_name example.com; ssl_certificate <my-path-here>.crt; ssl_certificate_key <my-path-here>.key; } # REDIRECTS (send everything to non-www https) server { listen 80; listen 443; server_name www.example.com example.com; ssl_certificate <my-path-here>.crt; ssl_certificate_key <my-path-here>.key; return 301 https://example.com$request_uri; }
陷阱(对于那些通常不使用服务器的人):
你几乎可以看到我所能看到的; 我只是稍微不同的configuration(根据您描述的要求),只是为了简化一些事情。 有一点我要说明的是,你似乎没有在你的configuration中包含一个dev.example.com段?
无论如何,以下是我将如何改善您的当前configuration:
# Primary block including SSL redirect server { listen 80; listen 443 ssl; server_name example.com; root /var/www/example.com; index index.html; ssl_certificate <my-path-here>.crt; ssl_certificate_key <my-path-here>.key; if ( $scheme = http ){ rewrite ^ https://$server_name$request_uri? permanent; } } # WWW Redirect server { listen 80; listen 443 ssl; server_name www.example.com ssl_certificate <my-path-here>.crt; ssl_certificate_key <my-path-here>.key; rewrite ^ https://example.com$request_uri? permanent; }
澄清以上内容:这是一种“干净”的做事方式; 如果有人在http://example.com请求您的网站,他们将通过if语句redirect到https://example.com ,同样,如果他们通过https请求网站,他们将被正确地提供给网站一个问题与以前的单独陈述)。 如果他们尝试通过http://www.example.com访问该网站,则会被redirect,或者通过https://www.example.com正确地协商SSL,但仍然如上所述redirect。
正如我注意到的,你没有在你的configuration文件中包含任何与dev.example.com相关的段 – 我想这是因为你已经有这些工作,但如果没有,请随时评论这一点,我会乐于协助将他们包括在内。
希望有所帮助!