Block request that have specific string/word nginx - 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?

Related

Can a response from an http request alter the base address in the next client request?

I have an octoprint server running at http://192.168.1.205. I also have an nginx server hosting myDomain. I want to be able to use the nginx server to pass on a request for http://myDomain/octo to http://192.168.1.205 using a reverse proxy. Here is my nginx code...
server {
server_name myDomain;
location /octo/ {
rewrite ^/octo/(.*) /$1 break; #strip /octo from url
proxy_pass http://192.168.1.205;
}
}
The first http://myDomain/octo request is passed on to http://192.168.1.205 correctly. But after the first response the code in the client makes another request to http://myDomain/moreUri. Since this uri doesn't have /octo nginx doesn't know to send it to http://192.168.1.205/moreUri. Is there a way to have nginx change something in the first response so that the client then makes following requests to http://myDomain/octo/moreUri?
I was able to accomplish this for a case where the octoprint server responded with a redirect. I used ...
proxy_redirect http://192.168.1.205/ http://myDomain/octo/;
and it worked. But that only works on redirects and the following requests were wrong again.
Is there a way to have nginx change something in the first response so
that the client then makes following requests to
http://myDomain/octo/moreUri?
I am not aware that this is possible.
What about to adjust the nginx configuration accordingly ? using location / to process all requests within that block and add an additional redirect directive to address the "Since this uri doesn't have /octo nginx doesn't know to send it to http://192.168.1.205/moreUri" should do the trick.
server {
server_name myDomain;
location / {
rewrite ^/octo/(.*) /$1 break; #strip /octo from url
rewrite ^/(.*)/(.*) /octo/$2 break; #rewrite /moreURI to /octo/moreURI
proxy_pass http://192.168.1.205;
}
}
No matter if the above nginx reconfiguration fixes your issue, i assume the root cause why you need to configure the nginx as reverse proxy in this way might be a misconfigured (or not optimally configured) application. Check the config file if it is possible to configure the applications base path. If so, set it to /octo/ (so the application itself prepends /octo/ to all the links it presents to the user and all requests to the backend automatically) and adjust the rewrite rules accordingly.

nginx reverse to all pages

I have the following configuration that works for only 1 page:
location /mypage.html/ {
proxy_pass http://${remote_server}/;
}
When I am trying to navigate to other pages on the remote server, I get page not found.
Is there any way to keep the reverse proxy open for all pages on the remote server?
This will match everything, technically everything that starts /, and route the same request to your remote server:
location / {
proxy_pass http://${remote_server}/;
}

NGINX rewrite location without changing url

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.

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 ..

How to use nginx to redirect one page request to a different page

I want to redirect http://example.com/balcony-covers/ to
http://example.com/balcony-screens/ permanently. How can I do this in nginx.
Use your nginx.conf file.
Put
location /balcony-covers/ {
return 301 $scheme://$host/balcony-screens/;
}
in your server block

Resources