Nginx proxy pass with regex is not working - nginx

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.

Related

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

NGINX location block looking for url containing a #

Good afternoon,
I currently have a problem trying to proxy_pass to a url that contains a # sign. I'm assuming it's causing me issues since it's the comment character
the url that works is http://ip:port/remotelogin/#/
and I would like to show up as https://proxy ip/remotelogin/#/
location /remotelogin/#/ {
proxy_http_version 1.1;
proxy_pass "http://ip:port/";
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 http;
}
Just replace /remotelogin/#/ with /remotelogin/.

can we place proxy_set_header X-Forwarded-For after proxy_pass

I have a nginx as a reverse proxy in a containerised application. I have an issues in which when I add proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; my deployment does not work.
here is the part of location block in my nginx.conf file.
location #app {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header X-Real-IP $remote_addr;
}
but when i replace $proxy_add_x_forwarded_for with http_x_forwarded_for the deployment works but i am unable to pass the real client ip.
Can anyone please confirm if this is syntactically correct & we can add proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; after proxy_pass?
I can confirm that
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
is correct.
I have a running location block:
location / {
proxy_pass http://127.0.0.1:3333;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
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_set_header X-Forwarded-Port $server_port;
}

Allow access service / proxy_pass only from a certain URL in nginx

I have a website running at https://mywebsite.com. I also install another application (Minio) on the same server running at port 9000.
NOTE: When access Minio via myServerIP:9000, it automatically goes to myServerIP:9000/minio/. If I haven't logged in yet, then I will be redirected to myServerIP:9000/minio/login. If I go to myServerIP:9000/minio/anyRandomPath, I will also be redirected to the login page or another default page (if I have already logged in)
Now I want to access Minio via address https://mywebsite.com/files, so I setup my nginx as below
location /files/ {
proxy_pass http://127.0.0.1:9000/minio/;
proxy_set_header URI_REQUEST_ORIGIN $request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Proto $scheme;
}
This doesn't work (it shows a blank page with title "Minio login"). When I check the browser's console, it says there's an error message Uncaught SyntaxError: Unexpected token < with a link to https://mywebsite.com/minio/index_bundle_xxx.js. So I try adding another location block in my nginx.conf:
location /minio/ {
proxy_pass http://127.0.0.1:9000/minio/;
proxy_set_header URI_REQUEST_ORIGIN $request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Proto $scheme;
After that, it works as expected. But the problem is that now I can access Minio via both /minio and /files
Now how can I block direct access to Minio via https://mywebsite.com/minio?
I tried some workaround like adding below location blocks:
location =/minio/ {
return 404;
}
location =/minio/login/ {
return 404;
}
But this is clearly a bad way and also not efficient, since I can just go to mywebsite.com/minio/typeAnythingHere to be redirected to the login page.

NGINX match multiple paths with proxy pass

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.

Resources