针对特定网址的Nginx规则

时间:2015-09-02 19:24:48

标签: url nginx url-rewriting proxy rule

我的网站上有这个网址组

mywebsite.com.br/

mywebsite.com.br/sao-paulo/sp

mywebsite.com.br/rio-de-janeiro/rj

mywebsite.com.br/natal/rn

mywebsite.com.br/aparecida-do-rio-doce/go

...

总共有5561个不同的网址。有一种方法可以为所有这些网址发送相同的html文件吗?但是,还有一些其他URL必须转发到我的nodejs服务器,如下所示:

    mywebsite.com.br/update-password/1234

    mywebsite.com.br/update-password/

    mywebsite.com.br/user/confirm

    mywebsite.com.br/user/confirm/123

    mywebsite.com.br/api/v1/auth/facebook

    mywebsite.com.br/api/v1/auth/local

    mywebsite.com.br/api/v1/user/new

    mywebsite.com.br/api/v1/user/statistics

如何设置Nginx模式为第一组网址(5561个不同的网址)提供相同的html文件,并将第二组转发到我的nodejs服务器?

1 个答案:

答案 0 :(得分:0)

这是使用地图的一种方式:

map $uri $forward2nodejs {
    ~^/update-password/ 1;
    ~^/user/confirm 1;
    ~^/api/v1/auth/ 1;
    ~^/api/v1/user/ 1;
}

server {
    server_name mywebsite.com.br;

    # default location for the 5561 different urls
    location / {
        try_files /default.html =404;
    }

    if ($forward2nodejs) {
        return 301 http://nodejs;       
    }
}

这是在前缀位置使用proxy_pass的另一个:

server {
    server_name mywebsite.com.br;

    # default location for the 5561 different urls
    location / {
        try_files /default.html =404;
    }

    location /update-password/ {
        include nodejs_proxy_pass;       
    }

    location /user/confirm {
        include nodejs_proxy_pass;       
    }

    location /api/v1/auth/ {
        include nodejs_proxy_pass;       
    }

    location /api/v1/user/ {
        include nodejs_proxy_pass;       
    }
}
相关问题