Nginx rewrite to proxy_pass server/path - nginx

Is it possible to use Nginx proxy_pass to rewrite URL as below:
location /foo {
proxy_pass http://external-server-IP:8080/some/path/;
}

Just in case someone still needs this, the easy way to do this:
location ~ ^/foo/.* {
rewrite ^/foo(.*) /$1 break;
proxy_pass https://external-server:8080/remote-path/;
}
rewrite ^/foo$ /foo/ redirect;
What this does is that it sends the request to the external server, masquerading it under your own domain.
rewrite ^/foo(.*) /$1 break;
The first rewrite is just to remove the added URL path (the remote server is not expecting that.
rewrite ^/foo$ /foo/ redirect;
And the 2nd rewrite is just in case you want to use the index page, so that it actually goes to the remote index page as well.

Related

How can I use nginx to set a vhost and webroot on a URI of an existing vhost?

I want to send a vhost's requests to git.domain1.tld to sub.domain2.tld/git
I suppose that would conflict with overwritten files so how can I get that domain to point to that location?
You can try this:
location / {
proxy_pass https://sub.domain2.tld/git;
}
Or
rewrite ^/$ https://sub.domain2.tld/git permanent;
rewrite ^/(.*)$ https://sub.domain2.tld/git/$1 permanent;

NGINX Rewrite is encoding querystring as a path

I want to rewrite internally several locations /customers/foo?bar=2 to an existing location in my nginx configuration at /blah so that it's as if the request was to /blah/customers/foo?bar=2.
location /blah {
# View in fiddler
proxy_pass http://127.0.0.1:8888/;
# Lots of config here I don't want to repeat everywhere else
}
location /customers/ {
rewrite ^/customers/(.*) /blah/customers/$1$is_args$args;
}
location /other/ {
rewrite ^/other/(.*) /blah/other/$1$is_args$args;
}
# etc...
Nginx is rewriting the URL with the querystring encoded as a path /blah/customers/foo%34bar=2.
The same thing happens with rewrite ^ /blah$request_uri;. It encodes the ? as %3F effectively garbling the URL.
If I do a client redirect rewrite ^ /blah$request_uri permanent; the URL is correct and contains the ? but I want an internal redirect inside my NGINX config.
Don't use $is_args$args, because the rewrite directive will automatically append any existing query string.
For example:
rewrite ^/customers/(.*) /blah/customers/$1 last;
Although, I would prefer:
rewrite ^(.*)$ /blah$1 last;
Or even:
rewrite ^ /blah$uri last;

nginx rewrite url to remove subdirectory

I would like to rewrite an URL to remove recurring parts from the URL by nginx. Is that possible?
example: dummy.com/sub1/sub2/myproject/trunk --> dummy.com/myproject/trunk
Is a nginx rewrite rule the right way to solve this problem?
location /sub1/sub2 {
rewrite ^/sub1/sub2/(.*)$ $1 last;
}
Solution was to put a proxy_pass in location /
servername dummy.com
location / {
proxy_pass 123.456.789.0:81/sub1/sub2/;
}
A rewrite was not necessary

Nginx proxy_pass after rewrite loses URI segments

I am trying to rewrite a url then proxy_pass the result to another server. Here is my setup:
server {
rewrite ^/foo(.*)$ $1 last;
location /bar {
proxy_pass http://myserver/;
}
}
So when I make a GET request for /foo/bar/dir/file.txt I expect the first rewrite to remove /foo, the location block to catch the result of the rewrite, then proxy_pass to http://myserver:8000/dir/file.txt because the trailing slash on the proxy_pass will strip off /bar.
This is what I expect however when I have the trailing slash then /dir/file.txt is lost in the proxy_pass, and the request is only made to http://mysever/ root URI. I confirmed when I take off the slash then /bar/dir/file.txt is indeed the request the upstream server.
Is there some extra step I am missing?
Sounds weird, I expect the same behaviour from this config as you. What if you strip the /bar prefix inside the location block? Maybe this workaround would work?
location /bar {
rewrite ^/bar(.*) $1 break;
proxy_pass http://myserver;
}

nginx proxy_pass to localhost

I'm trying to use proxy_pass in nginx to forward requests to another port on localhost like this:
location /foo {
rewrite ^/foo/(.*) /$1 break;
proxy_pass http://127.0.0.1:8080/;
}
location /bar {
rewrite ^/bar/(.*) /$1 break;
proxy_pass http://localhost:8080/;
}
location /blah {
rewrite ^/blah/(.*) /$1 break;
proxy_pass http://192.168.77.56:8080/;
}
So only the last one works. The first two give me a page-unavailable error.
I know the endpoint is working as I can go directly to localhost:8080 and see output I expected.
Any idea what I'm doing wrong?
[Edit]: Further enlightenment... It seems the rewrite line has something to do with it. Using it like I have here seems to work on non-localhost IPs, i.e. it removes /blah from the path and keeps the rest as it sends it to its final destination. If I remove the rewrite line I can proxy to localhost (of course losing my indented other stuff on the url).
This worked:
location /blah {
rewrite ^/blah/(.*) /$1 break;
proxy_pass http://$server_addr:8080;
}

Resources