简体   繁体   中英

Nginx rule for specific URL's

I have this url group on my website

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

...

In total, are 5561 different urls. There is a way to send the same html file for all of those urls? But, there is some others URL that must forwarded to my nodejs server, like this ones:

    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

How can I set a Nginx pattern to serve the same html file for the first group of urls (5561 different urls), and forward to my nodejs server the second group?

Here's one way using a map:

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;       
    }
}

Here's another using proxy_pass in prefix locations:

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;       
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM