Skip to content

Commit e0c7747

Browse files
authored
Fix IP address as name detection in nginx conf (#1060)
Adds lexer rule to detect IP address _without_ a port. I've created two commits for this PR. The first generates the test snapshot files with the existing ruleset, the second adds the new rule and updates the snapshot. Noticed as part of the [NGINX docs](https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/#proxying-http-traffic-to-a-group-of-servers), you can see the `192` being highlighted by itself. Let me know if there are any conventions I missed, or tweaks I should make. Thanks!
1 parent 0bf0e9f commit e0c7747

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

lexers/embedded/nginx_configuration_file.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
<rule pattern="\$[^\s;#()]+">
5959
<token type="NameVariable"/>
6060
</rule>
61+
<rule pattern="(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)">
62+
<token type="Name"/>
63+
</rule>
6164
<rule pattern="([a-z0-9.-]+)(:)([0-9]+)">
6265
<bygroups>
6366
<token type="Name"/>

lexers/testdata/nginx.actual

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
http {
2+
upstream app_backend {
3+
server 127.0.0.1;
4+
server 127.0.0.1:8082;
5+
server backend.com;
6+
server backend-2.com weight=5;
7+
server backend-backup.org backup;
8+
}
9+
10+
map $http_host $site_root {
11+
default /var/www/default; # Default root
12+
example.com /var/www/example_com;
13+
example.org /var/www/example_org;
14+
}
15+
16+
server {
17+
listen 80 default_server;
18+
server_name _;
19+
20+
root /var/www/default;
21+
22+
location / {
23+
index index.html;
24+
}
25+
26+
location /status {
27+
return 200 "Default server status: OK\n";
28+
add_header Content-Type text/plain;
29+
}
30+
}
31+
}

lexers/testdata/nginx.expected

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
[
2+
{"type":"Keyword","value":"http"},
3+
{"type":"Text","value":" "},
4+
{"type":"Punctuation","value":"{"},
5+
{"type":"Text","value":"\n "},
6+
{"type":"KeywordNamespace","value":"upstream"},
7+
{"type":"Text","value":" "},
8+
{"type":"LiteralString","value":"app_backend"},
9+
{"type":"Text","value":" "},
10+
{"type":"Punctuation","value":"{"},
11+
{"type":"Text","value":"\n "},
12+
{"type":"KeywordNamespace","value":"server"},
13+
{"type":"Text","value":" "},
14+
{"type":"Name","value":"127.0.0.1"},
15+
{"type":"Punctuation","value":";"},
16+
{"type":"Text","value":"\n "},
17+
{"type":"KeywordNamespace","value":"server"},
18+
{"type":"Text","value":" "},
19+
{"type":"Name","value":"127.0.0.1"},
20+
{"type":"Punctuation","value":":"},
21+
{"type":"LiteralNumberInteger","value":"8082"},
22+
{"type":"Punctuation","value":";"},
23+
{"type":"Text","value":"\n "},
24+
{"type":"KeywordNamespace","value":"server"},
25+
{"type":"Text","value":" "},
26+
{"type":"LiteralString","value":"backend.com"},
27+
{"type":"Punctuation","value":";"},
28+
{"type":"Text","value":"\n "},
29+
{"type":"KeywordNamespace","value":"server"},
30+
{"type":"Text","value":" "},
31+
{"type":"LiteralString","value":"backend-2.com"},
32+
{"type":"Text","value":" "},
33+
{"type":"LiteralString","value":"weight=5"},
34+
{"type":"Punctuation","value":";"},
35+
{"type":"Text","value":"\n "},
36+
{"type":"KeywordNamespace","value":"server"},
37+
{"type":"Text","value":" "},
38+
{"type":"LiteralString","value":"backend-backup.org"},
39+
{"type":"Text","value":" "},
40+
{"type":"LiteralString","value":"backup"},
41+
{"type":"Punctuation","value":";"},
42+
{"type":"Text","value":"\n "},
43+
{"type":"Punctuation","value":"}"},
44+
{"type":"Text","value":"\n\n "},
45+
{"type":"KeywordNamespace","value":"map"},
46+
{"type":"Text","value":" "},
47+
{"type":"NameVariable","value":"$http_host"},
48+
{"type":"Text","value":" "},
49+
{"type":"NameVariable","value":"$site_root"},
50+
{"type":"Text","value":" "},
51+
{"type":"Punctuation","value":"{"},
52+
{"type":"Text","value":"\n "},
53+
{"type":"KeywordNamespace","value":"default"},
54+
{"type":"Text","value":" "},
55+
{"type":"LiteralString","value":"/var/www/default"},
56+
{"type":"Punctuation","value":";"},
57+
{"type":"Text","value":" "},
58+
{"type":"CommentSingle","value":"# Default root\n"},
59+
{"type":"Text","value":" "},
60+
{"type":"KeywordNamespace","value":"example.com"},
61+
{"type":"Text","value":" "},
62+
{"type":"LiteralString","value":"/var/www/example_com"},
63+
{"type":"Punctuation","value":";"},
64+
{"type":"Text","value":"\n "},
65+
{"type":"KeywordNamespace","value":"example.org"},
66+
{"type":"Text","value":" "},
67+
{"type":"LiteralString","value":"/var/www/example_org"},
68+
{"type":"Punctuation","value":";"},
69+
{"type":"Text","value":"\n "},
70+
{"type":"Punctuation","value":"}"},
71+
{"type":"Text","value":"\n\n "},
72+
{"type":"KeywordNamespace","value":"server"},
73+
{"type":"Text","value":" "},
74+
{"type":"Punctuation","value":"{"},
75+
{"type":"Text","value":"\n "},
76+
{"type":"KeywordNamespace","value":"listen"},
77+
{"type":"Text","value":" "},
78+
{"type":"LiteralNumberInteger","value":"80"},
79+
{"type":"Text","value":" "},
80+
{"type":"LiteralString","value":"default_server"},
81+
{"type":"Punctuation","value":";"},
82+
{"type":"Text","value":"\n "},
83+
{"type":"KeywordNamespace","value":"server_name"},
84+
{"type":"Text","value":" "},
85+
{"type":"LiteralString","value":"_"},
86+
{"type":"Punctuation","value":";"},
87+
{"type":"Text","value":"\n\n "},
88+
{"type":"KeywordNamespace","value":"root"},
89+
{"type":"Text","value":" "},
90+
{"type":"LiteralString","value":"/var/www/default"},
91+
{"type":"Punctuation","value":";"},
92+
{"type":"Text","value":"\n\n "},
93+
{"type":"KeywordNamespace","value":"location"},
94+
{"type":"Text","value":" "},
95+
{"type":"LiteralString","value":"/"},
96+
{"type":"Text","value":" "},
97+
{"type":"Punctuation","value":"{"},
98+
{"type":"Text","value":"\n "},
99+
{"type":"KeywordNamespace","value":"index"},
100+
{"type":"Text","value":" "},
101+
{"type":"LiteralString","value":"index.html"},
102+
{"type":"Punctuation","value":";"},
103+
{"type":"Text","value":"\n "},
104+
{"type":"Punctuation","value":"}"},
105+
{"type":"Text","value":"\n\n "},
106+
{"type":"KeywordNamespace","value":"location"},
107+
{"type":"Text","value":" "},
108+
{"type":"LiteralString","value":"/status"},
109+
{"type":"Text","value":" "},
110+
{"type":"Punctuation","value":"{"},
111+
{"type":"Text","value":"\n "},
112+
{"type":"KeywordNamespace","value":"return"},
113+
{"type":"Text","value":" "},
114+
{"type":"LiteralNumberInteger","value":"200"},
115+
{"type":"Text","value":" "},
116+
{"type":"LiteralString","value":"\"Default"},
117+
{"type":"Text","value":" "},
118+
{"type":"LiteralString","value":"server"},
119+
{"type":"Text","value":" "},
120+
{"type":"LiteralString","value":"status:"},
121+
{"type":"Text","value":" "},
122+
{"type":"LiteralString","value":"OK\\n\""},
123+
{"type":"Punctuation","value":";"},
124+
{"type":"Text","value":"\n "},
125+
{"type":"KeywordNamespace","value":"add_header"},
126+
{"type":"Text","value":" "},
127+
{"type":"LiteralString","value":"Content-Type"},
128+
{"type":"Text","value":" "},
129+
{"type":"LiteralString","value":"text/plain"},
130+
{"type":"Punctuation","value":";"},
131+
{"type":"Text","value":"\n "},
132+
{"type":"Punctuation","value":"}"},
133+
{"type":"Text","value":"\n "},
134+
{"type":"Punctuation","value":"}"},
135+
{"type":"Text","value":"\n"},
136+
{"type":"Punctuation","value":"}"}
137+
]

0 commit comments

Comments
 (0)