我正在尝试让这 2 个位置指令在 Nginx 中工作,但是在启动 Nginx 时出现了一些错误。
location ~ ^/smx/(test|production) {
proxy_pass http://localhost:8181/cxf;
}
location ~ ^/es/(test|production) {
proxy_pass http://localhost:9200/;
}
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误:
nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block
Run Code Online (Sandbox Code Playgroud)
有没有人觉得很熟悉?我在这里错过了什么?
我在 nginx 配置中有两个位置可以工作:
location ^~ /media/ {
proxy_pass http://backend.example.com;
}
location ^~ /static/ {
proxy_pass http://backend.example.com;
}
Run Code Online (Sandbox Code Playgroud)
如何将这两者合并到一个位置?
我试过这个建议
location ~ ^/(static|media)/ {
proxy_pass http://backend.example.com;
}
Run Code Online (Sandbox Code Playgroud)
但这对我不起作用。
此外,当我不使用后端时,以下配置运行正常:
location ~ ^/(static|media)/ {
root /home/project_root;
}
Run Code Online (Sandbox Code Playgroud)
更新(日志中的一些字符串)
xx.xx.xx.xx - - [31/Dec/2013:13:48:18 +0000] "GET /content/11160/ HTTP/1.1" 200 5310 "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 OPR/18.0.1284.68"
xx.xx.xx.xx - - [31/Dec/2013:13:48:18 +0000] "GET /static/font-awesome/css/font-awesome.min.css HTTP/1.1" 404 200 "http://www.example.com/content/11160/" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, …Run Code Online (Sandbox Code Playgroud) 我正在 Ubuntu 12.04.1 LTS 上的 bash shell 中运行此命令。我试图一举删除[和]字符,即不必第二次通过管道传输到 sed。
我知道方括号在正则表达式中具有特殊含义,所以我通过在前面加上反斜杠来逃避它们。我期待的结果只是字符串,123但方括号仍然存在,我很想知道为什么!
~$ echo '[123]' | sed 's/[\[\]]//'
[123]
Run Code Online (Sandbox Code Playgroud) 我的 Nginx 配置抛出 404.php如下:
## Any other attempt to access PHP files returns a 404.
location ~* ^.+\.php$ {
return 404;
}
Run Code Online (Sandbox Code Playgroud)
但是,我要运行的子文件夹中有一些 index.php 文件。当前的配置是这样的:
location = /sitename/subpage/index.php {
fastcgi_pass phpcgi; #where phpcgi is defined to serve the php files
}
location = /sitename/subpage2/index.php {
fastcgi_pass phpcgi;
}
location = /sitename/subpage3/index.php {
fastcgi_pass phpcgi;
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但问题是重复的位置,如果有很多子页面,那么配置就会变得很大。
我尝试了像 * 和一些正则表达式这样的通配符,它表示 nginx 测试通过但没有加载页面,即 404。我尝试的是:
location = /sitename/*/index.php {
fastcgi_pass phpcgi;
}
location ~* ^/sitename/[a-z]/index.php$ {
fastcgi_pass phpcgi;
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以在该位置使用一些路径名作为正则表达式或通配符?
我只需要从正则表达式中获取匹配项:
$ cat myfile.txt | SOMETHING_HERE "/(\w).+/"
Run Code Online (Sandbox Code Playgroud)
输出必须只是括号内匹配的内容。
不要认为我可以使用grep因为它匹配整行。
请让我知道如何做到这一点。
我见过几个人这样做的例子:
grep [f]oobar
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么这比
grep foobar
Run Code Online (Sandbox Code Playgroud) Nginx~和正则~*表达式有什么区别?
例如:
if ($http_referer ~* www.foobar.net) {
...
}
Run Code Online (Sandbox Code Playgroud)
对比
if ($http_referer ~ www.foobar.net) {
...
}
Run Code Online (Sandbox Code Playgroud) 当我想搜索 html 标签的一部分时,我厌倦了不得不逃避的事情。
我怎样才能准确搜索我输入的内容而不必转义?
例如
ack-grep 'console.log(foo'
Run Code Online (Sandbox Code Playgroud)
我得到:
Unmatched ( in regex; marked by <-- HERE in m/console.log( <-- HERE par/
Run Code Online (Sandbox Code Playgroud) 我遇到了别名问题。我想附加到文件的前 4 位数字作为目录的一部分。(如 '../123456.jpg'=>'../123/123456.jpg' 和 '../12.png'=>'../12/12.png')
这是我所拥有的:
location ~ ^/i/gallery2/(\d{1,4})(.*)$ {
alias /home/web/images/gallery/$1/$1$2;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
nginx: [emerg] pcre_compile() failed: missing ) in "^/i/gallery2/(\d"
Run Code Online (Sandbox Code Playgroud)
任何人都有任何想法如何让它发挥作用?看起来“{1,4}”有问题有没有另一种方式来做我想要的?
fail2ban无法识别失败的公钥 ssh 登录,我认为这可以通过修改failregexof/etc/fail2ban/filter.d/sshd.config以匹配以下行来解决:
<date> <time> <server-hostname> sshd[25917]: Connection closed by <client-ip> [preauth]
Run Code Online (Sandbox Code Playgroud)
但是我找不到关于如何正确修改所述失败正则表达式的体面介绍。因为我真的不知道像^%(__prefix_line)sinclude 这样的变量很难得到一个有效的正则表达式。
我知道最新版本 0.9.1 包含正则表达式以匹配“由 [preauth] 关闭的连接”行,但我使用的是 Debian 存储库中的 fai2ban,并且 0.9.1 的配置与我的配置不兼容有。