【问题标题】:Nginx redirect a specific subdomain to specific locationNginx 将特定子域重定向到特定位置
【发布时间】:2023-02-25 05:55:47
【问题描述】:

我希望这样当用户键入 task-manager.example.com 时,他们会自动重定向到 location /api/ ,而不必键入 task-manager.example.com/api 的完整路径。只有 task-manager.example.com 应该被重定向到 location /api/

对于example.comwww.example.com,我希望所有请求都定向到location /,无论用户是否键入example.com/api。对example.com/apiwww.example.com/api的任何请求都应自动重定向到example.com/www.example.com/

是否可以配置 Nginx 来实现此功能?

这是我当前的配置:

server
{
    server_name example.com www.example.com task-manager.example.com;

    location /
    {
        # Frontend application
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://localhost:9091;
    }

    location /api/
    {
        # Backend application
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://localhost:9090;
    }

    # some Certbot SSL configuration ...
}

server
{
    if ($host = www.example.com) {
        return 301 https://$host/;
    } # managed by Certbo


    if ($host = example.com) {
        return 301 https://$host/;
    } # managed by Certbo


    if ($host = task-manager.example.com) {
        return 301 https://$host/api;
    } # managed by Certbo


    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com task-manager.example.com;
    return 404; # managed by Certbot
}

【问题讨论】:

    标签: nginx nginx-reverse-proxy nginx-config nginx-location


    【解决方案1】:

    我能够通过将 if conditions 移动到相关的 location blocks 来解决我的问题。这允许我为特定主机指定重定向以转到特定 URI。

    这是我的新配置:

    server
    {
        server_name example.com www.example.com task-manager.example.com;
    
        location /
        {
            if ($host = task-manager.example.com) {
                return 301 /api;
            }
    
            # Frontend application
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
    
            proxy_pass http://localhost:9091;
        }
    
        location /api/
        {
    
            if ($host = www.example.com) {
                return 301 /;
            }
    
            if ($host = example.com) {
                return 301 /;
            }
    
            # Backend application
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
    
            proxy_pass http://localhost:9090;
        }
    
        # here is usually some Certbot SSL configuration ...
    }
    
    server
    {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name example.com www.example.com task-manager.example.com;
        return 301 https://$host$request_uri;
    }
    
    

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 2022-01-11
      • 2017-04-12
      • 2021-11-03
      • 2015-07-29
      • 2017-06-15
      • 2017-02-20
      • 2019-12-23
      • 1970-01-01
      相关资源
      最近更新 更多