Nginx rewrite doesn't work with typical extensions - nginx

I have a nginx rewrite location conf like this
location /playernew {
error_log /home/heyka/rewrite-error.log debug;
if ($request_filename ~ /playernew/([A-Za-z0-9_-]+)/(.+)){
rewrite /playernew/([A-Za-z0-9_-]+)/(.+) /playernew/$2 break;
}
For e.g. url .com/playernew/wydra/assets/css/style.csss works. Notice .csss extension.
And in log I have:
2019/09/09 17:35:41 [notice] 28519#0: *103 "/playernew/([A-Za-z0-9_-]+)/(.+)" matches "/home/website/www/playernew/wydra/assets/css/style.csss", client: ip, server: nocache.website.com, request: "GET /playernew/wydra/assets/css/style.csss HTTP/1.1", host: "nocache.website.com"
2019/09/09 17:35:41 [notice] 28519#0: *103 "/playernew/([A-Za-z0-9_-]+)/(.+)" matches "/playernew/wydra/assets/css/style.csss", client: ip, server: nocache.website.com, request: "GET /playernew/wydra/assets/css/style.csss HTTP/1.1", host: "nocache.website.com"
2019/09/09 17:35:41 [notice] 28519#0: *103 rewritten data: "/playernew/assets/css/style.csss", args: "", client: ip, server: nocache.website.com, request: "GET /playernew/wydra/assets/css/style.csss HTTP/1.1", host: "nocache.website.com"
2019/09/09 17:35:41 [error] 28519#0: *103 open() "/home/website/www/playernew/assets/css/style.csss" failed (2: No such file or directory), client: ip, server: nocache.website.com, request: "GET /playernew/wydra/assets/css/style.csss HTTP/1.1", host: "nocache.website.com"
But...
When I hit the .com/playernew/wydra/assets/css/style.css. Notice .css... Doesn't work completly. Nothing in logs. Nothing. Like rewrite command never existed.
Even with config like...
location /playernew {
error_log /home/arek/rewrite-error.log debug;
rewrite style.css / break;
the same :/
Why? What is going on?
Why .csss works but .css doesn't. And the same with other regular extensions.
Again -> hitting /playernew/wydra/setup/index.phpp generates log and rewrite works.
But hitting /playernew/wydra/setup/index.php... no log + doesn't work.
For me it has problem with any typical extension

Related

Logstash grok pattern for nginx error dosen't work for all lines

I have a grok pattern for nginx error log:
(?<timestamp>%{YEAR}[./]%{MONTHNUM}[./]%{MONTHDAY} %{TIME}) \[%{LOGLEVEL:severity}\] %{POSINT:pid}#%{NUMBER:threadid}\: \*%{NUMBER:connectionid} %{GREEDYDATA:text}, client: %{IP:client}, server: %{GREEDYDATA:server}, request: \"(?:%{WORD:requesttype} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion}))\"(, upstream: "%{GREEDYDATA:upstream}\")?, host: "%{DATA:host}\"(, referrer: \"%{GREEDYDATA:referrer}\")?
And I have this kind of errors
2022/01/17 08:23:39 [error] 8#8: *0000016 this is my error message, client: 1.2.3.4, server: my.server.name, request: "GET /my/url/ HTTP/1.1", upstream: "http://upstream.server.name", host: "my.server.name", referrer: "https://referrer.server.name/"
2022/01/17 12:30:41 [error] 8#8: *0000016 access forbidden by rule, client: 1.2.3.4, server: my.server.name, request: "GET / HTTP/2.0", host: "my.server.name"
2022/01/17 08:23:39 [error] 8#8: *0000016 could not be resolved
For testing I'm using grokconstructor in appspot and my grok pattern works great for first two lines, but I can't make it works for the last line:
What pattern will work for all three lines?
this would work:
(?<timestamp>%{YEAR}[./]%{MONTHNUM}[./]%{MONTHDAY} %{TIME}) \[%{LOGLEVEL:severity}\] %{POSINT:pid}#%{NUMBER:threadid}\: \*%{NUMBER:connectionid} %{DATA:text}(, client: %{IP:client}, server: %{GREEDYDATA:server}, request: \"(?:%{WORD:requesttype} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion}))\"(, upstream: "%{GREEDYDATA:upstream}\")?, host: "%{DATA:host}\"(, referrer: \"%{GREEDYDATA:referrer}\")?)?$.
The last part is made optional with ()?.
Just a note, for the key-value pairs at the end of the first two lines, you should use the kv plugin instead of doing it in grok. So store that part using the grok filter in a temporary field like this:
(?<timestamp>%{YEAR}[./]%{MONTHNUM}[./]%{MONTHDAY} %{TIME}) \[%{LOGLEVEL:severity}\] %{POSINT:pid}#%{NUMBER:threadid}\: \*%{NUMBER:connectionid} %{DATA:text}(, %{GREEDYDATA:kv_value})?$
And then you use the kv filter on kv_value:
kv {
field_split => " ,"
value_spliy => ": "
trim_value => "\""
source => "kv_value"
}
In both cases, don't forget the $ at the end of the grok pattern, as it will fail to work properly without.

kubernetes nginx ingress rewrite-target not work

I am following rewrite-target guide from https://kubernetes.github.io/ingress-nginx/examples/rewrite/.
But it doesn't work as I expected
Here is my ingress.
kind: Ingress
metadata:
name: ingress-resource
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/enable-rewrite-log: "true"
spec:
rules:
- host: "test.gamedrive.cc"
http:
paths:
- path: /player-gateway(/|$)(.*)
pathType: Prefix
backend:
service:
name: player-gateway
port:
number: 80
If make a request to /player-gateway/health/startup
it should rewritten to /health/startup.
But rewritten data from Nginx is data: "/" and got 404 error code. Here is the log from Nginx
2021/11/07 06:58:03 [notice] 312#312: 26100
"(?i)/player-gateway(/|$)(.)" matches
"/player-gateway/health/startup", client: 113.53.150.16, server:
test.gamedrive.cc, request: "GET /player-gateway/health/startup
HTTP/1.1", host: "test.gamedrive.cc" 2021/11/07 06:58:03 [notice]
312#312: *26100 rewritten data: "/", args: "", client: 113.53.150.16,
server: test.gamedrive.cc, request: "GET
/player-gateway/health/startup HTTP/1.1", host: "test.gamedrive.cc"
113.53.150.16 - - [07/Nov/2021:06:58:03 +0000] "GET /player-gateway/health/startup HTTP/1.1" 404 139 "-"
"PostmanRuntime/7.28.4" 258 0.001 [default-player-gateway-80] []
10.244.0.109:80 139 0.000 404 e834c8338f2938ffc84db4e7e4053706
I try to debug by changed nginx.ingress.kubernetes.io/rewrite-target: /$2 to nginx.ingress.kubernetes.io/rewrite-target: /health/startup.
and the response code is 200 as expected
2021/11/07 07:04:32 [notice] 452#452: 29805
"(?i)/player-gateway(/|$)(.)" matches
"/player-gateway/health/startup", client: 113.53.150.16, server:
test.gamedrive.cc, request: "GET /player-gateway/health/startup
HTTP/1.1", host: "test.gamedrive.cc" 2021/11/07 07:04:32 [notice]
452#452: *29805 rewritten data: "/health/startup", args: "", client:
113.53.150.16, server: test.gamedrive.cc, request: "GET /player-gateway/health/startup HTTP/1.1", host: "test.gamedrive.cc"
113.53.150.16 - - [07/Nov/2021:07:04:32 +0000] "GET /player-gateway/health/startup HTTP/1.1" 200 2 "-"
"PostmanRuntime/7.28.4" 258 0.002 [default-player-gateway-80] []
10.244.0.109:80 2 0.000 200 66967d6ab87d542f3269860107a4b7c4
Seem like /$2 or (/|$)(.*) is not working.
Please help me solve this problem.
Thanks.

What request path is location = /bar supposed to match in Nginx?

What request path is location = /bar supposed to match in Nginx?
What works fine: location /bar
Here is my nginx configuration, host file configuration, and my HTML files.
# cat /etc/nginx/sites-enabled/foo
server {
listen 80;
listen [::]:80;
server_name foo;
root /tmp/;
location /bar/ {
alias /var/www/foo/;
}
}
# cat /etc/hosts
127.0.0.1 localhost foo
127.0.1.1 debian
# cat /tmp/index.html
Hi! I am Tmp!
# cat /var/www/foo/index.html
<p>Hi! I am Index!</p>
# cat /var/www/foo/max.html
<p>Hi! I am Max!</p>
HTTP requests to the root, /bar/, and /bar/max.html produce the expected
output:
# systemctl restart nginx && curl http://foo/
Hi! I am Tmp!
# systemctl restart nginx && curl http://foo/bar/
<p>Hi! I am Index!</p>
# systemctl restart nginx && curl http://foo/bar/max.html
<p>Hi! I am Max!</p>
What does not work fine: location = /bar
Now I edit the configuration to replace location /bar with location = /bar:
# cat /etc/nginx/sites-enabled/foo
server {
listen 80;
listen [::]:80;
server_name foo;
root /tmp/;
location = /bar/ {
alias /var/www/foo/;
}
}
# systemctl restart nginx && curl http://foo/
Hi! I am Tmp!
These HTTP requests no longer work:
# systemctl restart nginx && curl http://foo/bar/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
# tail -n 1 /var/log/nginx/error.log
2018/05/25 00:07:50 [error] 29157#29157: *1 open() "/tmp/bar/index.html" failed (2: No such file or directory), client: 127.0.0.1, server: foo, request: "GET /bar/ HTTP/1.1", host: "foo"
# systemctl restart nginx && curl http://foo/bar
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
# tail -n 1 /var/log/nginx/error.log
2018/05/25 00:08:49 [error] 29203#29203: *1 open() "/tmp/bar" failed (2: No such file or directory), client: 127.0.0.1, server: foo, request: "GET /bar HTTP/1.1", host: "foo"
# systemctl restart nginx && curl http://foo/bar/max.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
# tail -n 1 /var/log/nginx/error.log
2018/05/25 00:10:10 [error] 29265#29265: *1 open() "/tmp/bar/max.html" failed (2: No such file or directory), client: 127.0.0.1, server: foo, request: "GET /bar/max.html HTTP/1.1", host: "foo"
It appears that the GET requests for either /bar or /bar/ match the location = /bar/ directive? I thought these requests should have worked with this directive because http://nginx.org/en/docs/http/ngx_http_core_module.html#location mentions:
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.
But as explained in my example, it does not seem to work? What kind of request would match the location = /bar directive then?
The URI /bar/ relies on the index directive to internally rewrite the URI to /bar/index.html. See this document for details.
The exact match location syntax will only match the original URI, and not the rewritten URI.
nginx will process the rewritten URI using the default location (which in your configuration, is the server context). So, the URI /bar/index.html will be searched for at /tmp/bar/index.html, and not found.

lua entry thread aborted: runtime error: attempt to concatenate field 'cookie_sessionid' (a nil value)

/etc/nginx/sites-enabled/default:
location /pine {
add_header Access-Control-Allow-Origin http://localhost:6285;
add_header Access-Control-Allow-Credentials true;
access_by_lua_file /home/akuznetsov/auth.lua;
proxy_pass http://localhost:9100;
}
auth.lua (version 1, works fine):
ngx.req.set_header('x-user-id', ngx.var.cookie_sessionid)
auth.lua (version 2, not working):
ngx.req.set_header('x-user-id', 'session:' .. ngx.var.cookie_sessionid)
in /var/log/nginx/error.log i get this error:
2016/04/06 16:13:10 [error] 14183#0: *1 lua entry thread aborted: runtime error: /home/akuznetsov/auth.lua:2: attempt to concatenate field 'cookie_sessionid' (a nil value)
stack traceback:
coroutine 0:
/home/akuznetsov/auth.lua:2: in function </home/akuznetsov/auth.lua:1>, client: 127.0.0.1, server: localhost, request: "OPTIONS /pine HTTP/1.1", host: "localhost", referrer: "http://localhost:6285/chart/kjckCBcG/?pine=http://localhost/pine"
What's wrong with concat?
ngx.var.cookie_sessionid is nil and just as message tell you, you can't concatenate (i.e. ..) that. Provide if check with logic to handle this case or use ngx.req.set_header('x-user-id', 'session:' .. ngx.var.cookie_sessionid or "") if you okay with using empty string as default.

nginx = / location pattern not working

I am trying to configure nginx to serve a static html page on the root domain, and proxy everything else to uwsgi. As a quick test I tried to divert to two different static pages:
server {
server_name *.example.dev;
index index.html index.htm;
listen 80;
charset utf-8;
location = / {
root /www/src/;
}
location / {
root /www/test/;
}
}
This seems to be what http://nginx.org/en/docs/http/ngx_http_core_module.html#location says you can do. But I'm always getting sent to the test site, even on the / request by visiting http://www.example.dev in my browser.
Curl output:
$ curl http://www.example.dev -v
* Rebuilt URL to: http://www.example.dev/
* Hostname was NOT found in DNS cache
* Trying 192.168.50.51...
* Connected to www.example.dev (192.168.50.51) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.37.1
> Host: www.example.dev
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.8.0 is not blacklisted
< Server: nginx/1.8.0
< Date: Tue, 19 May 2015 01:11:10 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 415
< Last-Modified: Wed, 15 Apr 2015 02:53:27 GMT
< Connection: keep-alive
< ETag: "552dd2a7-19f"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
...
And the output from the nginx access log:
192.168.50.1 - - [19/May/2015:01:17:05 +0000] "GET / HTTP/1.1" 200 415 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36" "-"
So I decided to comment out the test location. So I have only the location = / { ... block. Nginx now 404s and logs the following error:
2015/05/19 01:24:12 [error] 3116#0: *6 open() "/etc/nginx/html/index.html" failed (2: No such file or directory), client: 192.168.50.1, server: *.example.dev, request: "GET / HTTP/1.1", host: "www.example.dev"
Which is the default root in the original nginx conf file? I guess this confirms my location = / pattern is not matching.
I added $uri to the access log and see that it is showing /index.html which I guess means the first location pattern is matching, but then it goes into the second location block? So now I just need to figure out how to serve my index.html from the / block, or just add another block like: location =/index.html
according to #Alexey Ten commented, in ngx_http_index doc:
It should be noted that using an index file causes an internal redirect, and the request can be processed in a different location. For example, with the following configuration:
location = / {
index index.html;
}
location / {
...
}
a “/” request will actually be processed in the second location as “/index.html”.
In your case, request to "/" will not get /www/src/index.html, but /www/test/index.html.

Resources