Nginx: to show cleaner url on user side - nginx

I am trying to rewrite url so that on user side they would recive more clraner url.
Server url:
https://www.foo.com/news/article/pager.php?blog_id=2&tmpl_id=233?&page=2
On users browser url:
https://www.foo.com/news/article/page/2/
I have setup rewrite on nginx.conf like below but unsuccessful. Any idea why?
location / {
rewrite ^/article/page/([^/]+)/$ /article/pager.php?blog_id=2&tmpl_id=233&page=$1 break;
}

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.

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?

NGINX proxy_pass URL with parameters to another path

I tried the following config in my nginx proxy:
location /new/path {
proxy_pass http://IP_Address:PORT/old/path.html?jh=twrtax2jlqs5t07d556lfhpfn3he0og;
}
After that, I tested it with the following URL: domain.com/new/path
My Browser shows the new path URL but in the background it opens /old/path.html without the rest after the question mark....
Any ideas? Whats wrong?

Nginx - redirect or rewrite url

We have nginx setup with location proxy pass and required one specific url need to redirect or rewrite when it was hit.
Src Url:
https://application-url:port/services/app1/callback/?oauth_token=<<tokens>>
Dest Url:
https://application-url:port/services/app1/callback?oauth_token=<<tokens>>
Any solution here.
It seems src and dest url are same. if you want to rewrite the url:
then add below lines in your server
location /callback {
rewrite ^/callback(.*) https://application-url:port/services/app1/callback$?oauth_token=<<tokens>> permanent
}

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

Resources