67
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
49
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. ...
35
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 ...
34
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 ...
31
votes
Nginx location regex doesn't work with proxy pass
It tells you that the URI in the proxy pass directive can't be used in a regex location. This is because nginx can't replace the part of the URI matching the regex in the location block with the one ...
23
votes
Accepted
Don't need the whole line, just the match from regular expression
2 Things:
As stated by @Rory, you need the -o option, so only the match are printed (instead of whole line)
In addition, you neet the -P option, to use Perl regular expressions, which include useful ...
19
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
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
.*$ ...
16
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 ...
15
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 ...
15
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,
...
15
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 /...
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,...
13
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/...
13
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....
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
How do I make sec ignore a timestamp properly
Well, after almost a day of hair pulling, I finally understand a) how to do it and b) a misconception I have about sec.
In reading the sec man page and it describes desc= as essentially showing the ...
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
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') | ...
9
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/">
..
...
8
votes
Don't need the whole line, just the match from regular expression
Because you tagged your question as bash in addition to shell, there is another solution beside grep :
Bash has its own regular expression engine since version 3.0, using the =~ operator, just like ...
8
votes
nginx url rewriting. i just don't get it
I believe that this website may help you greatly:
http://regex101.com/r/uP4nT1
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
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;
...
7
votes
Show real server name in nginx logs and params passed to fastcgi?
Use $http_host in your log_format to get the actual host that the client sent.
You can also use that variable in access_log directives to split your logs by hostname.
7
votes
Accepted
Apache LocationMatch regex behaviour does not seem correct
It is possible to workaround it by using this configuration:
<VirtualHost 127.0.0.1:80>
ServerName localhost
<Location "/">
Header set X-Intelligence "CLEVER"
</Location&...
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
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
...
7
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
regex × 688nginx × 218
apache-2.2 × 71
rewrite × 65
fail2ban × 60
.htaccess × 52
mod-rewrite × 48
linux × 41
bash × 36
redirect × 35
regular-expressions × 33
postfix × 32
apache-2.4 × 32
grep × 28
sed × 21
haproxy × 18
logging × 17
url × 15
php × 13
web-server × 13
perl × 13
powershell × 12
lighttpd × 12
awk × 12
pcre × 12