How to proxy_pass only remaining URL after a match - nginx

Only need to proxy pass remaining url when match to location
location /blog { proxy_pass http://example.com }
i.e if somebody requests /blog/page1/temp.html they are getting proxy passed to example.com/blog/page1/temp.html
I want to change it to example.com/page1/temp.html

I want to change example.com/blog/page1/temp.html to example.com/page1/temp.html
Specify uri into proxy pass directive (/ after hostname in this case):
location /blog/ {
proxy_pass http://example.com/;
}
Or use rewrite like this:
location /blog {
rewrite /blog/([^/]+) $1;
proxy_pass http://example.com
}

Related

Nginx same location 2 blocks with diffrent proxy pass

Trying to configure nginx to have 2 proxy passes for the same location.
Example:
domain.com to go to the homepage proxy
domain.com/user to go to user profile proxy
What i need to do:
What I tryed:
location = / { proxy_pass http://home_page; }
location ~ ^/(.+) { proxy_pass http://user_profile_page; }
If I go to domain.com/ and domain.com/username it goes to the same proxy.
I have to mention that the config has also the /auth location block with exact match "="
that has domain.com/auth, domain.com/auth/login. This location block works as expected
location ^~ /auth { proxy_pass http://auth_page; }

Nginx an application redirects on root site

Navigate to localhost/app1 redirects me on localhost/login, expected - localhost/app1/login.
Is it possible to change the application redirect to the right place using nginx?
location /app1/ {
proxy_pass http://localhost:8080/;
}
Used Referer URL to make the right way redirection. The configs below:
nginx.conf - get application name from $http_referer
http {
map $http_referer $app {
~*//[a-z0-9_.-]+/([a-z0-9_.-]+) $1;
}...
}
root.conf - redirect wrong URLs to $app$uri
location / {
if ($http_referer != "") {
rewrite ^(.*)$ /$app$uri redirect;
}
app1.conf - important! The Referer header must be added!
location /app1/ {
proxy_pass http://localhost:8080/;
proxy_set_header Referer $http_referer;
...
}

Rewrite rules without changing uri in Nginx

How can I rewrite an uri without returning a redirect in the process in Nginx ?
The rewrite result is in the same host.
Exemple: rewrite "mysite.com/foo" returning the same result as "mysite.com/bar", but we dont change the uri in the process.
It's kinda like a proxy_pass but for the same host.
You can proxy_pass to host+uri.
location ~ ^/foo/(.*)$ {
include proxy_params;
proxy_pass http://127.0.0.1/bar/$1;
}
Or rewrite and proxy_pass should work:
location ~ ^/foo {
rewrite ^foo(.*) /bar$1
proxy_pass http://example.com;
}
I hope any of those 2 works for you.

nginx proxy_pass removing base path

So I currently have a site at http://www.example.com and I would like that all requests from
http://www.example.com/api/v2/<anything>
to be proxied to
http://api.v2.com/<anything>
Note that http://api.v2.com does not have a base path.
My config:
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
This way, however, the requests are being proxies to http://api.v2.com/api/v2/<anything> keeping the original path, and not http://api.v2.com/<anything>.
I've already noticed that:
location /api/v2 {
proxy_pass http://api.v2.com/api;
works as desired - http://api.v2.com/api/v2/<anything> proxies to http://api.v2.com/api/<anything>.
Is there a way to achieve the first behavior (that is , http://api.v2.com/api/v2/<anything> to http://api.v2.com/<anything>)? Do I have to use a rewrite? I know it it related to normalized URIs and that is expected, just would like to know if there's a way to do it.
If you want to remove the proxy path from resulting url to the server, either you should have the uri part in the proxy url, or you should specify the rewrite part.
In this specific case, it should be like as follows,
location /api/v2/ {
rewrite /api/v2/(.*) /$1 break;
proxy_pass http://api.v2.com/;
}
for more information visit nginx documentation https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive.
server {
listen 80;
server_name www.example.com;
location /api/v2/ {
proxy_pass http://api.v2.com/;
}
location / {
index index.html;
root /var/www/example.com/htdocs;
}
}
Source: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Different behavour of "location" and "proxy_pass" on nginx x-accel-redirect

I have my nginx conf like :
location ^~ /mount_points/mount_point1 {
internal;
alias /repos/mount_point_one;
}
location ^~ /to_proxy {
internal;
proxy_pass http://myproxy:5000;
}
When I request for 'http://localhost/mount_points/mount_point1/myfile.zip' I get "/repos/mount_point_one/myfile.zip" as expected.
While request for 'http://localhost/to_proxy/myfile2.html', I get "http://myproxy:5000/to_proxy/myfile2.html".
In the first case, the "/mount_points/mount_point1" part was removed, and in the second case, the "/to_proxy" part still there, I have to fake a "/to_proxy" address in the upstream server to find out this.
Did I missed something? If I just have to rewrite the url, how can I delete the "/to_proxy" part issue to the upstream server?
Thank you.
The proxy_pass directive can perform an aliasing function, but only if an optional URI is provided.
location ^~ /to_proxy/ {
internal;
proxy_pass http://myproxy:5000/;
}
To make the alias mapping work correctly, a trailing / is also added to the location parameter.
See this document for details.
If the trailing / on the location parameter causes problems, you can use a rewrite ... break instead:
location ^~ /to_proxy {
internal;
rewrite ^/to_proxy(?:/(.*))?$ /$1 break;
proxy_pass http://myproxy:5000;
}

Resources