nginx配置使用

 

nginx 安装(阿里云服务器)

 
1,nginx 安装
yum -y install gcc automake autoconf libtool make
yum install gcc gcc-c++
yum install -y lrzsz
cd /usr/local/src
tar -zxvf pcre-8.42.tar.gz
cd pcre-8.42
./configure
make
make install
 
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
 
tar -zxvf openssl-1.0.1t.tar.gz
tar -zxvf nginx-1.14.1.tar.gz
 
cd nginx-1.14.1
./configure --sbin-path=/usr/local/nginx/  --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11 --with-openssl=/usr/local/src/openssl-1.0.1t
make
make install
 
2,simple nginx 安装 无ssl
 yum -y install gcc gcc-c++ automake autoconf libtool make
yum install -y lrzsz
cd /usr/local/src
tar -zxvf pcre-8.42.tar.gz
cd pcre-8.42
./configure
make
make install
cd /usr/local/src
tar -zxvf zlib-1.2.11.tar.gz
cd /usr/local/src/zlib-1.2.11
./configure
make
make install
cd /usr/local/src
tar -zxvf nginx-1.14.1.tar.gz
cd /usr/local/src/nginx-1.14.1
./configure --sbin-path=/usr/local/nginx/  --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11
make
make install

Nginx location匹配规则:

  1. "="前缀指令匹配,如果匹配成功,则停止其他匹配
  2. 普通字符串指令匹配,顺序是从长到短,匹配成功的location,如果使用^~,则停止其他匹配(正则匹配)
  3. 正则表达式指令匹配,按照配置文件里的顺序,成功就停止其他匹配
  4. 如果第三步中有匹配成功,则使用该结果,否则使用第二步结果
  5. (location =) > (location 完整路径) >(location ^~路径)>(location ~,~*正则顺序) > (location 部分起始路径) > (/)

匹配的顺序是先匹配普通字符串,然后再匹配正则表达式。另外普通字符串匹配顺序是根据配置中字符长度从长到短,也就是说使用普通字符串配置的location顺序是无关紧要的,反正最后nginx会根据配置的长短来进行匹配,但是需要注意的是正则表达式按照配置文件里的顺序测试。找到第一个比配的正则表达式将停止搜索。一般情况下,匹配成功了普通字符串location后还会进行正则表达式location匹配。有两种方法改变这种行为,其一就是使用“=”前缀,这时执行的是严格匹配,并且匹配成功后立即停止其他匹配,同时处理这个请求;另外一种就是使用“^~”前缀,如果把这个前缀用于一个常规字符串那么告诉nginx 如果路径匹配那么不测试正则表达式。

反向代理配置

  proxy_pass节点,用于代理请求,适用于前后端负载分离或多台机器、服务器负载分离的场景,在匹配到location配置的URL路径后,转发请求到【proxy_pass】配置的URL,

是否会附加location配置路径与【proxy_pass】配置的路径后是否有"/"有关,有"/"则不附加;location后的地址都会路由到proxy_pass后的地址

匹配模式及顺序示例:

  location = /uri    =开头表示精确匹配,只有完全匹配上才能生效。

  location ^~ /uri   ^~ 开头对URL路径进行前缀匹配,并且在正则之前。不能使用正则

  location ~ pattern  ~开头表示区分大小写的正则匹配。

  location ~* pattern  ~*开头表示不区分大小写的正则匹配。

  location /uri     不带任何修饰符,也表示前缀匹配,但是在正则匹配之后。

  location /      通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default。 

示例:

location  = / {

  # 精确匹配 / ,主机名后面不能带任何字符串

  [ configuration A ] 

}

location  / {

  # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求

  # 但是正则和最长字符串会优先匹配

  [ configuration B ] 

}

location /documents/ {

  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索

  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条

  [ configuration C ] 

}

location ~ /documents/Abc {

  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索

  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条

  [ configuration C ] 

}

location ^~ /images/ {

  # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。

  [ configuration D ] 

}

location ~* \.(gif|jpg|jpeg)$ {

  # 匹配所有以 gif,jpg或jpeg 结尾的请求

  # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则

  [ configuration E ] 

}

location /images/ {

  # 字符匹配到 /images/,继续往下,会发现 ^~ 存在

  [ configuration F ] 

}

location /images/abc {

  # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在

  # F与G的放置顺序是没有关系的

  [ configuration G ] 

}

location ~ /images/abc/ {

  # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用

    [ configuration H ] 

}

location ~* /js/.*/\.js

实验案例

  • 测试"^~"和"~",nginx配置如下。
    location ^~ /helloworld { #1 return 601;} #location /helloworld { #2# return 602;#}location ~ /helloworld { return 603;}
    浏览器输入http://localhost/helloworld/test,返回601。
    如将#1注释,#2打开,浏览器输入http://localhost/helloworld/test,返回603。
    注:#1和#2不能同时打开,如同时打开,启动nginx会报nginx: [emerg] duplicate location "/helloworld"...,因为这两个都是普通字符串。
  • 测试普通字符串的长短(普通字符串的匹配与顺序无关,与长短有关)。
    location /helloworld/test/ { #1 return 601;} location /helloworld/ { #2 return 602;}
    浏览器输入http://localhost/helloworld/test/a.html,返回601。浏览器输入http://localhost/helloworld/a.html,返回602。
  • 测试正则表达式的顺序(正则匹配与顺序相关)。
    location /helloworld/test/ { #1 return 601;}location ~ /helloworld { #2 return 602;} location ~ /helloworld/test { #3 return 603;}
    浏览器输入http://localhost/helloworld/test/a.html,返回602;
    将#2和#3调换顺序,浏览器输入http://localhost/helloworld/test/a.html,返回603
 
 
 
posted @ 2019-10-29 17:02  原子切割员  阅读(135)  评论(0编辑  收藏  举报