Score:0

nginx regular expression reverse proxy location

eg flag

I try to create a location based reverse proxy.
My config does not work as expected.

It should pass every request that is made to /api & /auth to the backend, and everything else to the frontend server.

server {

 listen 80;
 server_name 127.0.0.1 localhost;

 # remove server version
 server_tokens off;

 try_files $uri $uri/ =404;

 autoindex off;

 location = /favicon.ico { access_log off; log_not_found off; }
 location = /robots.txt  { access_log off; log_not_found off; }

 location ~ ^/(api|auth)/ {
     proxy_pass http://127.0.0.1:8080/$1;
     proxy_http_version 1.1;
     proxy_set_header X-Forwarded-Proto $scheme;
     proxy_set_header Connection $connection_upgrade;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

 location ~ ^/(.*) {
     proxy_pass http://127.0.0.1:3000/$1;
     proxy_http_version 1.1;
     proxy_set_header X-Forwarded-Proto $scheme;
     proxy_set_header Connection $connection_upgrade;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }

}

The request to http://example.com/ seems to work correctly and it serves the frontend, but a request to /api or /auth returns a 404 error from the backend.

When i look on the reuqest URL that receives the backend, its always / and thats not correct.

What do i wrong?

EDIT: I got it working with the following changes:

    location ~ /(api|auth)/(.*)$ {
        proxy_pass http://127.0.0.1:8080/$1/$2;
        ...
    }

    location ~ /(.*)$ {
        proxy_pass http://127.0.0.1:3000/$1;
        ...
    }
us flag
Are you sure the backend is expecting `/api` and `/auth` as the path component for the requests? What is the output of `curl http://127.0.0.1:8000/api`?
Marc avatar
eg flag
@TeroKilkanen Yes, `/api` & `/auth` are part of the URL that the backend expect. `curl http://127.0.0.1:8080/api` i reach my backend app.
us flag
Please show the configuration as shown by `nginx -T` in your question.
Marc avatar
eg flag
@TeroKilkanen Thank you, i figured it out. I have edited the question and a possible solution added.
I sit in a Tesla and translated this thread with Ai:

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.