|
1 | 1 | # Nginx Reverse Proxy |
2 | 2 |
|
3 | | -A basic nginx web server Docker configuration. |
| 3 | +A nginx reverse proxy configuration that routes requests to different backend services based on subdomain. |
| 4 | + |
| 5 | +## Configuration |
| 6 | + |
| 7 | +This reverse proxy is configured to forward requests to different ports based on subdomain. Each subdomain maps to a specific service defined in `docker-compose.yml`. |
| 8 | + |
| 9 | +### Environment Variables |
| 10 | + |
| 11 | +The domain used for subdomain routing can be configured using the `DOMAIN` environment variable. This allows you to use different domains for different environments (dev, test, prod). |
| 12 | + |
| 13 | +**Default:** `sample-dev.com` (if `DOMAIN` is not set) |
| 14 | + |
| 15 | +**Examples:** |
| 16 | +- Development: `DOMAIN=sample-dev.com` |
| 17 | +- Test: `DOMAIN=sample-test.com` |
| 18 | +- Production: `DOMAIN=sample-prod.com` |
| 19 | + |
| 20 | +### Subdomain Routing |
| 21 | + |
| 22 | +The following subdomains are configured (using `${DOMAIN}` as the base domain): |
| 23 | + |
| 24 | +- `js-client-side.${DOMAIN}` → JavaScript SDK Client Side (port 3031) |
| 25 | +- `js-client-server.${DOMAIN}` → JavaScript SDK Client Server (port 3032) |
| 26 | +- `js-react.${DOMAIN}` → JavaScript SDK React Client Side (port 3034) |
| 27 | +- `server-side.${DOMAIN}` → Server Side Integration (port 3033) |
| 28 | +- `secure-signals-client-server.${DOMAIN}` → Google Secure Signals Client Server (port 3041) |
| 29 | +- `secure-signals-client-side.${DOMAIN}` → Google Secure Signals Client Side (port 3042) |
| 30 | +- `secure-signals-server-side.${DOMAIN}` → Google Secure Signals Server Side (port 3043) |
| 31 | +- `secure-signals-react.${DOMAIN}` → Google Secure Signals React Client Side (port 3044) |
| 32 | +- `prebid-client.${DOMAIN}` → Prebid Client Side (port 3051) |
| 33 | +- `prebid-client-server.${DOMAIN}` → Prebid Client Server (port 3052) |
| 34 | +- `prebid-secure-signals.${DOMAIN}` → Prebid Secure Signals Client Side (port 3061) |
| 35 | + |
| 36 | +**Example with default domain (`sample-dev.com`):** |
| 37 | +- `js-client-side.sample-dev.com` → JavaScript SDK Client Side (port 3031) |
| 38 | +- `js-client-server.sample-dev.com` → JavaScript SDK Client Server (port 3032) |
| 39 | +- etc. |
| 40 | + |
| 41 | +## Required Hosts File Configuration |
| 42 | + |
| 43 | +To use the subdomain-based routing, you must add entries to your hosts file so that these subdomains resolve to localhost. |
| 44 | + |
| 45 | +**Note:** Replace `sample-dev.com` with your configured `DOMAIN` value in the examples below. |
| 46 | + |
| 47 | +### Windows |
| 48 | + |
| 49 | +1. Open Notepad (or your preferred text editor) **as Administrator** |
| 50 | + - Right-click Notepad → "Run as administrator" |
| 51 | + - Or use PowerShell as Administrator |
| 52 | + |
| 53 | +2. Open the hosts file: |
| 54 | + ``` |
| 55 | + C:\Windows\System32\drivers\etc\hosts |
| 56 | + ``` |
| 57 | + |
| 58 | +3. Add the following entries at the end of the file: |
| 59 | + ``` |
| 60 | + 127.0.0.1 js-client-side.sample-dev.com |
| 61 | + 127.0.0.1 js-client-server.sample-dev.com |
| 62 | + 127.0.0.1 js-react.sample-dev.com |
| 63 | + 127.0.0.1 server-side.sample-dev.com |
| 64 | + 127.0.0.1 secure-signals-client-server.sample-dev.com |
| 65 | + 127.0.0.1 secure-signals-client-side.sample-dev.com |
| 66 | + 127.0.0.1 secure-signals-server-side.sample-dev.com |
| 67 | + 127.0.0.1 secure-signals-react.sample-dev.com |
| 68 | + 127.0.0.1 prebid-client.sample-dev.com |
| 69 | + 127.0.0.1 prebid-client-server.sample-dev.com |
| 70 | + 127.0.0.1 prebid-secure-signals.sample-dev.com |
| 71 | + ``` |
| 72 | + |
| 73 | +4. Save the file |
| 74 | + |
| 75 | +5. Flush DNS cache (run in PowerShell as Administrator): |
| 76 | + ```powershell |
| 77 | + ipconfig /flushdns |
| 78 | + ``` |
| 79 | + |
| 80 | +### macOS / Linux |
| 81 | + |
| 82 | +1. Open the hosts file with sudo: |
| 83 | + ```bash |
| 84 | + sudo nano /etc/hosts |
| 85 | + ``` |
| 86 | + (or use `vim`, `vi`, or your preferred editor) |
| 87 | + |
| 88 | +2. Add the following entries: |
| 89 | + ``` |
| 90 | + 127.0.0.1 js-client-side.sample-dev.com |
| 91 | + 127.0.0.1 js-client-server.sample-dev.com |
| 92 | + 127.0.0.1 js-react.sample-dev.com |
| 93 | + 127.0.0.1 server-side.sample-dev.com |
| 94 | + 127.0.0.1 secure-signals-client-server.sample-dev.com |
| 95 | + 127.0.0.1 secure-signals-client-side.sample-dev.com |
| 96 | + 127.0.0.1 secure-signals-server-side.sample-dev.com |
| 97 | + 127.0.0.1 secure-signals-react.sample-dev.com |
| 98 | + 127.0.0.1 prebid-client.sample-dev.com |
| 99 | + 127.0.0.1 prebid-client-server.sample-dev.com |
| 100 | + 127.0.0.1 prebid-secure-signals.sample-dev.com |
| 101 | + ``` |
| 102 | + |
| 103 | +3. Save and exit |
| 104 | + |
| 105 | +4. Flush DNS cache (if needed): |
| 106 | + ```bash |
| 107 | + # macOS |
| 108 | + sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder |
| 109 | + |
| 110 | + # Linux (systemd-resolved) |
| 111 | + sudo systemd-resolve --flush-caches |
| 112 | + ``` |
4 | 113 |
|
5 | 114 | ## Usage |
6 | 115 |
|
7 | | -### Build the image |
| 116 | +### Using Docker Compose (Recommended) |
| 117 | + |
| 118 | +When using `docker-compose.yml` from the project root, the reverse proxy will automatically connect to other services on the same Docker network: |
| 119 | + |
| 120 | +**Default domain (sample-dev.com):** |
| 121 | +```bash |
| 122 | +docker-compose up reverse-proxy |
| 123 | +``` |
| 124 | + |
| 125 | +**Custom domain:** |
| 126 | +```bash |
| 127 | +DOMAIN=sample-test.com docker-compose up reverse-proxy |
| 128 | +``` |
| 129 | + |
| 130 | +**Or set in your `.env` file:** |
| 131 | +```bash |
| 132 | +DOMAIN=sample-prod.com |
| 133 | +``` |
| 134 | + |
| 135 | +Then run: |
| 136 | +```bash |
| 137 | +docker-compose up reverse-proxy |
| 138 | +``` |
| 139 | + |
| 140 | +### Standalone Build and Run |
| 141 | + |
| 142 | +#### Build the image |
8 | 143 | ```bash |
9 | 144 | docker build -t nginx-reverse-proxy . |
10 | 145 | ``` |
11 | 146 |
|
12 | | -### Run the container |
| 147 | +#### Run the container |
| 148 | + |
| 149 | +**Default domain:** |
13 | 150 | ```bash |
14 | 151 | docker run -d -p 80:80 --name nginx-proxy nginx-reverse-proxy |
15 | 152 | ``` |
16 | 153 |
|
17 | | -### Run with custom static files |
| 154 | +**Custom domain:** |
18 | 155 | ```bash |
19 | | -docker run -d -p 80:80 -v /path/to/your/html:/usr/share/nginx/html --name nginx-proxy nginx-reverse-proxy |
| 156 | +docker run -d -p 80:80 -e DOMAIN=sample-test.com --name nginx-proxy nginx-reverse-proxy |
20 | 157 | ``` |
21 | 158 |
|
22 | | -## Configuration |
| 159 | +**Note:** When running standalone, you'll need to ensure the backend services are accessible. You may need to modify the `proxy_pass` directives in `default.conf.template` to use `host.docker.internal` or the appropriate Docker network hostname. |
| 160 | + |
| 161 | +## Customization |
23 | 162 |
|
24 | | -Edit `default.conf` to customize the nginx configuration. The default configuration serves static files from `/usr/share/nginx/html`. |
| 163 | +Edit `default.conf.template` to customize the nginx configuration: |
| 164 | +- Add or remove server blocks for different subdomains |
| 165 | +- Modify subdomain names in the `server_name` directives (use `${DOMAIN}` for the domain variable) |
| 166 | +- Adjust proxy headers as needed |
| 167 | +- Add additional location blocks for specific routes |
25 | 168 |
|
26 | | -To use as a reverse proxy, uncomment and modify the proxy_pass section in `default.conf`. |
| 169 | +**Important:** After modifying `default.conf.template`, rebuild the Docker image: |
| 170 | +```bash |
| 171 | +docker-compose build reverse-proxy |
| 172 | +docker-compose up -d reverse-proxy |
| 173 | +``` |
27 | 174 |
|
0 commit comments