Nginx
nginx的作用
术语
- directives(指令?): key-value
- context:{…}
serve static files
1 2 3 4 5 6 7 8
| http { server { listen 8080; root /path/to/static/file/folder; } }
events {}
|
然后reload
MIME Types
在不配置的情况下,css不能成功应用到html文件中
- 检查Response headers - Content-Type:
text/plain
1 2 3 4 5 6 7
| http { types { text/css css; text/html html; } server {...} }
|
但是MIME Type太多了!mime.types
有对应的映射表!
1 2 3 4
| http { include mime.types; server {...} }
|
Location block / context
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| http { include mime.types; server { listen 8080; root /path/to/static/files/folder;
location /fruits { root /path/to/static/files/folder; }
location /carbs { alias /path/to/static/files/fruits; } } }
events {...}
|
nginx默认只提供index.html,但可以通过try_files指定
1 2 3 4 5 6 7 8 9 10 11 12
| http { include mime.types; server { ...
location /vegetables { root /path/to/static/files/folder; try_files /vegetables/veggies.html /index.html =404 } } } ...
|
location也可以使用regex
1 2 3 4 5 6 7 8 9 10 11
| http { ... server { ... location ~* /count/[0-9] { root /path/to/static/files/folder; try_files /index.html =404; } } } ...
|
Redirect与Rewrite
重定向redirect
1 2 3
| location /crops { return 307 /fruits # 重定向码,但浏览器会显示/fruits }
|
如果不希望浏览器url变化,应该使用rewrite
1 2 3 4 5 6 7 8 9 10 11 12
| http { ... server { ... rewrite ^/number/(\w+) /count/$1;
location ~* /count/[0-9] { root /path/to/static/files/folder; try_files /index.html =404; } } }
|
负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| http { ... upstream backendserver { server 127.0.0.1:1111; server 127.0.0.1:2222; server 127.0.0.1:3333; server 127.0.0.1:4444; }
server { listen 8080; root /path/to/static/files/folder; location / { proxy_pass http://backendserver/; } ... } } ...
|
docker
遇到的小问题:docker compose up拉起服务的时候,如果某个container挂掉了,想进去debug的话用这个指令
1
| docker compose run --entrypoint /bin/bash <service-name>
|
注意!这里的service name是docker-compose文件里的!例如,一个docker-compose文件长这样
1 2 3 4
| service: postgres: image: postgres:latest ...
|
如果postgres不明原因挂了而且起不来,就可以用上面的指令进去看什么情况。
每个service都对应一个container name,但是在使用docker compose命令的时候一般用service名而不是container名,尽管在后台docker还是拉起了container。