Nginx配置静态资源

  • 打开 /etc/nginx/sites-available 的 default文件
    sudo cd /etc/nginx/sites-available sudo vim default
  • 修改default文件添加要匹配的url路径
    • 格式:

      location 要匹配的路径{
      root 映射到服务器文件的父路径
      }
    • laction

      Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
      location @name { ... }
      Default: —
      Context: server, location

      location 有两种匹配路径的方式:前缀字符串匹配(prefix string)和正则表达式匹配(regular expression)

      默认会优先前缀字符串匹配,再进行正则表达式匹配
      若 正则表达式匹配成功 则使用其匹配结果
      否则 使用前缀字符串匹配的结果
      此时 前缀字符串匹配也失败的话 就会报错

      符号的使用:
      • ~* 对大小写不敏感
      • ~ 对大小写敏感
      • = / 精确匹配 (用于常用的路径匹配,能增加匹配速度)
      • ^~ 不匹配正则表达式

      ex:

      ```

      location = / {
      [ configuration A ]
      }

      location / {
      [ configuration B ]
      }

      location /documents/ {
      [ configuration C ]
      }

      location ^~ /images/ {
      [ configuration D ]
      }

      location ~* .(gif|jpg|jpeg)$ {
      [ configuration E ]
      }

      ```

      • “/” match configuration A
      • “/index.html” match configuration B
      • “/documents/document.html” match configuration C
      • “/images/1.gif” match configuration D
      • /documents/1.jpg match configuration E
    • root

      Syntax: root path;
      Default:
      root html;
      Context: http, server, location, if in location
      Sets the root directory for requests.

      ex:

      location /i/ { root /data/w3; }
      /data/w3/i/top.gif文件将被发送以响应“/i/top.gif”请求。


参考:nginx wiki

猜你喜欢

转载自www.cnblogs.com/freeyun/p/9168866.html