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 ?


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/

A very late reply. this might help someone

try proxy_pass /myApp/ /location1 /location2;

Each location separated with space.

链接地址: http://www.djcxy.com/p/32542.html

上一篇: Cloudflare灵活的ssl问题?

下一篇: 如何在nginx中指向代理服务器的许多路径