简体   繁体   中英

Nginx - if $request_uri exists proxy_pass one url, otherwise another url

I want to handle 2 cases: test.example.com and test.example.com/ABC .

  1. If the entered url is the base domain ( test.example.com ), I want to proxy_pass a given endpoint (Let's say example.com/home ).

  2. If test.example.com/ABC is given, I want to proxy_pass to example.com/confirm/ABC

I made the (1) work like so:

server {
     listen 443 ssl;
     listen [::]:443 ssl;

     server_name test.example.com;

     location / {
         proxy_pass https://example.com/home;
     }
}

But I couldn't figure out how to say "If $request_uri exists, proxy_pass to different endpoint". I tried:

location / {
    if ($request_uri) {
       proxy_pass https://example.com/confirm/$request_uri;
    }
 
    proxy_pass https://example.com/home;
}

How can I achieve this?

$request_uri always exists and is never empty. The URI for the root of your domain is a single / (even if it's not displayed in the browser's address bar).

The location block which matches that URI is location = / . See this document for details.

For example:

location = / {
    proxy_pass https://example.com/home;
}
location / {
    proxy_pass https://example.com/confirm/;
}

The first location block only matches the root URI / , and the second location block matches any other URI. The remainder of the URI is automatically appended to /confirm/ . See this document for details.

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