Nginx:如果位置匹配,则仅使用服务器块

时间:2016-10-12 08:37:09

标签: redirect nginx

我有一个像这样的所有服务器块:

server {
    listen      80 default_server;
    server_name _;

    location /blog{
        # pass request to ghost
    }
    location /{
        # pass request to custom node app
    }
}

它传递给自定义节点应用程序,该应用程序验证请求的域,协议和路径,并在必要时发出单个301重定向;我们这样做是为了最小化301重定向,用于搜索引擎优化目的。

我还需要我的幽灵博客才能在https://www.exmaple.com/blog投放。我添加了以下块:

server {
    listen      80;
    server_name example.com;

    location /blog {
        return 301 https://www.example.com$request_uri;
    }
}

以便对裸域的请求将被重定向。但现在对example.com的请求返回默认的Nginx index.html页面。我该如何预防呢?我想避免使用if

1 个答案:

答案 0 :(得分:2)

您需要在裸域服务器块中具有路由到节点应用程序

的全部捕获
server {
    listen      80;
    server_name example.com;

    location /blog {
        return 301 https://www.example.com$request_uri;
    }

    location /{
        # pass request to custom node app
    }
}
相关问题