NGINX rewrite location without changing url - nginx

When visiting /abc/sitemap.xml I'd like to display the content of /abc/sitemap?output=xml without changing the url.
Currently, it shows the right content, but changes the url to /abc/sitemap?output=xml instead of keeping /abc/sitemap.xml, and here is my config.
location /abc/sitemap.xml {
rewrite ^ /abc/sitemap?output=xml;
}
Thanks

How about a proxy_pass:
location /abc/sitemap.xml {
proxy_pass http://$host/abc/sitemap?output=xml;
}
Note: use HTTP or HTTPS depending on your SSL configurations.

Related

Block request that have specific string/word nginx

I have NGINX as proxy with SSL for background Tomcat server with ORDS/Apex. I am trying to block access to admin login page. Url to page looks like this: https://10.10.10.10/ords/f?p=4550:1:10500576264651:::::
The part of URL is fixed (/ords/f?p=4550) and everything after (:1:10500576264651::::: ) is dynamic.
I'm trying to block ALL requests that have string "/ords/f?p=4550" in URL. For ordinary directory block works good, but when need to read URL I cant make it work.
#This do not work
location ^~ /ords/f?p=4550 {
return 404;
}
#This works
location ^~ /janko {
return 400;
}
I managed this in HAProxy relatively easy by reading ALC like this and redirecting to 404 page
#Recognize patern
acl denyPath url_beg /ords/f?p=4550
#Use backend that redirect to 404
use_backend redirectTo404 if denyPath
Do anybody have any idea how to do this on NGINX?

How to make a link redirect to a location with nginx

I am having a problem with trying to serve two different sites using the same server.
I have a landing page at the root of timothylim.is and a separate blog at timothylim.is/writing.
I have the following nginx configuration with two different locations:
location / {
rewrite ^/pserver(/.*)$ $1 break;
proxy_pass https://timothyylim.github.io/landing-page/;
}
location /writing {
rewrite ^/pserver(/.*)$ $1 break;
proxy_pass https://timothyylim.github.io/writing-online/;
}
However, when any of the links are clicked on the '/writing' location the browser navigates to timothylim.is/poetry/your-pillar/ instead of timothylim.is/writing/poetry/your-pillar/.
Is there a way to redirect to a subfolder on the main domain?
You could use the base meta tag instead of trying to change server behaviour.
<base href="timothylim.is/writing/">

NGINX: proxy_pass for a dynamic url

I have a docker container which serves a web application on:
my.server.domain:8080
When I request that URL with a browser, it is automatically redirected to the login page:
http://my.server.domain:8080/login
I'm trying to proxy the app so that I can avoid to use the port number.
Two possible ways to achieve that are:
1) http://my.server.domain/appname
2) http://appname.my.server.domain
Either way, will work for me.
But I'm struggling with making the right NGINX configuration.
I tried with:
location /appname {
proxy_pass http://my.server.domain:8080/;
}
But when I load http://my.server.domain/appname it gets redirected to http://my.server.domain/login which doesn't exist.
If I use / it works as expected:
location / {
proxy_pass http://my.server.domain:8080/;
}
But that's not what I need.
I can add more directives like:
location /login {
proxy_pass http://my.server.domain:8080/login;
}
But then it will fail on the next redirect and so on ..

Routing in nginx URI with same prefix as another URI

Currently trying to use Nginx as a proxy and finding some difficulties with routes having another URI as a prefix. Example :
location /api/route/to/specific/application {
proxy_pass http://proxying.to/site/A;
}
location /api {
proxy_pass http://proxying.to/another/site/B;
}
When visiting http://mysite.io/api/route/to/specific/application, I get a 404 from Nginx while when I visit http://proxying.to/site/A directly I get the expected page.
Any idea how I can make it work ?
Thanks

With nginx proxy/rewrite can I keep the original URL in the browser's Location field?

Using nginx.conf features like proxy-pass/rewrite, can I keep the original URL in the browser's Location field?
I have several PlayFramework apps running on different ports (9001, 9002, ...) with proxy forwarding set up via nginx.conf. People browse to them as:
http://domain.name/App1/
http://domain.name/App2/
etc.
My nginx.conf entries look like this:
location /App1/ {
proxy_pass http://localhost:9001/;
rewrite ^/App1/(.*) http://domain.name:9001/$1;
}
If I ask for http://domain.name/App1/, what I see in the browser's Location field is http://domain.name:9001. What I wish I saw was http://domain.name/App1/, that is, I want the name App1 to remain in the URI, and I'd rather not expose the port number.
Let's say App1 has a link /location/ABC. When I click on it I see http://domain.name:9001/location/ABC when I wish I saw http://domain.name/App1/location/ABC.
Can I achieve this with nginx.conf?
P.S. I put http://domain.name explicitly in the rewrite rule because without it I was getting localhost in the browser, and my browser's localhost is not the same as the server's.
Rewrites issue redirects for browser.
If you just want to mount several locations from upstreams - you do not need rewrites, just use:
location /App1/ {
proxy_pass http://localhost:9001/;
}
But apps should use relative links or account for their absolute location.
For more complex url manipulation you can use break-rewrites:
location /App1/ {
rewrite ^/App1/(.*) /$1 break;
proxy_pass http://localhost:9001;
}

Resources