Nginx基本配置http,server,location三层结构说明

文章目录

  • 文章配图
  • 配置案例
  • 结构说明
  • location优先级说明
    • 优先级规则
    • 说明
  • location配置代理注意事项
    • location是否以/结尾的区别
    • proxy_pass的值是否以/结尾
  • 总结
  • 参考

文章配图

配置案例

user       www www;  ## Default: nobody
worker_processes  5;  ## Default: 1
error_log  logs/error.log;
pid        logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
  worker_connections  4096;  ## Default: 1024
}

http {
  include    conf/mime.types;
  include    /etc/nginx/proxy.conf;
  include    /etc/nginx/fastcgi.conf;
  index    index.html index.htm index.php;

  default_type application/octet-stream;
  log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log   logs/access.log  main;
  sendfile     on;
  tcp_nopush   on;
  server_names_hash_bucket_size 128; # this seems to be required for some vhosts

  server { # php/fastcgi
    listen       80;
    server_name  domain1.com www.domain1.com;
    access_log   logs/domain1.access.log  main;
    root         html;

    location ~ \.php$ {
      fastcgi_pass   127.0.0.1:1025;
    }
  }

  server { # simple reverse-proxy
    listen       80;
    server_name  domain2.com www.domain2.com;
    access_log   logs/domain2.access.log  main;

    # serve static files
    location ~ ^/(images|javascript|js|css|flash|media|static)/  {
      root    /var/www/virtual/big.server.com/htdocs;
      expires 30d;
    }

    # pass requests for dynamic content to rails/turbogears/zope, et al
    location / {
      proxy_pass      http://127.0.0.1:8080;
    }
  }

  upstream big_server_com {
    server 127.0.0.3:8000 weight=5;
    server 127.0.0.3:8001 weight=5;
    server 192.168.0.1:8000;
    server 192.168.0.1:8001;
  }

  server { # simple load balancing
    listen          80;
    server_name     big.server.com;
    access_log      logs/big.server.access.log main;

    location / {
      proxy_pass      http://big_server_com;
    }
  }
}

结构说明

nginx配置结构分为三层 http > server > location
http 包含一到多个server, server包含一到多个location
配置项的优先级分别是location, server, http

假如:

http {
	...
	access_log /var/logs/nginx/nginx.log;
	
	server {
		server_name A;
		...
		access_log /var/logs/nginx/serverA/nginx.log;
		
		location / {
				...
				access_log /var/logs/nginx/serverA/localtion/nginx.log;
		}
	}
}
  • 匹配到server A,localtion /时日志会记录到 /var/logs/nginx/serverA/localtion/nginx.log;
  • 匹配到server A 其他location时日志会记录到/var/logs/nginx/serverA/nginx.log
  • 默认请求日志记录到 /var/logs/nginx/nginx.log;

location优先级说明

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

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

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}
  • The “/” request will match configuration A,
  • the “/index.html” request will match configuration B,
  • the “/documents/document.html” request will match configuration C,
  • the “/images/1.gif” request will match configuration D,
  • the “/documents/1.jpg” request will match configuration E.

The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection. They cannot be nested, and cannot contain nested locations.

优先级规则

  • (=)的优先级最高
  • 匹配(^~)优先级次之。不支持正则表达式。如果有多个location匹配的话,则使用表达式最长的那个
  • 正则表达式类型(~ ~*)的优先级次之
  • 常规字符串匹配,如果有多个location匹配的话,则使用表达式最长的那个

说明

  • 精准匹配,有匹配项,返回结果,结束解析。
  • 前缀匹配,有匹配项,选择最长,返回结果,结束解析。
  • 正则匹配,按正则表达式顺序为准,由上至下一旦匹配成功,返回结果,结束解析
  • 若未结束,继续普通命中,普通命中和前缀普通命中相似,顺序无所谓,按照location表达式的长短来确定命中结果

location配置代理注意事项

location是否以/结尾的区别

If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, or grpc_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:

location /user/ {
    proxy_pass http://user.example.com;
}

location = /user {
    proxy_pass http://login.example.com;
}
  • localtion结尾处有/时,访问/user会301重定向到/user/
  • localtion结尾处无/时,访问/user不重定向

proxy_pass的值是否以/结尾

如:

    location /ttt {
        proxy_pass  http://codeper.exwechat.com/;
    }   

    location /ttt2/{
        proxy_pass  http://codeper.exwechat.com/;
    } 
  • proxy_pass尾处有/时, 实际请求的url过滤location
  • proxy_pass尾处无/时, 实际请求的url包含location

总结

  • nginx配置分三层结构,层级越深配置优先级越高
  • location 是否以/结尾和当前访问的url影响着是否301跳转
  • proxy_pass是否以/结尾影响实际访问的url是否包含location配置的前缀

参考

Full Example Configuration
https://www.nginx.com/resources/wiki/start/topics/examples/full/
Nginx localtion配置规则及优先级
https://blog.csdn.net/duyusean/article/details/85396238

你可能感兴趣的:(nginx,nginx,conf)