Skip to content

Commit

Permalink
add load balancing config example
Browse files Browse the repository at this point in the history
  • Loading branch information
dolugen committed Nov 22, 2020
1 parent ffa7a13 commit e9a1843
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
43 changes: 34 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,72 @@
# nginx lab

Getting the image:
- [Getting the image](#getting-the-image)
- [Run the bare image](#run-the-bare-image)
- [Configuration](#configuration)
- [Proxy server](#proxy-server)
- [Match request path with regex](#match-request-path-with-regex)
- [Load balancing](#load-balancing)

## Getting the image

```
docker pull nginx
```

Run the bare image:
## Run the bare image

```
docker run -d -p 80:80 nginx
docker run --rm -d -p 80:80 nginx
curl localhost
```

You should get the **Welcome to nginx** web page.

## Configuration

Configuration directives's hierachy:
- `main` context contains `events` and `http`
- `http` contains `server`
- `server` contains `location`


Loading a minimal configuration (see `./nginx.conf` for details):
```
docker run -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro -v $(pwd)/data:/data:ro -p80:80 nginx
docker run --rm -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro -v $(pwd)/data:/data:ro -p80:80 nginx
```

Then you should be able to get:
- http://localhost
- http://localhost/images/example.png

Proxy server (see `nginx-proxy.conf`):
## Proxy server

See `nginx-proxy.conf`.

```
docker run -v $(pwd)/nginx-proxy.conf:/etc/nginx/nginx.conf:ro -v $(pwd)/data:/data:ro -p80:80 nginx
docker run --rm -v $(pwd)/nginx-proxy.conf:/etc/nginx/nginx.conf:ro -v $(pwd)/data:/data:ro -p80:80 nginx
curl localhost
```

Match request path with regex (see `nginx-regex.conf`):
## Match request path with regex

See `nginx-regex.conf`.

```
docker run -v $(pwd)/nginx-regex.conf:/etc/nginx/nginx.conf:ro -v $(pwd)/data:/data:ro -p80:80 nginx
docker run --rm -v $(pwd)/nginx-regex.conf:/etc/nginx/nginx.conf:ro -v $(pwd)/data:/data:ro -p80:80 nginx
```

- http://localhost/example.png

## Load balancing

See `nginx-lb.conf`.

```
docker run --rm -d -v $(pwd)/nginx-lb.conf:/etc/nginx/nginx.conf:ro -v $(pwd)/data:/data:ro -p 80:80 nginx
```

Now if you keep reading from http://localhost, it will respond with these responses in round-robin order:

- hello from site 1
- hello from site 2
- hello from site 3
5 changes: 5 additions & 0 deletions data/site1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<p>hello from site 1</p>
</body>
</html>
5 changes: 5 additions & 0 deletions data/site2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<p>hello from site 2</p>
</body>
</html>
5 changes: 5 additions & 0 deletions data/site3/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<p>hello from site 3</p>
</body>
</html>
29 changes: 29 additions & 0 deletions nginx-lb.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
http {
upstream myservers {
server 127.0.0.1:8081;
server 127.0.0.1:8082;
server 127.0.0.1:8083;
}

server {
listen 8081;
root /data/site1;
}

server {
listen 8082;
root /data/site2;
}

server {
listen 8083;
root /data/site3;
}

server {
location / {
proxy_pass http://myservers/;
}
}
}
events { }

0 comments on commit e9a1843

Please sign in to comment.