1.Nginx常用功能

  1. HTTP反向代理;
  2. 负载均衡;(内置策略为轮询,加权,Ip hash。扩展策略,就天马行空)
  3. web缓存;
  1. ## Nginx配置文件结构
  2. ########### 每个指令必须有分号结束。#################
  3. #user administrator administrators; #配置用户或者组,默认为nobody nobody。
  4. #worker_processes 2; #允许生成的进程数,默认为1
  5. #pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
  6. error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
  7. events {
  8. accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
  9. multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
  10. #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
  11. worker_connections 1024; #最大连接数,默认为512
  12. }
  13. http {
  14. include mime.types; #文件扩展名与文件类型映射表
  15. default_type application/octet-stream; #默认文件类型,默认为text/plain
  16. #access_log off; #取消服务日志
  17. log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
  18. access_log log/access.log myFormat; #combined为日志格式的默认值
  19. sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
  20. sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
  21. keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
  22. upstream mysvr {
  23. server 127.0.0.1:7878;
  24. server 192.168.10.121:3333 backup; #热备
  25. }
  26. error_page 404 https://www.baidu.com; #错误页
  27. server {
  28. keepalive_requests 120; #单连接请求上限次数。
  29. listen 4545; #监听端口
  30. server_name 127.0.0.1; #监听地址
  31. location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
  32. #root path; #根目录
  33. #index vv.txt; #设置默认页
  34. proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
  35. deny 127.0.0.1; #拒绝的ip
  36. allow 172.18.5.54; #允许的ip
  37. }
  38. }
  39. }

2. Nginx的location优先级

正则规则:

  • =开头表示精确匹配
    如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。
  • ^~ 开头表示uri以某个常规字符串开头,不是正则匹配
  • ~ 开头表示区分大小写的正则匹配;
  • ~* 开头表示不区分大小写的正则匹配
  • / 通用匹配, 如果没有其它匹配,任何请求都会匹配到

(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

Nginx的location是匹配的顺序:(从高到低)

  1. 等号类型(=)的优先级最高(完全一致才算匹配)。一旦匹配成功,则不再查找其他匹配项
  2. ^~类型表达式(前缀匹配)。一旦匹配成功,则不再查找其他匹配项
  3. 正则表达式类型(~ ~其中~区分大小写 ~不区分)
  4. 常规匹配
  1. # location优先级
  2. ## 1. 一般前端的location匹配: location ~ .*.
  3. ## 2. 提高文件匹配的优先级: location ^~ /upload
  4. server {
  5. listen 8080;
  6. #优先级第4
  7. location / {
  8. content_by_lua 'ngx.say("success4") ngx.exit(ngx.OK)';
  9. }
  10. #优先级第2
  11. location ^~ /hls
  12. {
  13. content_by_lua 'ngx.say("success2") ngx.exit(ngx.OK)';
  14. }
  15. #优先级第3
  16. location ~* index\.m3u8
  17. {
  18. content_by_lua 'ngx.say("success3") ngx.exit(ngx.OK)';
  19. }
  20. #优先级第1
  21. location = /hls/demo/index.m3u8
  22. {
  23. content_by_lua 'ngx.say("success1") ngx.exit(ngx.OK)';
  24. }
  25. }
  1. ## 记一次配置的案例:
  2. user nginx;
  3. worker_processes auto;
  4. #error_log /var/log/nginx/error.log warn;
  5. pid /var/run/nginx.pid;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. include /etc/nginx/mime.types;
  11. default_type application/octet-stream;
  12. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  13. '$status $body_bytes_sent "$http_referer" '
  14. '"$http_user_agent" "$http_x_forwarded_for"';
  15. access_log /var/log/nginx/access.log main;
  16. sendfile on;
  17. keepalive_timeout 65;
  18. server {
  19. listen 80;
  20. listen [::]:80;
  21. server_name localhost;
  22. charset utf-8;
  23. location ~ .*.(htm|html|GIF|JPG|JPEG|PNG|BMP|SWF|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|xlsx|mp3|wma|js|css|woff|otf|svg|json|ttf|TTF|woff2|unity3d|unityweb|assetbundle|mp4)$ {
  24. expires 7d;
  25. log_not_found off;
  26. access_log off;
  27. root /usr/share/nginx/html/web;
  28. index index.html;
  29. }
  30. location /redisDemo {
  31. proxy_pass http://redis-demo:8090/;
  32. }
  33. location /admin {
  34. proxy_pass http://tomcat10010:8080;
  35. }
  36. location /platform {
  37. proxy_pass http://tomcat10010:8080; # 者利用的都是容器名和容器内的PORT;因为容器都已经挂到同一个网桥下了
  38. }
  39. location ^~ /upload { # 优先级高于location ~.*.的前端内容;
  40. # alias /usr/local/upload; # 配置的时候,可以采用alias 或者root;这里配置的两种亲测都有效。
  41. root /usr/local;
  42. autoindex on;
  43. autoindex_exact_size off;
  44. autoindex_localtime on;
  45. charset utf-8,gbk;
  46. }
  47. }
  48. }

3.location和proxy_pass 路径上,最后的一个”/“

  1. # Nginx的配置: location 和 proxy_pass 两个参数的配置
  2. ## 希望请求访问 192.168.10.10:80/static/a.html
  3. server{
  4. port 80,
  5. server name 192.168.10.10
  6. location /static{
  7. proxy_pass 192.168.2.321:81
  8. }
  9. location /static{
  10. proxy_pass 192.168.2.321:81/
  11. }
  12. location /static/{
  13. proxy_pass 192.168.2.321:81
  14. }
  15. location /static/{
  16. proxy_pass 192.168.2.321:81/
  17. }
  18. }

第一种: location后面没有 / 、转发后面也没有/

192.168.10.10->server name

:80 ————-> port

/statc ———->location

/a.html ———>proxy_pass

location /static { proxy_pass 192.168.2.321:81 } 最后的访问: 网站经过nginx转向: 192.168.2.321:81/static/a.html


第二种: location后没有/ 、转发后面有/ location /static { proxy_pass 192.168.2.321:81/ } 最后的访问: 网站经过nginx转向:192.168.2.321:81/a.html

第三种: location后有/ 、转发后没有/ location /static/ { proxy_pass 192.168.2.321:81 } 最后的访问: 网站经过nginx转向:192.168.2.321:81/static/a.html

第四种: location后有/ 、转发后面有/

192.168.10.10—>server name

:80 ——————> port

/statc/ —————>location(path1)

a.html ————->proxy_pass (path2)

location /static/ { proxy_pass 192.168.2.321:81/ } 最后的访问: 网站经过nginx转向:192.168.2.321:81/a.html

总结:

  1. Nginx里面配置,端口后面的参数分为 path1 + path2 ; location配置的为path1, proxy_pass的配置为path2
  2. proxy_pass:
    1. # proxy_pass 上面配置的路径带上`/` ,那么这个网址最后仅需要跟上path2;
    2. # proxy_pass 上配置的路径没有带上`/`,那么这个网址最后需要跟上 path1+path2