Skip to content

Commit fbb4076

Browse files
committed
Add static files for GRPC error pages and locations
1 parent 4e0f510 commit fbb4076

File tree

8 files changed

+87
-82
lines changed

8 files changed

+87
-82
lines changed

build/Dockerfile.nginx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ RUN apk add --no-cache libcap \
1313

1414
COPY ${NJS_DIR}/httpmatches.js /usr/lib/nginx/modules/njs/httpmatches.js
1515
COPY ${NGINX_CONF_DIR}/nginx.conf /etc/nginx/nginx.conf
16+
COPY ${NGINX_CONF_DIR}/grpc-error-locations.conf /etc/nginx/grpc-error-locations.conf
17+
COPY ${NGINX_CONF_DIR}/grpc-error-pages.conf /etc/nginx/grpc-error-pages.conf
1618

1719
RUN chown -R 101:1001 /etc/nginx /var/cache/nginx /var/lib/nginx
1820

build/Dockerfile.nginxplus

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ RUN --mount=type=secret,id=nginx-repo.crt,dst=/etc/apk/cert.pem,mode=0644 \
2929

3030
COPY ${NJS_DIR}/httpmatches.js /usr/lib/nginx/modules/njs/httpmatches.js
3131
COPY ${NGINX_CONF_DIR}/nginx-plus.conf /etc/nginx/nginx.conf
32+
COPY ${NGINX_CONF_DIR}/grpc-error-locations.conf /etc/nginx/grpc-error-locations.conf
33+
COPY ${NGINX_CONF_DIR}/grpc-error-pages.conf /etc/nginx/grpc-error-pages.conf
3234

3335
RUN chown -R 101:1001 /etc/nginx /var/cache/nginx /var/lib/nginx
3436

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
location @grpc_deadline_exceeded {
2+
default_type application/grpc;
3+
add_header content-type application/grpc;
4+
add_header grpc-status 4;
5+
add_header grpc-message 'deadline exceeded';
6+
return 204;
7+
}
8+
9+
location @grpc_permission_denied {
10+
default_type application/grpc;
11+
add_header content-type application/grpc;
12+
add_header grpc-status 7;
13+
add_header grpc-message 'permission denied';
14+
return 204;
15+
}
16+
17+
location @grpc_resource_exhausted {
18+
default_type application/grpc;
19+
add_header content-type application/grpc;
20+
add_header grpc-status 8;
21+
add_header grpc-message 'resource exhausted';
22+
return 204;
23+
}
24+
25+
location @grpc_unimplemented {
26+
default_type application/grpc;
27+
add_header content-type application/grpc;
28+
add_header grpc-status 12;
29+
add_header grpc-message unimplemented;
30+
return 204;
31+
}
32+
33+
location @grpc_internal {
34+
default_type application/grpc;
35+
add_header content-type application/grpc;
36+
add_header grpc-status 13;
37+
add_header grpc-message 'internal error';
38+
return 204;
39+
}
40+
41+
location @grpc_unavailable {
42+
default_type application/grpc;
43+
add_header content-type application/grpc;
44+
add_header grpc-status 14;
45+
add_header grpc-message unavailable;
46+
return 204;
47+
}
48+
49+
location @grpc_unauthenticated {
50+
default_type application/grpc;
51+
add_header content-type application/grpc;
52+
add_header grpc-status 16;
53+
add_header grpc-message unauthenticated;
54+
return 204;
55+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error_page 400 = @grpc_internal;
2+
error_page 401 = @grpc_unauthenticated;
3+
error_page 403 = @grpc_permission_denied;
4+
error_page 404 = @grpc_unimplemented;
5+
error_page 429 = @grpc_unavailable;
6+
error_page 502 = @grpc_unavailable;
7+
error_page 503 = @grpc_unavailable;
8+
error_page 504 = @grpc_unavailable;
9+
error_page 405 = @grpc_internal;
10+
error_page 408 = @grpc_deadline_exceeded;
11+
error_page 413 = @grpc_resource_exhausted;
12+
error_page 414 = @grpc_resource_exhausted;
13+
error_page 415 = @grpc_internal;
14+
error_page 426 = @grpc_internal;
15+
error_page 495 = @grpc_unauthenticated;
16+
error_page 496 = @grpc_unauthenticated;
17+
error_page 497 = @grpc_internal;
18+
error_page 500 = @grpc_internal;
19+
error_page 501 = @grpc_internal;

internal/mode/static/nginx/config/http/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type Server struct {
77
Locations []Location
88
IsDefaultHTTP bool
99
IsDefaultSSL bool
10-
HTTP2 bool
10+
GRPC bool
1111
Port int32
1212
}
1313

internal/mode/static/nginx/config/servers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func createSSLServer(virtualServer dataplane.VirtualServer) http.Server {
7676
},
7777
Locations: locs,
7878
Port: virtualServer.Port,
79-
HTTP2: http2,
79+
GRPC: http2,
8080
}
8181
}
8282

@@ -94,7 +94,7 @@ func createServer(virtualServer dataplane.VirtualServer) http.Server {
9494
ServerName: virtualServer.Hostname,
9595
Locations: locs,
9696
Port: virtualServer.Port,
97-
HTTP2: http2,
97+
GRPC: http2,
9898
}
9999
}
100100

internal/mode/static/nginx/config/servers_template.go

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ server {
1717
}
1818
{{- else }}
1919
server {
20-
2120
{{- if $s.SSL }}
2221
listen {{ $s.Port }} ssl;
2322
ssl_certificate {{ $s.SSL.Certificate }};
@@ -50,25 +49,7 @@ server {
5049
{{ $proxyOrGRPC := "proxy" }}{{ if $l.IsGRPC }}{{ $proxyOrGRPC = "grpc" }}{{ end }}
5150
5251
{{- if $l.IsGRPC }}
53-
error_page 400 = @grpc_internal;
54-
error_page 401 = @grpc_unauthenticated;
55-
error_page 403 = @grpc_permission_denied;
56-
error_page 404 = @grpc_unimplemented;
57-
error_page 429 = @grpc_unavailable;
58-
error_page 502 = @grpc_unavailable;
59-
error_page 503 = @grpc_unavailable;
60-
error_page 504 = @grpc_unavailable;
61-
error_page 405 = @grpc_internal;
62-
error_page 408 = @grpc_deadline_exceeded;
63-
error_page 413 = @grpc_resource_exhausted;
64-
error_page 414 = @grpc_resource_exhausted;
65-
error_page 415 = @grpc_internal;
66-
error_page 426 = @grpc_internal;
67-
error_page 495 = @grpc_unauthenticated;
68-
error_page 496 = @grpc_unauthenticated;
69-
error_page 497 = @grpc_internal;
70-
error_page 500 = @grpc_internal;
71-
error_page 501 = @grpc_internal;
52+
include /etc/nginx/grpc-error-pages.conf;
7253
{{- end }}
7354
7455
{{- if $l.ProxyPass -}}
@@ -86,63 +67,9 @@ server {
8667
}
8768
{{ end }}
8869
89-
{{- if $s.HTTP2 }}
90-
location @grpc_deadline_exceeded {
91-
default_type application/grpc;
92-
add_header content-type application/grpc;
93-
add_header grpc-status 4;
94-
add_header grpc-message 'deadline exceeded';
95-
return 204;
96-
}
97-
98-
location @grpc_permission_denied {
99-
default_type application/grpc;
100-
add_header content-type application/grpc;
101-
add_header grpc-status 7;
102-
add_header grpc-message 'permission denied';
103-
return 204;
104-
}
105-
106-
location @grpc_resource_exhausted {
107-
default_type application/grpc;
108-
add_header content-type application/grpc;
109-
add_header grpc-status 8;
110-
add_header grpc-message 'resource exhausted';
111-
return 204;
112-
}
113-
114-
location @grpc_unimplemented {
115-
default_type application/grpc;
116-
add_header content-type application/grpc;
117-
add_header grpc-status 12;
118-
add_header grpc-message unimplemented;
119-
return 204;
120-
}
121-
122-
location @grpc_internal {
123-
default_type application/grpc;
124-
add_header content-type application/grpc;
125-
add_header grpc-status 13;
126-
add_header grpc-message 'internal error';
127-
return 204;
128-
}
129-
130-
location @grpc_unavailable {
131-
default_type application/grpc;
132-
add_header content-type application/grpc;
133-
add_header grpc-status 14;
134-
add_header grpc-message unavailable;
135-
return 204;
136-
}
137-
138-
location @grpc_unauthenticated {
139-
default_type application/grpc;
140-
add_header content-type application/grpc;
141-
add_header grpc-status 16;
142-
add_header grpc-message unauthenticated;
143-
return 204;
144-
}
145-
{{- end }}
70+
{{- if $s.GRPC }}
71+
include /etc/nginx/grpc-error-locations.conf;
72+
{{- end }}
14673
}
14774
{{- end }}
14875
{{ end }}

internal/mode/static/nginx/config/servers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ func TestCreateServers(t *testing.T) {
847847
ServerName: "cafe.example.com",
848848
Locations: getExpectedLocations(false),
849849
Port: 8080,
850-
HTTP2: true,
850+
GRPC: true,
851851
},
852852
{
853853
IsDefaultSSL: true,
@@ -861,7 +861,7 @@ func TestCreateServers(t *testing.T) {
861861
},
862862
Locations: getExpectedLocations(true),
863863
Port: 8443,
864-
HTTP2: true,
864+
GRPC: true,
865865
},
866866
}
867867

0 commit comments

Comments
 (0)