Haproxy check if only key exists - haproxy

I am using haproxy as a load balancer. I have a specific condition for some criteria.
I need to make sure that the query params of logout should starts with http or https. If the query param value is not starting with http or https I simply need to deny the request.
I have checked so many condition but it doesn't seems to work for me.
I've tried the following
acl url_param1 urlp_reg(logout) ^(http|https)://.*$
http-request deny if url_param1
The above command will block all the URL's even if the URL doesn't have the logout key in URL.
I need to allow if the URL doesn't have logout param in URL. I just want to blok only if the logout parameter value doesn't starts with http/https.
Suggest me the condition for this.

Your description of how this behaves does not make sense -- as written, it should deny if the parameter exists and does begin with http(s).
The correct logic is this:
http-request deny if { url_param(logout) -m found } !{ urlp_reg(logout) ^(http|https)://.*$ }
Deny the request if the parameter exists and the parameter does not begin with http(s)://.

Related

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

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

Extracting params from the Referer Header field in HAProxy

I understand that I can use url_param / urlp to extract the query parameters from the URL that is requested, in HAProxy.
However, I need similar function for extracting parameters from the URL sent as HTTP Header field Referer. I guess url_param is only available for the requested URL, and not possible to use for HTTP Header values? If so, what other options do I have? I need to retrieve the value from query parameter and send it as specific HTTP Header to the backend server.
Sharing my solution (although Im not sure this is the most efficient and accurate way). I solved it with Regex.
# Example HTTP Referer: http://myexample.com/users?user-id=12345
# ACL
acl is_uid_in_hdr_referer hdr_sub(Referer) -i user-id
# Set value from query param "user-id" from Referer header to custom header "user-id"
http-request set-header user-id %[req.hdr(Referer),regsub(.+?user-id=,,g)] if is_uid_in_hdr_referer

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

Nginx redirect string variable with wild card wordpress

I have a bunch of this strange 404 error's in google search console and the URL's doesn't exists in my site and I need to redirect them to my homepage.
http://www.example.com/plugins/feedback.php?href=http%3A%2F%2Fwww.example.com%2Fremain-url-1%2F&_fb_noscript=1
http://www.example.com/plugins/feedback.php?href=http%3A%2F%2Fwww.example.com%2Fremain-url-2%2F&_fb_noscript=1
http://www.example.com/plugins/feedback.php?href=http%3A%2F%2Fwww.example.com%2Fremain-url-3%2F&_fb_noscript=1
I've made this two attempts but it's not working
rewrite ^/plugins/feedback(/.*)$ http://www.example.com/ permanent;
rewrite ^/plugins/feedback.php?href=http://www.example.com(/.*)$ /blog/ permanent;
Is it possible to redirect this with one wild card?
The rewrite directive cannot be used to filter parameters because it uses a normalized URI which does not include the query string. You can access parameters using the $args variable, or individually using the $arg_xxx variables.
However, the $request_uri contains the entire URI (including query string) and could be used with an if block or map to test for the presence of the parameters you seek.
For example:
if ($request_uri ~ ^/some/regular/expression) {
return 301 /;
}
The block could be placed in the server block scope, or within the location block which would normally process the /plugins/feedback.php URI.
See the following documents for details: if directive, map directive, if usage restrictions.

nginx redirect based on domain and amend the url

i have several server names as in www.example.com example.com www.example.de example.de. If there is no arguments I would like to amend the language to the URL
for example
www.example.com ---> www.example.com?language=en
example.de ---> example.de?language=de
regrads
You can test for the absence of arguments by comparing $args with an empty string. For example:
if ($args = "") {
return 301 $scheme://$host$request_uri?language=en;
}
If you have your domains implemented within different server blocks, you can customise the return statement appropriately.
If you need to implement all of this within a single server block supporting multiple host names, you may need to use a map directive to calculate the value of the language parameter. See this document for more.
This is not a perfect solution, as the edge condition www.example.com? will result in www.example.com??language=en which does not look nice.