Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to point many paths to proxy server in nginx

I'm trying to set nginx location that will handle various paths and proxy them to my webapp.

Here is my conf:

    server {

    listen          80;
    server_name     www.example.org;

    #this works fine
    location / { 
       proxy_set_header X-Forwarded-Host $host;
       proxy_set_header X-Forwarded-Server $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_pass http://localhost:8081/myApp/;
    }

    #not working
    location ~ ^/(.+)$ { 
       proxy_pass http://localhost:8081/myApp/$1;
    }
}

I would like to access myApp with various paths like: /myApp/ABC, /myApp/DEF, myApp/GEH or /myApp/ZZZ. Of course these paths are not available in myApp. I want them to point to root of myApp and keep url. Is that possible to archive with nginx ?

like image 632
elzix88 Avatar asked Nov 16 '25 10:11

elzix88


2 Answers

Nginx locations match in order of definition. location / is basically a wildcard location, so it will match everything, and nothing will reach the second location. Reverse the order of the two definitions, and it should work. But actually, now that I look at it more closely, I think both locations are essentially doing the same thing:

/whatever/path/ ->>proxies-to->> http://localhost:8081/myApp/whatever/path/
like image 197
David Eyk Avatar answered Nov 18 '25 20:11

David Eyk


A very late reply. this might help someone

try proxy_pass /myApp/ /location1 /location2;

Each location separated with space.

like image 29
Sibiraj Avatar answered Nov 18 '25 19:11

Sibiraj