Answer a question

I am trying to exclude some paths in my nginx proxypass and want everything else to go to my proxypass.

i.e I dont want to give proxy_pass to any url which starts with 'tiny' or 'static', but want everythign else to go to my proxypass location.

and I am using following regex to achieve this:

~ ^((?!tiny|static).)*$

But I always get 404 error. If I navigate to following url in browser

localhost:8080/xyz

I want it to go to

localhost:8000/api/tiny/records/xyz

Can someone please help me in pointing out what is the issue ?

Here is my full nginx conf file:-

server {
        listen       8080;
        server_name  localhost;
        location   ~ ^((?!tiny|static).)*$ {
            proxy_pass  http://localhost:8000/api/tiny/records/$1;
        }
        location   / {
            proxy_pass  http://localhost:8000;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

Thanks a lot.

Answers

You are missing a / and have the * in the wrong place. The regular expression should be:

^(/(?!tiny|static).*)$

But you do not need to use a regular expression with a negative lookahead assertion. Instead, place a normal regular expression on the other location block.

For example:

location / {
    proxy_pass  http://localhost:8000/api/tiny/records/;
}
location ~ ^/(tiny|static) {
    proxy_pass  http://localhost:8000;
}
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐