【问题标题】:nginx default redirects and custom redirectsnginx 默认重定向和自定义重定向
【发布时间】:2017-01-03 23:27:19
【问题描述】:

操作系统 debian 8; 我正在尝试编写 nginx 配置什么会 1) 将所有请求从根 domain.a 重定向到 domain.b 2) 将所有带有路由的请求从 domain.a/$1 重定向到域 domain.a/api/route/$1

我能够完成 2) 但是当我在浏览器中键入 domain.a 时,它会显示 nginx 默认页面。我希望将其转发到 domain.b

server {

    listen 80;

    server_name domain.a; 

#should redirect all other requests to domain.b , but it not happens
    return 301 domain.b;

# correctly redirects from domain.a to domain.b api
    location ~/(.*)$ {
        return 301 https://domain.b/api/route/$1;
    }

}

【问题讨论】:

    标签: regex redirect nginx routes


    【解决方案1】:

    您可以使用location = / 语法隔离/ URI。这可能对你有用:

    location = / {
        return 301 https://domain.b/;
    }
    location / {
        return 301 https://domain.b/api/route$uri;
    }
    

    详情请见this document

    【讨论】:

    • 非常感谢!有效!
    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2011-10-28
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多