我有一个域,example.com
。由此,我有这些:
- blog.example.com
- api.example.com
- books.example.com
我已获得根域及其子域的 SSL 证书。
我希望重定向是这样的:
- > http://example.com => https://example.com
- > http://blog.example.com => https://blog.example.com
- > http://books.example.com => https://books.example.com
这是我的 example.com
nginx conf
server {
listen 443 ssl default_server;
root /home/django/khophi;
index index.html index.htm;
server_name example.com;
include /etc/nginx/globalssl.conf; //ssl config
}
server {
listen 80 default_server;
server_name example.com;
return 301 https://$host$request_uri;
}
blog.example.com
的 nginx 配置文件
server {
listen 443 ssl; // now listens for https
root /var/www/html/blog;
index index.php index.html index.htm;
server_name blog.example.com;
#include /etc/nginx/globalssl.conf;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
// error pages
location ~ \.php$ {
//php specific things
}
}
server {
listen 80;
server_name blog.example.com;
return 301 https://blog.example.com$request_uri;
}
books.example.com
遵循与 blog.example.com
conf 类似的结构
以上设置会发生什么情况?
- 访问
http://example.com
重定向到https://example.com
(正如我所愿 - 访问
http://blog.example.com
重定向到https://blog.example.com
但是,显示页面https://example.com
的
就我而言,我想要:
example.com
保留为default_server
http://example.com
重定向到https://example.com
- ALL http://请求子域重定向到各自的 https://版本
- 如果请求的子域不存在,它应该重定向到 default_server ( https://example.com )
最佳答案
我没有使用 return
,而是在主域及其子域的重定向服务器 block 上使用 rewrite
,如下所示:
// blog.example.com
server {
listen 80;
server_name blog.example.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
//example.com
server {
listen 80 default_server;
server_name example.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
关于redirect - 域的 SSL,包括子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34983456/