Skip to content

Commit d39a0a4

Browse files
Custom listen ports document (#3715)
1 parent adc42b7 commit d39a0a4

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
title: Customizing NGINX Ingress Controller Ports
3+
description: |
4+
How to customize F5 NGINX Ingress Controller ports.
5+
weight: 1800
6+
doctypes: ["concept"]
7+
toc: true
8+
---
9+
## Customizing NGINX Ingress Controller Ports
10+
11+
This document explains how to change the default ports that NGINX Ingress Controller is configured to use, as well as how to add additional `listen` settings. For more information, please read the [NGINX Listen documentation](http://nginx.org/en/docs/http/ngx_http_core_module.html#listen).
12+
13+
## Changing Default Ports
14+
15+
By default, NGINX Ingress Controller listens on ports 80 and 443. These ports can be changed easily, but modifying the `listen` ports for your NGINX Ingress resources will require the editing of `.tmpl` files.
16+
17+
If you are using NGINX Ingress Controller CRDs (VirtualServer):
18+
- `nginx-plus-virtualserver.tmpl` for NGINX Plus
19+
- `nginx-virtualserver.tmpl` if using NGINX OSS
20+
21+
If you are using `Ingress` resource you will need to modify:
22+
- `nginx-plus-ingress.tmpl` if using NGINX Plus
23+
- `nginx-ingress.tmpl` if using NGINX OSS
24+
25+
In this example, we will use the `nginx-virtualserver.tmpl` template to change the port from 80 to 85.
26+
You can find the [nginx-virtualserver template files in our repository](https://github.com/nginxinc/kubernetes-ingress/tree/main/internal/configs/version2).
27+
28+
We start by modifying `nginx-virtualserver.tmpl` to change the port setting:
29+
30+
```nginx
31+
server {
32+
listen 80{{ if $s.ProxyProtocol }} proxy_protocol{{ end }};
33+
34+
server_name {{ $s.ServerName }};
35+
36+
set $resource_type "virtualserver";
37+
set $resource_name "{{$s.VSName}}";
38+
set $resource_namespace "{{$s.VSNamespace}}";
39+
```
40+
To change the listen port from `80` to `85`, edit the `listen` line at the start of the server configuration block.
41+
42+
After changing the number, the file looks like this:
43+
44+
```nginx
45+
server {
46+
listen 85{{ if $s.ProxyProtocol }} proxy_protocol{{ end }};
47+
48+
server_name {{ $s.ServerName }};
49+
50+
set $resource_type "virtualserver";
51+
set $resource_name "{{$s.VSName}}";
52+
set $resource_namespace "{{$s.VSNamespace}}";
53+
```
54+
55+
Modify the file you need (per the example above). In the example, we modified `nginx-plus-virtualserver.tmpl`:
56+
57+
58+
## Rebuild the NGINX Ingress Controller image
59+
60+
You must rebuild the NGINX Ingress Controller image for the new port settings to take effect.
61+
Once the image is built and pushed, make sure you update your deployment to point to the new image and deploy.
62+
Once deployed, create a new `VirtualServer` resource and run `nginx -T` to confirm if the port change has taken effect.
63+
64+
Ensure that your `Deployment` and your `Service` match up to the new port you configured in the templates.
65+
Below is an example of `Deployment` and `Service` matching to the new port that NGINX Ingress Controller now listens on.
66+
67+
```yaml
68+
apiVersion: apps/v1
69+
kind: Deployment
70+
metadata:
71+
name: nginx-ingress
72+
namespace: nginx-ingress
73+
spec:
74+
replicas: 1
75+
selector:
76+
matchLabels:
77+
app: nginx-ingress
78+
template:
79+
metadata:
80+
labels:
81+
app: nginx-ingress
82+
annotations:
83+
prometheus.io/scrape: "true"
84+
prometheus.io/port: "9113"
85+
prometheus.io/scheme: http
86+
spec:
87+
serviceAccountName: nginx-ingress
88+
containers:
89+
- image: nginx/nginx-ingress:3.0.2
90+
imagePullPolicy: IfNotPresent
91+
name: nginx-ingress
92+
ports:
93+
- name: http
94+
containerPort: 85
95+
- name: https
96+
containerPort: 443
97+
- name: readiness-port
98+
containerPort: 8081
99+
- name: prometheus
100+
containerPort: 9113
101+
readinessProbe:
102+
httpGet:
103+
path: /nginx-ready
104+
port: readiness-port
105+
periodSeconds: 1
106+
securityContext:
107+
```
108+
109+
Notice that now, the `http` port is set to `85`, which reflects the change we made in the template file.
110+
111+
Here is the `service` file:
112+
113+
```yaml
114+
apiVersion: v1
115+
kind: Service
116+
metadata:
117+
name: nginx-ingress
118+
namespace: nginx-ingress
119+
spec:
120+
externalTrafficPolicy: Local
121+
type: LoadBalancer
122+
ports:
123+
- port: 80
124+
targetPort: 85
125+
protocol: TCP
126+
name: http
127+
- port: 8443
128+
targetPort: 8443
129+
protocol: TCP
130+
name: https
131+
selector:
132+
app: nginx-ingress
133+
```
134+
135+
Since NGINX Ingress Controller is now listening on ports 85 and 8443, you must modify the `targetPort` in the NGINX Ingress Controller service to match the change in the deployment to ensure traffic will be sent to the proper port.
136+
137+
The parameter to change above is `targetPort`. Since we have changed NGINX Ingress Controller to listen on port 85, we need to match that in the service: requests will be sent to NGINX Ingress Controller on port 85 instead of the default value, port 80.
138+
139+
If you view the `NGINX` configuration .conf file using `nginx -T`, you should see the port you defined in the .template file is now set on the `listen` line.
140+
141+
Here is an example output of the `NGINX` configuration that has been generated:
142+
143+
```console
144+
kubectl exec -it -n nginx-ingress nginx-ingress-54bffd78d9-v7bns -- nginx -T
145+
```
146+
147+
```nginx
148+
server {
149+
listen 85;
150+
listen [::]:85;
151+
listen 8011;
152+
153+
server_name cafe.example.com;
154+
155+
set $resource_type "virtualserver";
156+
set $resource_name "cafe";
157+
set $resource_namespace "default";
158+
```

0 commit comments

Comments
 (0)