实用的前端nginx配置

230 阅读3分钟

运行nginx所需的最低配置

server {
    listen 80;
    server_name test.netease.com;

    return 200 "Hello";
  }

Root 指令

root指令用于设置请求的根目录,从而允许nginx将传入的请求映射到文件系统上。

server {
  listen 80;
  server_name test.netease.com;
  root /home/appops/;
}

它允许nginx根据请求返回服务器内容:

test.netease.com/index.html     
# return /home/appops/index.html
test.netease.com/css/style.css
# return /home/appops/css.style.css

Location指令

location指令用于根据请求的URI(统一资源标识符)来设置配置。

location /hello {
   return 200 "hello";
}

我们可以在给定的上下文中使用多个location指令:

server {
  listen 80;
  server_name test.netease.com;
  root /home/appops/;

  location /hello {
    add_header Content-Type text/plain;
    return 200 "hello";
  }

  location /bye {
    add_header Content-Type text/plain;
    return 200 "bye";
  }
}
test.netease.com   /hello    # => "hello"
test.netease.com   /hello123 # => "hello"
test.netease.com   /bye    # => "bye"

Nginx还提供了一些可以与 location 指令结合使用的修饰符。修饰符已分配优先级:

=           - Exact match 精确匹配
^~          - Preferential match 优先匹配
~ && ~*     - Regex match 正则表达式匹配
no modifier - Prefix match 前缀匹配
  • 首先nginx将检查所有精确匹配项。
  • 如果上一项不存在,它将寻找优先选项。
  • 如果上一项匹配也失败,则将按其出现顺序测试正则表达式匹配。
  • 如果其他所有操作均失败,则将使用最后一个前缀匹配。
location /hello {
  add_header Content-Type text/plain;
  return 200 'Prefix match: 匹配所有以 /match 开头的';
}

location ~* /hello[0-9] {
  add_header Content-Type text/plain;
  return 200 '不区分大小写的正则表达式匹配~*';
}

location ~ /hello[0-9] {
  return 200 '区分大小写的正则表达式匹配~';
}

location ^~ /hello0 {
  return 200 'Preferential match: 优先匹配';
}

location = /hello {
  return 200 'Exact match: 精确匹配';
}
/hello     # => 'Exact match: 精确匹配'
/hello0    # => 'Preferential match: 优先匹配'
/hello1    # => '不区分大小写的正则表达式匹配~*'
/HELLO1    # => '区分大小写的正则表达式匹配~'
/hello-123   # => ''Prefix match: 匹配所有以 /hello 开头的'

try_files指令

尝试不同的路径,并返回找到的任何路径。

try_files $uri index.html =404;

/hello.html将尝试按以下顺序返回文件:

$uri(/hello.html);

index.html

如果未找到:404

location目录匹配详解

平时我们在配置location时常因为一个 “/" 导致访问不通报错,下面我们已一个例子详细介绍一下。

访问test.netease.com/hello/index.html

location /hello/ {
    proxy_connect_timeout 18000; 
    proxy_send_timeout 18000;
    proxy_read_timeout 18000;
    proxy_pass http://127.0.0.1:8080;
}

此时会匹配到/hello/,从而代理到127.0.0.1:8080上。location如果没有“/”时,请求就可以模糊匹配以字符串开头的所有字符串,而有“/”时,只能精确匹配字符本身。

详细说明

配置location /hello 可以匹配 /hellonetease请求,也可以匹配/hello*/netease等等,只要以hello开头的目录都可以匹配到。而location /hello/必须精确匹配/hello/这个目录的请求,不能匹配/hellonetease/或/hello*/netease等请求。

proxy_pass有无“/”的区别

加 ”/“

location  /hello/ {
    proxy_pass  http://127.0.0.1:8080/;
   }

请求代理到 http://127.0.0.1:8080/index.html

不加”/“

location  /hello/ {
    proxy_pass  http://127.0.0.1:8080;
   }

http://127.0.0.1:8080/hello/index.html

目录后面加”/“

location  /hello/ {
    proxy_pass  http://127.0.0.1:8080/netease/;
   }

http://127.0.0.1:8080/netease/index.html

目录后面不加”/“

location  /hello/ {
    proxy_pass  http://127.0.0.1:8080/netease;
   }

http://127.0.0.1:8080/neteaseindex.html

实战测试

server {
  listen 80;
  server_name test.netease.com;
  
  # http://test.netease.com/hello01/xxx -> http://localhost:3000/hello01/xxx
  location /hello01/ {
    proxy_pass http://localhost:3000;
  }

image.png image.png

  # http://test.netease.com/hello02/xxx -> http://localhost:3000/xxx
  location /hello02/ {
    proxy_pass http://localhost:3000/;
  }
  # http://test.netease.com/hello03/xxx -> http://localhost:3000/hello03*/xxx
  location /hello03 {
    proxy_pass http://localhost:3000;
  }
  # http://test.netease.com/hello04/xxx -> http://localhost:3000//xxx,请注意这里的双斜线。
  location /hello04 {
    proxy_pass http://localhost:3000/;
  }
  # http://test.netease.com/hello05/xxx -> http://localhost:3000/neteasexxx,请注意这里的netease和xxx之间没有斜杠。
  location /hello05/ {
    proxy_pass http://localhost:3000/netease;
  }

image.png image.png

  # http://test.netease.com/hello06/xxx -> http://localhost:3000/netease/xxx
  location /hello06/ {
    proxy_pass http://localhost:3000/netease/;
  }
  # http://test.netease.com/hello07/xxx -> http://localhost:3000/netease/xxx
  location /hello07 {
    proxy_pass http://localhost:3000/netease;
  }
  # http://test.netease.com/hello08/xxx -> http://localhost:3000/netease//xxx,请注意这里的双斜杠。
  location /hello08 {
    proxy_pass http://localhost:3000/netease/;
  }
}

参考链接