S
S
Sergey2014-08-08 09:35:29
Nginx
Sergey, 2014-08-08 09:35:29

How to use URI in proxy_pass for @location?

Seems like the simplest config, but

server {
    listen 80;
    server_name "test";
    
    location @remote {
        proxy_pass                  https://someremoteserver/api/;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For f$proxy_add_x_forwarded_for;
        proxy_set_header        Host $http_host;
        proxy_next_upstream  error timeout http_404 http_502 http_504;
    }

    location / {
        root /home/www/test/;
        index index.html;
        try_files $uri $uri/ @remote;
    }
}

does not work. The error
"proxy_pass" cannot have URI part in location given by regular expression...
"/api/" cannot be removed. Although without it the error disappears.
How to redirect request to api?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2014-08-08
@ua9msn

You can just change $uri :

location @remote {
    rewrite (.*) /api$1 break;
    proxy_pass https://someremoteserver;
    ...
}

Here's what's in the documentation
If the URI is changed inside the proxied location using the rewrite directive, and the request will be processed with this configuration (break):
location /name/ {
        rewrite    /name/([^/]+) /users?name=$1 break;
        proxy_pass http://127.0.0.1;
    }

In this case, the URI specified in the directive is ignored, and the entire modified request URI is sent to the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question