nginx解析

 nginx配置文档

 

 腾讯云nginx 教程

http://shouce.jb51.net/nginx/left.html

http://www.nginx.cn/nginx-how-to

Nginx文章

useradd -s /sbin/nologin nginx

yum install pcre-devel openssl-devel  gcc

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx  --with-http_ssl_module --with-stream --with-http_stub_status_module  .......

nginx -h 帮助
nginx -s stop
nginx -s reload
nginx -V

Linux 之centos7 制作服务自启动systemd





location 中根目录的alias和root的区别

location /abc/ {
    alias /home/html/abc/;
}

在这段配置下,http://test/abc/a.html就指定的是/home/html/abc/a.html。这段配置亦可改成

location /abc/ {
    root /home/html/;
}

这样,nginx就会去找/home/html/目录下的abc目录了,得到的结果是相同的。

解析:如果是alias的话,匹配的 /abc/ 就等于/home/html/abc/,  如果是root的话,匹配中的 /abc/ 中的 /  等于/home/html/  ,而 /abc/  等于/home/html/abc/

但是,如果我把alias的配置改成:

location /abc/ {
    alias /home/html/def/;
}

  那么nginx将会从/home/html/def/取数据,这段配置还不能直接使用root配置,如果非要配置,只有在/home/html/下建立一个 def->abc的软link(快捷方式)了。一般情况下,在location /中配置root,在location /other中配置alias是一个好习惯。至于alias和root的区别,我估计还没有说完全,如果在配置时发现奇异问题,不妨把这两者换换试试。

但nginx在处理php脚本时,需要传递给fastcgi才能处理,所以比apache的别名设置多一个,下面我们以phpmyadmin别名设置为例:

location ~ ^/phpmyadmin.+.php$ {
root /home/www/default;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /phpmyadmin {
root /home/www/default;
index index.php;
}

 proxy_pass 使用详解

永福的博客

 补充解释1:proxy_pass  后的url如果加了   /   就会改变 请求,减去location 后的匹配  url段,不加  /  就不会改变请求,前端的url是什么,代理后的请求就是什么,如:

补充2:当location使用  正则匹配时,proxy_pass  后的url是不能加  /  的("proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block ),

1:

location  / {
 proxy_pass http://192.168.0.132/aa/;
index index.html;
}

访问  http://test.com/aa,  后端的请求是/aa/aa/,回到根目录下找aa文佳夹下的aa下的index.html

2:

location  /aa {
 proxy_pass http://192.168.0.132;
index index.html;
}

访问192.168.0.139/aa/   就是访问  html/aa/index.html ,访问192.168.0.139/aaaa/    就是访问 html/aaaa/index.html

location  /aa {
 proxy_pass http://192.168.0.132/;
index index.html;
}
访问192.168.0.139/aa/   就是访问  html/index.html   访问192.168.0.139/aaaa/    就是访问html/aa/index.html

(curl 访问是 url/aa    和 url/aa/     是不一样的  文件和目录,当输入  url/aa  找不到时 ,输入url//aa/   可以正常返回页面。而使用浏览器访问时 ,浏览器会同时访问 两个, url/aa  找不到时,就自动请求url//aa/ ,地址栏也跟着加上  /  )

 location匹配规则

符号含义
= 开头表示精确匹配
^~ 开头表示 uri 以某个常规字符串开头,理解为匹配 url 路径即可。nginx 不对 url 做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
/ 通用匹配,任何请求都会匹配到
!~ 取反
!~* 取反

 Location配置总结

 nginx配置location总结(厉害)匹配顺序

location  / {
return 500;                           #A
}

location ~*  /Bb {
 proxy_pass http://www.baidu.com;    #B
}
location
~ /BB { proxy_pass http://192.168.0.132; #C } location ^~ /bb { proxy_pass http://192.168.0.132; #D } location = /bB/ { proxy_pass http://www.163.com; #E }


test.com/BB 进入A ,test.com/bb 进入D ,test.com/bB 进入E,test.com/bbbbb ,进入D ,test.com/bf 进入A
location   /zuul {
                proxy_pass http://192.168.0.147/;
        }

location  ^~  /zuul {
                proxy_pass http://192.168.0.147/;
        }
这两个是等效的,都是匹配 /zuul 开头, /zuul/aaa 和 /zuulaaa 都会匹配到,将aaa传给后端,
当location 匹配 = /zuul 则只能只 /zuul 才能匹配,/zuul/aaa 和 /zuulaaa 都不行
location  ~ 的正则匹配是不支持 proxy_pass 的
nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /usr/local/nginx/conf/nginx.conf:60
 

Nginx内置变量以及日志格式变量参数详解

nginx防盗链 :1   2   3   4    5    6    7(官网)  

nginx代理后登陆网页失败(请求头被保留)

场景:进入登陆界面,输入错误的账号密码,提示密码错误,说明代理配置没有问题。但是输入正确的账号密码,返回400。

本场景(vue和微服务)的认证原理:输入正确的账号密码,路由到认证服务后,返回给你auth-token文件,以后的请求在请求头上都会有token文件,服务器再验证token文件,如果正确就登陆成功。

      而Nginx有underscores_in_headers 参数,nginx默认request的header的那么中包含’_’时,会自动忽略掉。

       解决方法是:在nginx里的nginx.conf配置文件中的http部分中添加如下配置:underscores_in_headers on; (默认 underscores_in_headers 为off)

 Nginx的超时timeout配置详解       2       3

Nginx访问限制配置

Nginx中的stub_status模块

Waiting: 开启keep-alive的情况下,这个值等于 active – (reading + writing),意思就是Nginx已经处理完成,正在等候下一次请求指令的驻留连接.
所以,在访问效率高,请求很快被处理完毕的情况下,Waiting数比较多是正常的.如果reading +writing数较多,则说明并发访问量非常大,正在处理过程中.

 nginx代理的后端web返回301或302时,会默认返回80端口,如下,location:返回的是80端口。所以nginx用监听非80端口来代理时,就会出错,因为跳到了80端口了

proxy_redirect

[root@test02 ~]# curl -i aaa.com:90
HTTP/1.1 302
Server: nginx/1.16.0
Date: Thu, 19 Mar 2020 07:44:10 GMT
Content-Length: 0
Connection: keep-alive
Location: http://aaa.com/index
Content-Language: en

proxy_redirect可以修改返回给客户端的 location地址,即301的地址
没配置前 Location: http://aaa.com/index
1)
proxy_redirect http://aaa.com/index /aa;
会拿 Location: http://aaa.com/index 来匹配,可以匹配http://aaa.com/index  或者http://aaa.com/

一段一段从前往后匹配:如http://aaa.com/xxl-job-admin/toLogin 可匹配 http://aaa.com/xxl-job-admin/
然后跳转 http://aaa.com:90/aa
2)
proxy_redirect http://aaa.com/ /aa;
    Location: http://aaa.com:90/aaindex
3)
proxy_redirect http://aaa.com /aa;
    Location: http://aaa.com:90/aa/index
4)
proxy_redirect http://aaa.com http://iii.com/aa;
    Location: http://iii.com/aa/index

 
原文地址:https://www.cnblogs.com/fanever/p/11162945.html