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 ...
Community wiki
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. ...
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 ...
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 ...
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/ {
...
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 ...
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
.*$ ...
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 ...
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,
...
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') | ...
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 /...
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....
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/...
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 ...
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,...
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 = /...
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]
...
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/">
..
...
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;
...
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 ...
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
...
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 ...
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 ...
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 ...
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="$(...
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\]$
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 ...
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,...
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$ {
...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
regex × 717nginx × 227
apache-2.2 × 70
fail2ban × 67
rewrite × 66
.htaccess × 56
mod-rewrite × 49
linux × 43
bash × 37
postfix × 34
redirect × 34
regular-expressions × 33
apache-2.4 × 32
grep × 28
sed × 21
logging × 18
haproxy × 18
url × 17
ansible × 14
php × 13
powershell × 13
web-server × 13
perl × 13
apache2 × 12
lighttpd × 12