Skip to content

Commit 9484eca

Browse files
committed
niginx adjustments
1 parent 8fade17 commit 9484eca

3 files changed

Lines changed: 22 additions & 100 deletions

File tree

docs/hands-on/7/docker/index.md

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Two changes are needed in `compose.yaml`: add the `nginx` service as the new ent
77
image: nginx:alpine
88
hostname: nginx
99
ports:
10-
- 80:80
10+
- 8080:80
1111
volumes:
1212
- $SETUP/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
1313
depends_on:
@@ -24,7 +24,7 @@ SETUP=./setup
2424

2525
Remove the `ports` mapping from the `gateway` service and add a `deploy` block to set the replica count:
2626

27-
``` { .yaml .copy .select title="compose.yaml" hl_lines="5 6 7" }
27+
``` { .yaml .copy .select title="compose.yaml" hl_lines="5" }
2828
gateway:
2929
build:
3030
context: ./gateway-service
@@ -40,79 +40,3 @@ Remove the `ports` mapping from the `gateway` service and add a `deploy` block t
4040
!!! warning "Remove the `hostname` field from gateway"
4141

4242
If the `gateway` service had a `hostname: gateway` entry, remove it. Docker Compose cannot assign the same hostname to multiple containers — the field is silently ignored when `replicas > 1`. The DNS service name `gateway` continues to work correctly for internal communication.
43-
44-
## Full `compose.yaml`
45-
46-
The complete file after all changes:
47-
48-
``` { .yaml .copy .select linenums="1" title="compose.yaml" }
49-
name: store
50-
51-
services:
52-
53-
db:
54-
image: postgres:17
55-
hostname: db
56-
ports:
57-
- 5432:5432
58-
volumes:
59-
- ${VOLUME_DB}:/var/lib/postgresql/data
60-
environment:
61-
POSTGRES_USER: ${DB_USER:-store}
62-
POSTGRES_PASSWORD: ${DB_PASSWORD:-devpass}
63-
POSTGRES_DB: ${DB_NAME:-store}
64-
65-
account:
66-
build:
67-
context: ./account-service
68-
dockerfile: Dockerfile
69-
hostname: account
70-
environment:
71-
DATABASE_HOST: db
72-
DATABASE_PORT: 5432
73-
DATABASE_DB: ${DB_NAME:-store}
74-
DATABASE_USERNAME: ${DB_USER:-store}
75-
DATABASE_PASSWORD: ${DB_PASSWORD:-devpass}
76-
deploy:
77-
replicas: 2
78-
depends_on:
79-
- db
80-
81-
auth:
82-
build:
83-
context: ./auth-service
84-
dockerfile: Dockerfile
85-
hostname: auth
86-
environment:
87-
DATABASE_HOST: db
88-
DATABASE_PORT: 5432
89-
DATABASE_DB: ${DB_NAME:-store}
90-
DATABASE_USERNAME: ${DB_USER:-store}
91-
DATABASE_PASSWORD: ${DB_PASSWORD:-devpass}
92-
depends_on:
93-
- db
94-
95-
gateway:
96-
build:
97-
context: ./gateway-service
98-
dockerfile: Dockerfile
99-
deploy:
100-
replicas: 3
101-
depends_on:
102-
- account
103-
- auth
104-
105-
nginx:
106-
image: nginx:alpine
107-
hostname: nginx
108-
ports:
109-
- 80:80
110-
volumes:
111-
- $SETUP/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
112-
depends_on:
113-
- gateway
114-
```
115-
116-
!!! info "Port mapping change"
117-
118-
The platform now listens on **port 80** instead of 8080. All API calls, Prometheus scrape targets, and test scripts must be updated accordingly: `http://localhost/gateway/...` instead of `http://localhost:8080/gateway/...`.

docs/hands-on/7/nginx/index.md

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,14 @@ Nginx is configured as an [HTTP reverse proxy](https://nginx.org/en/docs/http/ng
33
Create the file `setup/nginx/nginx.conf`:
44

55
``` { .nginx .copy .select linenums="1" title="nginx.conf" }
6-
events {}
7-
8-
http {
9-
10-
upstream gateway_cluster {
11-
least_conn;
12-
server gateway:8080;
13-
}
14-
15-
server {
16-
listen 80;
17-
18-
location / {
19-
proxy_pass http://gateway_cluster;
20-
proxy_set_header Host $host;
21-
proxy_set_header X-Real-IP $remote_addr;
22-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
23-
proxy_set_header X-Forwarded-Proto $scheme;
24-
}
25-
}
26-
}
6+
--8<-- "docs/hands-on/7/nginx/nginx.conf"
277
```
288

299
Key directives:
3010

3111
| Directive | Purpose |
3212
|---|---|
33-
| `upstream gateway_cluster` | Names a pool of backend servers. Docker DNS expands `gateway:8080` to all replica IPs at startup. |
13+
| `upstream gateways` | Names a pool of backend servers. Docker DNS expands `gateway:8080` to all replica IPs at startup. |
3414
| `least_conn` | Load balancing algorithm: each new request goes to the replica with the fewest active connections. |
3515
| `proxy_pass` | Forwards the matched request to the upstream pool. |
3616
| `X-Real-IP` | Passes the original client IP address to the backend service. |

docs/hands-on/7/nginx/nginx.conf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
http {
2+
3+
upstream gateways {
4+
server gateway:8080;
5+
}
6+
7+
server {
8+
listen 80;
9+
10+
location / {
11+
proxy_pass http://gateways;
12+
proxy_set_header Host $host;
13+
proxy_set_header X-Real-IP $remote_addr;
14+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
15+
proxy_set_header X-Forwarded-Proto $scheme;
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)