Score:0

Nginx location matching order

th flag

I read some articles on nginx matching and they said the longer location block should be matched first.

However, when I try to set up a proxy for two locations, the first location pics up all traffic and the second does not get any of it.

This is my server config.

server {
listen 80;
server_name IP;
#return 301 https://$host$request_uri;
# nginx/sites-available/fdp.conf

location /lls/ {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass_request_headers on;
    proxy_pass http://IP:8080;
}

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass_request_headers on;
    proxy_pass http://IP:8000/;
}
}

Why does nginx not redirect the traffic to the /lls proxy? I also tried to use regex location ~ /lls but also no luck here. Both applications run when I visit the ports on the IP.

I have an application running under port 8000. My goal is to add a second application running under /lls. For this I want all traffic to go to port 8000 unless /lls is in the url, then I want all traffic to go to the 2nd application on port 8080. In the current situation traffic intended for port 8080 is picked up by port 8000.

Output of curl -v IP/lls:

*   Trying IP:80...
* TCP_NODELAY set
* Connected to IP (IP) port 80 (#0)
> GET /lls HTTP/1.1
> Host: IP
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 400
< Server: nginx/1.18.0 (Ubuntu)
< Date: Sat, 09 Oct 2021 08:41:47 GMT
< Content-Type: text/turtle
< Transfer-Encoding: chunked
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: Origin,Authorization,Accept,Content-Type
< Access-Control-Expose-Headers: Location,Link
< Allow: GET,POST,PUT,PATCH,DELETE
< Access-Control-Allow-Methods: GET,POST,PUT,PATCH,DELETE
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
<
* Connection #0 to host IP left intact

Output of lsof -Pi :8080

COMMAND       PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
docker-pr 2698710 root    4u  IPv4 8351515      0t0  TCP *:8080 (LISTEN)
docker-pr 2698721 root    4u  IPv6 8351519      0t0  TCP *:8080 (LISTEN)

Output of lsof -Pi :8000

 COMMAND       PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
docker-pr 2615262 root    4u  IPv4 7781060      0t0  TCP *:8000 (LISTEN)
docker-pr 2615267 root    4u  IPv6 7781066      0t0  TCP *:8000 (LISTEN)
cn flag
Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/130389/discussion-on-question-by-moopsish-nginx-location-matching-order).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.