nginx.conf69 lines, 2.5 KB
| 1 | # The load balancer. Clients talk to port 8080, and nginx hands every request |
| 2 | # to one of the API servers. |
| 3 | events { |
| 4 | # a stream holds two connections |
| 5 | worker_connections 8192; |
| 6 | } |
| 7 | |
| 8 | http { |
| 9 | # no line per request: one load test would write a hundred thousand of them |
| 10 | access_log off; |
| 11 | |
| 12 | # The API servers. Docker's DNS answers the name api with the address of every |
| 13 | # container of that service, and nginx asks it again every five seconds. |
| 14 | upstream api { |
| 15 | # the list of servers lives in shared memory, so it can change while nginx runs |
| 16 | zone api 64k; |
| 17 | # Docker's DNS, asked again every five seconds |
| 18 | resolver 127.0.0.11 valid=5s; |
| 19 | # max_fails=0: a server that refused a connection is tried again on the next request |
| 20 | server api:8081 resolve max_fails=0; |
| 21 | keepalive 256; |
| 22 | } |
| 23 | |
| 24 | server { |
| 25 | listen 8080; |
| 26 | |
| 27 | location / { |
| 28 | proxy_pass http://api; |
| 29 | # a server that is gone doesn't refuse: nobody answers. Wait a second, not a minute |
| 30 | proxy_connect_timeout 1s; |
| 31 | proxy_set_header Host $host; |
| 32 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 33 | add_header X-Served-By $upstream_addr always; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | # The web app: what make web built, and the API under /api/, on one |
| 38 | # address. So the browser never calls another origin, and needs no CORS. |
| 39 | server { |
| 40 | listen 8088; |
| 41 | root /srv/web; |
| 42 | # the types of files by their names: a browser runs no .js sent as text |
| 43 | include /etc/nginx/mime.types; |
| 44 | # the build's JavaScript and CSS go out compressed |
| 45 | gzip on; |
| 46 | gzip_types text/css application/javascript; |
| 47 | |
| 48 | # an address the app routes itself, like /u/user1, gets the app |
| 49 | location / { |
| 50 | try_files $uri /index.html =404; |
| 51 | # its name never changes, so the browser asks again every time |
| 52 | add_header Cache-Control "no-cache"; |
| 53 | } |
| 54 | |
| 55 | # their names change with their content, so they can be kept for good |
| 56 | location /assets/ { |
| 57 | add_header Cache-Control "public, max-age=31536000, immutable"; |
| 58 | } |
| 59 | |
| 60 | # /api/feed is /feed to the API servers |
| 61 | location /api/ { |
| 62 | proxy_pass http://api/; |
| 63 | proxy_connect_timeout 1s; |
| 64 | proxy_set_header Host $host; |
| 65 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 66 | add_header X-Served-By $upstream_addr always; |
| 67 | } |
| 68 | } |
| 69 | } |