NGINX match multiple paths with proxy pass - nginx

I'm trying to pass multiple locations to a proxy, though, I can simply not make it work. Can anybody point me in the right direction?
This is what I have so far:
location / {
try_files $uri $uri/ /index.html =404
gzip on;
}
location ~* ^/(login|callback|ph|ch|th) {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "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;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://127.0.0.1:4002;
}
As you probably can tell, I'm trying to pass
/login
/callback
/ph
/ch
/th
to localhost on port 4002, but it's not passing them?
EDIT: If it's of any help. I think my front-end it hijacking the path-location? Not sure though.

All requests are starts from /, so your location should look like this:
location ~* ^/(login|callback|ph|ch|th) {
if you use start string symbol.

Related

Nginx proxy pass with regex is not working

Essentially, I would like to be able to proxy https://test.myurl.com/proxy/12345 to https://12345.myurl.com:8443, however, I would like 12345 to be arbitrary.
I have been able to get the proxy pass working with the following (i.e. statically assigned):
location /proxy/12345/ {
proxy_pass https://12345.myurl.com:8443/;
proxy_redirect off;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header Host "test.myurl.com";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
However, making it arbitrary does not seem to work:
location ~* ^/proxy/([0-9]+)/ {
proxy_pass https://$1.myurl.com:8443/;
proxy_redirect off;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header Host "test.myurl.com";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
While Nginx accepts my configuration, I get a 404 Not Found. I have looked at a number of other related questions and played around with the config to no avail.

Nginx changes POST to GET using proxy_pass

I want to use Nginx create a gateway to receive requests and pass them along to a network of microservices.
What I need Nginx to do is just act as a proxy server, taking the requests, passing them along to whatever service, and returning the response without any changes.
This is my configuration for my local setup:
server {
listen 8080;
location /api/register/ {
proxy_pass http://micro_auth_backend:8082;
}
location /api/location/ {
proxy_pass http://localhost:8088;
}
}
It works correctly for GET requests, but when doing a POST call, the server will always receive a GET response.
I have tried adding some more configs inside the location, such as this example below, but so far nothing has worked:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Any suggestions would be appreciated.
Thank you
Just removed the trailing slash on location:
location /api/register {
proxy_pass http://micro_auth_backend:8082;
}
Now it works.
you can add this code to your nginx.conf file. It works for me.
location /api/register/ {
proxy_pass http://localhost:8082;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api/location/ {
proxy_pass http://localhost:8088;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

How to configure nginx to redirect with subpaths matching given pattern

I am using nginx as a reverse proxy. I want to redirect locations matching some patterns keeping exactly the same subpaths. For example, this configuration does not work for me:
location ~ ^/bpmn/?(.*)$ {
resolver 127.0.0.11 ipv6=off;
set $upstream activiti:8080;
proxy_pass http://$upstream/$1;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
I would like to redirect the location matches the path "/bpmn/a/b/c" to "http://$upstream/a/b/c". My configuration works only for one level (e.g. "bpmn/a").
Thanks a lot for your help,
Regards.

how to config nginx when the server make redirect

I configured my Nginx to locate /doc to one of my local servers, just like this:
location /doc {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:8000;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-Proto $scheme;
}
Then my server on 8000 port will make a redirect to /message/html
This is the problem...
The correct URL should be /doc/message/html
How can I config my Nginx to redirect the URL of /redirected_by_port_8000_server to /doc/redirected_by_port_8000_server in the location /doc block
You have to rewrite the URI. In nginx you will be able to pass /doc at the beginning of the URI by adding / at the end of the proxy_pass statement. It looks like below. Such that http://whatever/doc -> http://127.0.0.1:8000/doc
proxy_pass http://127.0.0.1:8000/;
However, the next proxy should rewrite by itself the /doc path and add it at its beginning. In nginx it would look like.
location /doc {
rewrite ^/(.*) /$1/message/html/ break;
proxy_pass http://127.0.0.1:8000/;
}
Many thanks to #Richard Smith.
I used curl -I checked the redirect location and changed my Nginx config file below:
location /doc {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:8000;
proxy_redirect https://$http_host https://$http_host/doc; #this will rewrite the redirect Location header
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-Proto $scheme;
}

nginx rewrite rule for redirection

I have two apps running on host1:7000 and host2:7000. I am fronting the two hosts by an nginx reverse proxy, where I want mydomain.com/admin to point to host1:7000/portal and mydomain.com/user to host2:7000/portal.
I have written the following config
listen 80;
server_name mydomain.com *.mydomain.com;
location ~ ^/admin/(.*)$ {
proxy_pass $scheme://<IP-ADDRESS>/$1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
I can get to mydomain.com/admin to be redirected to host1:7000/portal but when the app redirects from host1:7000/portal on to host1:7000/login via relative path, in the browser I see mydomain.com/login. What do I need to do to get the second redirect go mydomain/admin/login?
Why do people use regexps for no reason and have all kind of problems with it?…
location /admin/ {
proxy_pass http://host1:7000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
This will automatically strip /admin/ from proxied request and prepend it in Location header (which is used in redirect).
See proxy_pass and proxy_redirect docs.

Resources