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;
...
}