三层架构
三层结构:(具体以下图为例)
基本配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| worker_processes 1;
events { worker_connections 1024; }
http { server { listen 80; server_name localhost; localtion / { root html; index index.html index.htm; } } }
|
Listen
server块中最重要的指令就是listen指令,这个指令有三种配置语法。这个指令默认的配置值是:listen *:80 | *:8000;
只能在server块种配置这个指令。
- 监听某个ip地址
- 监听某个端口(建议 )
- 其他(无需多加关注)
1 2 3 4 5 6 7 8
| //第一种 listen address[:port] [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
//第二种 listen port [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
//第三种(可以不用重点关注) listen unix:path [default_server] [ssl] [http2 | spdy] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
|
拓展:可以简单了解server_name,它可以准确匹配、通配符(前后)以及正则匹配。
location
在 nginx 官方定义:
1
| location [ = | ~ | ~* | ^~ ] uri { ... }
|
小技巧:可以将 nginx 的 location 想象成 window.location
去除 host
的部分。