haproxy rewrite on backend to add a path for some requests to a specific domain - haproxy

I am looking to try and get haproxy to rewrite a url on the backend. For example if the end user navigates to bitechomp.domain.com they should only see this URL but the request to the backend server needs to have the request re-written to include a path. e.g. bitechomp.domain.com/bitechomp
I believe I have the regex to match it, but struggling to find the syntax to then just have it add the folder path at the end.
^([a-zA-Z0-9]/)?(bitechomp).$

I believe I have resolved this.
http-request set-path /bitechomp/ if { path_reg ^([a-zA-Z0-9]/)?(bitechomp).$ }
This works for any domain so both bitechomp.domain1.com and bitechomp.domain2.com would be re-written to bitechomp.domain1.com/bitechomp and bitechomp.domain2.com/bitechomp

Related

HA Proxy rewrite and redirect

Trying to do a rewrite and redirect. I've been trying this, it works to some extent but not 100% what I want it to do
acl old url_beg /site/ab
http-request redirect location /new/%[query] if old
the url can be for example
https://host/site/ab/xx
https://host/site/ab/yyyy
https://host/site/ab/zzzzzz
https://host/site/ab/zzzzzz/asdajshdjasd
I am looking to grab the bold marked part and simply redirect the user to https//host/new/boldmarkedpart
Any string that comes after the bold marked part can be trashed. For example "/asdajshdjasd" in the last example.
Any idea how to accomplish this? Thank you!!
If i understand correctly, you want to split the path part of url and get its 4th part.
In string foo1/foo2/foo3/foo4/foo5 you want only foo4.
This should work for you:
acl old path_beg /site/ab
http-request redirect location /new/%[path,field(4,/)] if old
It may be confusing that you want 3rd directory from path and here you take 4th word, but that's because when you split /foo2/foo3/foo4/foo5 by / then the first word is empty.
field converter is documented here: https://cbonte.github.io/haproxy-dconv/2.2/configuration.html#7.3.1-field
Other notes:
%[query] would return the query part of url, which is everything after ? character and you don't have query part at all in your examples.
url in my tests had schema://hostname:port/path, so testing acl old url_beg /site/ab never matched, path is for that

Redirect ALL get parameters to another domain in Nginx

Only answers I can find for this have specific parameters and don't work if the parameters change.
I have a URL coming in such as:
/logs.php?user_id=10032&account_id=10099&message=0&interval=1&online=1&rsid=65374826&action=update&ids=827,9210,82930&session_id=1211546313-1602275138
I need to redirect this url to a completely different domain and file but keep the get params.
So it ends up redirecting to:
https://example.com/the_logger.php?REPEAT_ALL_GET_PARAMETERS_HERE
Here is what I have unsuccessfully tried so far:
location logs.php
{
rewrite https://example.com/the_logger.php?$args last;
}
But it doesn't seem to match the url or redirect. I think I'm misunderstanding the logic of nginx confs. If it were .htaccess I think I'd be okay. I can put a few more examples here if need be of what I'm trying to achieve.
As you state this is a redirect and not reverse proxying a request, I would use the return directive to tell the client to do a 301 or 302. Using the return directive is the simpler and more recommend approach to redirecting a client.
Something like should do what you want:
location /logs {
return 302 https://example.com/the_logger.php$is_args$args;
}
Where $is_args would output a ? if and only if the query string is not empty, and $args is the query string itself

How to make a redirect to nginx, depending on the part of the URL?

Welcome essence of the problem.
We have a path
site.com/seveniry-dlya-turistov/...
(Where ... is the character code specific record (of goods), and in front of it, as you might guess - section of this article)
Those show on site.com website under "souvenirs for tourists" page of a souvenir.
I need to redirect this type:
If there is a request to .../eveniry-dlya-turistov/.. substitute the section title in the name of .../seveniry/..., see if there is a request for
site.com/seveniry-dlya-turistov/elemnet1/
we have to do a 301 redirect to
site.com/seveniry/elemnet1/
Please tell me how to do it, and why does not work like that ...
location /catalog {
rewrite ^/catalog/souvernirs-for-tourists/(.*)$ http://SITE-NAME/suveniry/$1 permanent;
}
Thanks in advance for your help!
Should be something like
rewrite ^/seveniry-dlya-turistov/(.*)$ /seveniry/$1 last;
But you need to test the exact regex since it's hard to do by head without testing. nginx will look for another location block matching /seveniry/$1 to serve content.

Nginx URL rewrite storing part of URI & Using it in redirect

Trying to make a nginx url rewrite for a branding only global site that
has search functionality (but no product catalog assigned as it's the global site) that will store part(or all if it's better) of the URL to pass it to a dedicated redirect script that geo redirects the user's query to the correct regional site.
Can someone tell me what is wrong with what I am doing?
rewrite ^/catalogsearch/result?q=(.*)$
/redirect-script?x=/catalogsearch/result=q=$1
permanent;
I don't think you can match the query string with the rewrite regexp.
Try this:
rewrite ^/catalogsearch/result$
/redirect-script?x=/catalogsearch/result=q=$arg_q
permanent;
$arg_q is one of the automatic variables containing the query arguments, see http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_

Nginx url rewrite for restful url

I have a Rails web app which has static files under dynamic restful urls. For example:
/projects/1/attachments/some_file.xls
I want to setup Nginx to redirect to the static file on the server:
/public/attachments/1/some_file.xls
Where 1 is the dynamic project id.
How would the location block and rewrite statement look for the Nginx config file?
UPDATE
I marked an answer below as it answers my original question. Though rewriting my project attachment urls wasn't necessary in my case. I forgot that I was already remapping the url in my Rails view erb.
My true goal was to stop Thin from adding caching response headers to my attachment files. Ultimately, I was able to prevent this with Nginx by just adding a location for attachments like such:
location /attachments/ {
expires off;
add_header Pragma "no-cache";
add_header Cache-Control "no-cache, no-store";
access_log off;
break;
}
Here is simple example.
location /project/ {
rewrite ^/project/([0-9]+)/attachments/(.*)$ /public/attachments/$1/$2 last;
}
If you want more complicated rule, see the official help.
http://wiki.nginx.org/HttpRewriteModule#rewrite
Sounds like you're doing something wrong with your image/file library, as you shouldn't have to do any nginx rewrite rules for Paperclip, Dragonfly, etc to just work. For example in Paperclip you can set the url and/or path format explicitly when you use the DSL in the Model. For Paperclip it would look like this:
has_attached_file :attachment,
:url => '/attachments/:id/:style/:basename.:extension',
:path => ':rails_root/public/attachments/:id/:style/:basename.:extension'
Then everything should get served by nginx automatically since it's in your public directory. No rewrite rule necessary.