nginx

Nginx

默认配置文件

1
/etc/nginx/nginx.conf

转发请求

1
2
3
4
5
6
7
8
9
10
http {
server {
listen 80;

# 访问 / 的时候内部转发请求到 9001 端口
location / {
proxy_pass http://localhost:9001/;
}
}
}

server block

A server block is a subset of Nginx’s configuration that defines a virtual server used to handle requests of a defined type. Administrators often configure 多个 server and decide which block should handle which connection based on the requested 域名, 端口, IP 地址.

server_name 的匹配:

  • Host
  • * 开头的字符匹配
  • * 结尾的字符匹配
  • ~ 开头的正则匹配
  • 选择默认的 server block
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 80;
server_name example.com;

. . .

}

server {
listen 80;
server_name ~^(www|host1).*\.example\.com$;

. . .

}

server {
listen 80;
server_name ~^(subdomain|set|www|host1).*\.example\.com$;

. . .

}

location block

A location block lives within a server block and is used to define how Nginx should handle requests for different resources and URIs for the parent server. The URI space can be subdivided in whatever way the administrator likes using these blocks. It is an extremely flexible model.

前缀匹配:

正则敏感匹配:

阻止正则匹配:

nginx 是否启动

1
sudo systemctl status nginx

或者

1
sudo service status nginx

启动 nginx

1
sudo service nginx start

查看 nginx 有哪些命令

1
2
zk@zk-Lenovo:~/Desktop$ sudo service nginx
Usage: nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}

nginx 实际上在使用哪个位置的配置文件

1
sudo nginx -t

或者

1
nginx -V

待读文章

https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms

推荐文章