首页 > 解决方案 > 如何在 NGINX 中将 http 指向 https 子域?

问题描述

我正在开发一个 Spring Boot 应用程序,它为 3 种类型的用户提供内容。所有三个用户都“生活”在同一个应用程序中。我想将 NGINX 配置为 1)将所有 http 重定向到 https 和 2)重定向流量,如下所示:

http 到https://www.example.com

http://b2b.example.comhttps://b2b.example.com/b2b(理想情况下不显示“/b2b”。这里所有的 b2b spring boot 端点都在监听)

到目前为止,这是我的 NGINX 配置:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        return 301 https://$host$request_uri;
}

server {

    listen 443;
    server_name example.com www.example.com;

    ssl on;
    ssl_certificate ...;
    ssl_certificate_key ...;
    ssl_session_cache shared:SSL:10m;

    access_log ...;
    error_log ...;

    location / {

        proxy_pass http://localhost:5050;
        proxy_set_header Host $host;

        # re-write redirects to http as to https, example: /home
        proxy_redirect http:// https://;
    }
}

server {

    listen 443;
    server_name b2b.example.com;

    ssl on;
    ssl_certificate ...;
    ssl_certificate_key ...;
    ssl_session_cache shared:SSL:10m;

    access_log ...;
    error_log ...;
    location / {

        proxy_pass http://localhost:5050/b2b;
        proxy_set_header Host $host;

        # re-write redirects to http as to https, example: /home
        proxy_redirect http:// https://;
    }
}

在 Sring Boot 方面,所有 B2B 端点都在侦听以“B2B”开头的模式。例如,这些用户的登录页面是 .../B2B/login。现在,如果我访问 b2b.example.com,我会被重定向到 b2b.example.com/B2B/login。我想要的是浏览器显示“B2B.example.com/login”并显示“/B2B/login”页面。所有在 URL 中省略“/B2B”部分的 B2B 网站。

标签: springspring-bootnginx

解决方案


推荐阅读