redirect - 域的 SSL,包括子域

标签 redirect ssl nginx

我有一个域,example.com。由此,我有这些:

  • blog.example.com
  • api.example.com
  • books.example.com

我已获得根域及其子域的 SSL 证书。

我希望重定向是这样的:

这是我的 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;
}

This link helped

关于redirect - 域的 SSL,包括子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34983456/

相关文章:

c - 将 stdout/stderr 重定向到 C 中的函数

php - 跟踪非垃圾邮件的电子邮件点击次数

tomcat - Nginx 反向代理单个 tomcat 实例上的多个应用程序

logging - Windows 中的 Nginx $request_time 和 $upstream_response_time

header 重定向后 PHP $_SESSION 为空

mysql - 通过 SSL 连接到 MySQL 时出现错误 2026 (HY000) : SSL connection error: protocol version mismatch

java - "bad record MAC"Java 和 PostgreSQL 之间的 SSL 错误

php - HTTPS 子域将页面重定向到主域

node.js - Dockerized NGINX 配置与在 Azure 上运行的 ReactJS 应用程序(容器实例)

php - 最好的重定向方法?