1.Nginx常用功能
- HTTP反向代理;
- 负载均衡;(内置策略为轮询,加权,Ip hash。扩展策略,就天马行空)
- web缓存;
## Nginx配置文件结构
########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
}
}
2. Nginx的location优先级
正则规则:
- 已
=
开头表示精确匹配
如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。^~
开头表示uri以某个常规字符串开头,不是正则匹配- ~ 开头表示区分大小写的正则匹配;
- ~* 开头表示不区分大小写的正则匹配
- / 通用匹配, 如果没有其它匹配,任何请求都会匹配到
(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)
Nginx的location是匹配的顺序:(从高到低)
- 等号类型(=)的优先级最高(完全一致才算匹配)。一旦匹配成功,则不再查找其他匹配项
- ^~类型表达式(前缀匹配)。一旦匹配成功,则不再查找其他匹配项
- 正则表达式类型(~ ~其中~区分大小写 ~不区分)
- 常规匹配
# location优先级
## 1. 一般前端的location匹配: location ~ .*.
## 2. 提高文件匹配的优先级: location ^~ /upload
server {
listen 8080;
#优先级第4
location / {
content_by_lua 'ngx.say("success4") ngx.exit(ngx.OK)';
}
#优先级第2
location ^~ /hls
{
content_by_lua 'ngx.say("success2") ngx.exit(ngx.OK)';
}
#优先级第3
location ~* index\.m3u8
{
content_by_lua 'ngx.say("success3") ngx.exit(ngx.OK)';
}
#优先级第1
location = /hls/demo/index.m3u8
{
content_by_lua 'ngx.say("success1") ngx.exit(ngx.OK)';
}
}
## 记一次配置的案例:
user nginx;
worker_processes auto;
#error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
listen [::]:80;
server_name localhost;
charset utf-8;
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)$ {
expires 7d;
log_not_found off;
access_log off;
root /usr/share/nginx/html/web;
index index.html;
}
location /redisDemo {
proxy_pass http://redis-demo:8090/;
}
location /admin {
proxy_pass http://tomcat10010:8080;
}
location /platform {
proxy_pass http://tomcat10010:8080; # 者利用的都是容器名和容器内的PORT;因为容器都已经挂到同一个网桥下了
}
location ^~ /upload { # 优先级高于location ~.*.的前端内容;
# alias /usr/local/upload; # 配置的时候,可以采用alias 或者root;这里配置的两种亲测都有效。
root /usr/local;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8,gbk;
}
}
}
3.location和proxy_pass 路径上,最后的一个”/“
# Nginx的配置: location 和 proxy_pass 两个参数的配置
## 希望请求访问 192.168.10.10:80/static/a.html
server{
port 80,
server name 192.168.10.10
location /static{
proxy_pass 192.168.2.321:81
}
location /static{
proxy_pass 192.168.2.321:81/
}
location /static/{
proxy_pass 192.168.2.321:81
}
location /static/{
proxy_pass 192.168.2.321:81/
}
}
第一种: 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
总结:
- Nginx里面配置,端口后面的参数分为
path1 + path2
; location配置的为path1, proxy_pass的配置为path2 - proxy_pass:
# proxy_pass 上面配置的路径带上`/` ,那么这个网址最后仅需要跟上path2;
# proxy_pass 上配置的路径没有带上`/`,那么这个网址最后需要跟上 path1+path2