1、配置文件的语法格式

先来看一个简单的 nginx 配置

  1. # worker 进程数量
  2. worker_processes 1;
  3. events {
  4. # 每个 worker 进行可以处理的连接数量
  5. worker_connections 1024;
  6. }
  7. # http 服务
  8. http {
  9. # 可以处理的 mime 类型
  10. include mime.types;
  11. default_type application/octet-stream;
  12. # 是否可以发送文件
  13. sendfile on;
  14. # 连接超时时间
  15. keepalive_timeout 65;
  16. # server 服务模块(可以有多个)
  17. server {
  18. listen 80; # 监听的端口号
  19. server_name localhost; # 主机名
  20. # 访问 URI 路径(可以有多个)
  21. location / {
  22. root html; # 当前站点的根目录,是一个相对路径,相对于 /usr/local/nginx 根目录而言
  23. index index.html index.htm;
  24. }
  25. location /static {
  26. root html;
  27. index index.html index.htm;
  28. }
  29. location /nginx_status {
  30. stub_status on;
  31. access_log off;
  32. }
  33. }
  34. server {
  35. listen 80; # 监听的端口号
  36. # 支持通配符匹配,default 代表默认项,如果没有 default 则以最后一个 server 作为默认项
  37. server_name www.wesoft.org *.wesoft.org wesoft.* default;
  38. location / {
  39. root html;
  40. index index.html index.htm;
  41. }
  42. location /nginx_status {
  43. stub_status on;
  44. access_log off;
  45. }
  46. }
  47. }

上述配置中的 events、http、server、location、upstream 等属于配置项块。而 worker_processes 、worker_connections、include、listen 属于配置项块中的属性。 /nginx_status 属于配置块的特定参数参数。其中 server 块嵌套于 http 块,其可以直接继承访问 http 块当中的参数。

配置块 名称开头用大口号包裹其对应属性
属性 基于空格切分属性名与属性值,属性值可能有多个项

都以空格进行切分
如:access_log logs/host.access.log main | | 参数 | 其配置在 块名称与大括号间,其值如果有多个也是通过空格进行拆 |

注意:如果配置项值中包括语法符号,比如空格符,那么需要使用单引号或双引号括住配置项值,否则Nginx会报语法错误。例如:

  1. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  2. '$status $body_bytes_sent "$http_referer" '
  3. '"$http_user_agent" "$http_x_forwarded_for"';

location

语法:location [ = | ~ | ~* | ^~ | @ ] /uri/

=:表示把URI作为字符串,以便与参数中的uri做完全匹配。
/: 基于uri目录匹配
~:表示正则匹配URI时是字母大小写敏感的。
~*:表示正则匹配URI时忽略字母大小写问题。
^~:表示正则匹配URI时只需要其前半部分与uri参数匹配即可。

root

可配置在 server 与 location 中,基于 root 路径 + URL 中路径去寻找指定文件

alias

只能配置 location 中。基于 alias 路径 + URL,移除 location 前缀后的路径来寻找指定文件

2、配置第一个静态WEB服务

  1. server {
  2. listen 80;
  3. server_name www.wesoft.org;
  4. root /usr/www/html/;
  5. location / {
  6. index index.html;
  7. }
  8. location /test {
  9. # root /usr/www/html/test/; # root 属性会将 /test 加入到路径的后面
  10. alias /usr/www/html/test/; # alias 属性不会将 /test 加入到路径的后面
  11. index index.html;
  12. }
  13. }

3、动静分离

基于目录动静分离

  1. location /static {
  2. alias /usr/www/html/static/
  3. index index.html;
  4. }

基于正则动静分离

当使用正则匹配的时候,访问的时候不用带 static 目录

  1. location ~* \.(gif|jpg|jpeg|png|js|css)$ {
  2. root /usr/www/html/static/
  3. }

4、防盗链配置

  1. # 加入至指定 location 即可实现
  2. valid_referers none blocked *.wesoft.org;
  3. if ($invalid_referer) {
  4. return 403;
  5. }

如:配置静态资源防盗链

  1. location ~* \.(jpg|jpeg|gif|png|css|js)$ {
  2. root /usr/www/html/static/;
  3. valid_referers none blocked *.wesoft.org;
  4. if ($invalid_referer) {
  5. return 403;
  6. }
  7. }

5、下载限速

  1. location /download {
  2. limit_rate 1m; # 限制每秒下载速度,这个属性可以写在 server 中,全局生效
  3. limit_rate_after 30m; # 大小超过 30m 之后才启用限速,这个属性可以写在 server 中,全局生效
  4. }

6、创建 IP 黑名单

  1. #封禁指定IP
  2. deny 192.168.0.1;
  3. allow 192.168.0.1;
  4. #开放指定IP 段
  5. allow 192.168.0.0/24;
  6. #封禁所有
  7. deny all;
  8. #开放所有
  9. allow all;
  10. # 创建黑名单文件
  11. echo 'deny 192.168.0.132;' >> balck.ip
  12. #http 配置块中引入 黑名单文件
  13. include black.ip;