As of May 31, 2023, we have updated our Code of Conduct.

Hot answers tagged

70 votes

Nginx location regex doesn't work with proxy pass

A small addition to the great answer from Xaviar: If you happen to be not so well acquainted with nginx, there's an important difference between adding the slash to the end of the proxy_pass ...
54 votes
Accepted

Using sed to remove both an opening and closing square bracket around a string

This is easy, if you follow the manual carefully: all members inside a character class lose special meaning (with a few exceptions). And ] loses its special meaning if it is placed first in the list. ...
Saparagus's user avatar
  • 653
37 votes
Accepted

What is the difference between Nginx ~ and ~* regexes?

~: If a tilde modifier is present, this location will be interpreted as a case-sensitive regular expression match. ~*: If a tilde and asterisk modifier is used, the location block will be interpreted ...
cduffin's user avatar
  • 844
37 votes
Accepted

Nginx wildcard/regex in location path

The = modifier in location block is an exact match, without any wildcards, prefix matching or regular expressions. That's why it doesn't work. On your regex attempt, [a-z] matches a single character ...
Tero Kilkanen's user avatar
21 votes

NGINX Proxy_Pass remove url substring

It's really basic and simple. Just add /path/ part to proxy_pass and nginx will replace locations prefix with that path. You need to replace /string_1/ with /, so do it: location /string_1/ { ...
Alexey Ten's user avatar
  • 8,260
18 votes

How do I match a wildcard host in ACL lists in HAproxy?

I feel that hdr_sub is better for your needs. I was using hdr_end for a while but it runs into the following problem: requests with port 80 usually get the port stripped so the host header looks like ...
Rj Wilson's user avatar
  • 181
18 votes

Don't need the whole line, just the match from regular expression

sed -n "s/^.*\(captureThis\).*$/\1/p" -n don't print lines s substitute ^.* matches anything before the captureThis \( \) capture everything between and assign it to \1 .*$ ...
Joshua's user avatar
  • 539
17 votes

How do I match a wildcard host in ACL lists in HAproxy?

There are cases where you need to be explicit about this, such as handling redirects for wildcard SSL with multiple levels of subdomains. Matching end (hdr_end or -m end) or substring (hdr_sub or -m ...
gregmac's user avatar
  • 1,569
17 votes
Accepted

NGINX Proxy_Pass remove url substring

I found the way to rewrite the proxy_pass URL: location /string_1/ { if ($request_uri ~* "/string_1/(.*)") { proxy_pass http://internal_host:port/$1; } } Regards, ...
Pedro's user avatar
  • 657
17 votes
Accepted

Extracting part of the string using Ansible regex_search and save the output as a variable

You need to add a group to your regex and a second parameter that specifies which group to return: - set_fact: my_var: "{{ zoo_config_content.stdout | regex_search('dataDir=(.+)', '\\1') | ...
Gerald Schneider's user avatar
16 votes
Accepted

nginx rule - match all paths except one

I ended up using the following solution: location ~ ^/newsletter/(.*)$ { location ~ /newsletter/one(.*) { // logic here } // logic here } This matches all the paths under /...
dasj19's user avatar
  • 393
15 votes

Modify fail2ban failregex to match failed public key authentications via ssh

No regex hacking is required (at least since fail2ban 0.10.4). In /etc/fail2ban/jail.conf is the following information: [sshd] # To use more aggressive sshd modes set filter parameter "mode" in jail....
Sparhawk's user avatar
  • 251
14 votes

Extract repository name from GitHub url in bash

Old post, but I faced the same problem recently. The regex ^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$ works for the three types of URL. #!/bin/bash # url="git://github.com/some-user/...
Hicham's user avatar
  • 141
14 votes
Accepted

Regex for multiple sshd Received disconnect from ... [preauth]

You can use this rule: ^%(__prefix_line)sReceived disconnect from <HOST>: 11: (Bye Bye)? \[preauth\]$ To test it with fail2ban-regex or egrep, you can just strip off the ^%(__prefix_line)s ...
sebix's user avatar
  • 4,293
14 votes
Accepted

Why is it unnecessary to escape "/" in Nginx regular expressions?

The reason you don't have to escape / is that / is not a delimiter. It sounds like you are accustomed to writing regular expressions in other applications, where a delimiter is required. For instance,...
Michael Hampton's user avatar
12 votes

nginx rule - match all paths except one

This works but has one flaw. It will also not match on "one" followed by any characters. location ~ ^/newsletter/(?!one).*$ { //configuration here } Although, this may be better: location = /...
Tim's user avatar
  • 441
11 votes
Accepted

Modify fail2ban failregex to match failed public key authentications via ssh

This line does it: ^%(__prefix_line)sConnection closed by <HOST> \[preauth\]$ Tested with the following logstring: Apr 29 12:30:12 sendai sshd[25917]: Connection closed by 127.0.0.1 [preauth] ...
sebix's user avatar
  • 4,293
10 votes
Accepted

Can LocationMatch Regex match Query String portion?

Actually it's possible since Apache 2.4 (or less) using the tag as follow : <LocationMatch "/test/upload.js"> <If "%{QUERY_STRING} =~ /query=test/"> .. ...
Vivien's user avatar
  • 116
9 votes
Accepted

Redirect of all the URLs that contain 1 word in specific, but that do not contain other words

I was able to solve my problem in the following way: location ~ /travel(.*)+/(.*)$ { if ($uri !~ "^(.*)/(reservation|hotel|faq)(.*)"){ return 301 https://www.misite.com$request_uri; ...
Alorse's user avatar
  • 221
8 votes
Accepted

Reg Exp for URL in HAProxy

You need to construct two regex's that are mutually exclusive: only one can be true at a time: How about if you make the second item be a regex like: ^/path[^/] The [^/] means "any character that ...
TomOnTime's user avatar
  • 7,857
8 votes
Accepted

Block specific URL in HAProxy / url-encoding

As it happens, HAProxy has a converter to decode the field, making sure that your ACL will always match a given string. url_dec Takes an url-encoded string provided as input and returns the ...
GregL's user avatar
  • 9,330
8 votes
Accepted

NGINX: map and try_files not working

The try_files statement is using numeric captures from the regular expression in the location statement. The problem with numeric captures is that they are overwritten whenever a regular expression is ...
Richard Smith's user avatar
7 votes

Is it possible to get the matched regex from within the apache LocationMatch directive?

According to the Apache documentation this is now supported... From 2.4.8 onwards, named groups and backreferences are captured and written to the environment with the corresponding name prefixed ...
pillingworth's user avatar
7 votes

Nginx proxy_pass root and specific url only

location / matches any location - if you want to match only /, you should use location = /. See this document for details. Also, your regular expression location is missing the ~ or ~* operator. But ...
Richard Smith's user avatar
6 votes

Extract repository name from GitHub url in bash

Summing up: Get url without (optional) suffix: url_without_suffix="${url%.*}" Get repository name: reponame="$(basename "${url_without_suffix}")" Get user (host) name afterwards: hostname="$(...
hypnoglow's user avatar
  • 161
6 votes

Modify fail2ban failregex to match failed public key authentications via ssh

At least in openssh 7.3 the log messages also contain a port number. So I had to modify sebix's solution to the following: ^%(__prefix_line)sConnection closed by <HOST> port \d+ \[preauth\]$
code-chicken's user avatar
6 votes
Accepted

SELinux file path context not working with regex

Try using HOME_DIR/\.google_authenticator -- gen_context(system_u:object_r:radiusd_google_authenticator_t,s0) Instead. Home directories aren't necessarily in /home and this acts as a macro when you ...
Matthew Ife's user avatar
  • 23.3k
6 votes
Accepted

Using FilesMatch to target ALL files

If you really mean what you say, that is, match ALL files, that would be something like: <FilesMatch ^> Try, but in any case I think this looks like a very bad idea. Better to be more specific,...
Daniel Ferradal's user avatar
6 votes
Accepted

nginx: multiple matching location blocks

There's a good article about server and location block matching here. Only one location block can match, so you're going to create a location block just for mp3 files. location ~* \.mp3$ { ...
Tim's user avatar
  • 31.5k
6 votes
Accepted

fail2ban find matches, but does not ban

Most probably you don't have pyinotify installed on your system which cause fail2ban to fail getting log file modifications. I had the same problem and fixed it using this. 1. Install pyinotify yum ...
Ergec's user avatar
  • 608

Only top scored, non community-wiki answers of a minimum length are eligible