nginx中root和alias的区别
访问地址:http://localhost/website
案例1:
location /website/ {
root /var/lib/www;
autoindex on;
}
案例2:
location /website/ {
alias /var/lib/www;
autoindex on;
}
如果是root,那么真实访问路径是:/var/lib/www/website/
如果是alias,那么真实的访问路径是:/var/lib/www/
2、nginx中try_file用法
它可以按顺序检查文件是否存在,如果存在就返回,
location /images/ {
root /opt/html/;
try_files $uri $uri/ /images/default.gif;
}
固定写法
$uri 是请求文件的路径
$uri/ 是请求目录的路径
意思是:在/opt/html/images/default.gif找到这个文件并返回
3、路由转发
http://localhost/apple-app-site-association
其中apple-app-site-association是一个文件,目的是返回apple-app-site-association文件--------》IOS唤醒配置
location ~(apple-app-site-association) {
alias /usr/local/web_project/weixin/;
try_files $uri $uri/ apple-app-site-association;
autoindex on;
}
http://localhost/app/link
目的是返回apple-app-site-association文件---------》IOS唤醒配置
location ^~ /app/link/ {
root /usr/local/web_project/weixin/;
try_files $uri $uri/ apple-app-site-association;
autoindex on;
}
或者如下
location /app/link/ {
root /usr/local/web_project/weixin/;
try_files $uri $uri/ apple-app-site-association;
autoindex on;
}
http://localhost/test.txt
返回test.txt文件------》微信公众账号对网址进行授权
location ~ \.(txt)$ {
root /mnt/web_project/test/weixin/;
autoindex on;
}
4、location匹配策略
location / {
# 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
# 但是正则和最长字符串会优先匹配
}location /documents/ {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
}location ^~ /documents/ {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,
# 只有后面的正则表达式匹配到时,还要继续往下搜索内容更加长的
}
1) 先检测匹配项的内容为非正则表达式修饰语的 location,然后再检测匹配项的内容为正则表达式修饰语的 location。
2) 匹配项的内容为正则与非正则都匹配的 location,按照匹配项的内容为正则匹配的 location 执行。
3) 所有匹配项的内容均为非正则表达式的 location,按照匹配项的内容完全匹配的内容长短进行匹配,即匹配内容多的 location 被执行。
4) 所有匹配项的内容均为正则表达式的 location,按照书写的先后顺序进行匹配,匹配后就执行,不再做后续检测。
5、location 基本语法使用情况
语法格式:location [=|~|~*|^~|@] pattern { ... }
[=|~|~*|^~|@] 被称作 location modifier ,这会定义 Nginx 如何去匹配其后的 pattern ,以及该 pattern 的最基本的属性(简单字符串或正则表达式
语法内容:
= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,以指定匹配模式开头的 URI 被匹配,不同的是,一旦匹配成功,那么 Nginx 就停止去寻找其他的 Location 块进行匹配了(与 Location 匹配顺序有关)
~ 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
location 好比是java中的if条件,
[=|~|~*|^~|@] 好比是java中的 equals、contains等等
pattern 好比是java中的值
5.1 =
Example:
server {
server_name localhost;
location = /abcd {
}
}
匹配情况:
http://localhost/abcd # 正好完全匹配
http://localhost/ABCD # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
http://localhost/abcd?param1m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
http://localhost/abcd/ # 不匹配,因为末尾存在反斜杠(trailing slash),Nginx 不认为这种情况是完全匹配
http://localhost/abcde # 不匹配,因为不是完全匹配
5.2 (None) 什么都不写---------------》相当于/ 通用匹配
可以不写 location modifier ,Nginx 仍然能去匹配 pattern 。这种情况下,匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,不能使用正则表达式
Example:
server {
server_name localhost;
location /abcd {
}
}
匹配情况:
http://localhost/abcd # 正好完全匹配
http://localhost/ABCD # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
http://localhost/abcd?param1m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
http://localhost/abcd/ # 末尾存在反斜杠(trailing slash)也属于匹配范围内
http://localhost/abcde # 仍然匹配,因为 URI 是以 pattern 开头的
5.3 ~
这个 location modifier 对大小写敏感,且 pattern 须是正则表达式
Example:
server {
server_name localhost;
location ~ ^/abcd$ {
}
}
匹配情况:
http://localhost/abcd # 完全匹配
http://localhost/ABCD # 不匹配,~ 对大小写是敏感的
http://localhost/abcd?param1m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
http://localhost/abcd/ # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
http://localhost/abcde # 不匹配正则表达式 ^/abcd$
5.4 ~*
与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式
Example:
server {
server_name localhost;
location ~* ^/abcd$ {
}
}
匹配情况:
http://localhost/abcd # 完全匹配
http://localhost/ABCD # 匹配,这就是它不区分大小写的特性
http://localhost/abcd?param1m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
http://localhost/abcd/ # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
http://localhost/abcde # 不匹配正则表达式 ^/abcd$
5.5 ^~
匹配情况类似 2. (None) 的情况,以指定匹配模式开头的 URI 被匹配,不同的是,一旦匹配成功,那么 Nginx 就停止去寻找其他的 Location 块进行匹配了(与 Location 匹配顺序有关)
5.6 @
用于定义一个 Location 块,且该块不能被外部 Client 所访问,只能被 Nginx 内部配置指令所访问,比如 try_files or error_page
server {
listen 80;
server_name example.com; location / {
root /home/example/dist;
try_files $uri $uri/ @router; # 配置使用路由
index index.html index.htm;
}
# 路由配置信息
location @router {
rewrite ^.*$ /index.html last;
}
}
6、正则表达式使用
包含五大类,如下:
(1)字符类
(2)数量限定符
(3)位置限定符
(4)特殊符号
(5) 其他普通字符集及其替换
6.1 字符类
6.2 数量限定符
6.3 位置限定符
6.4 特殊符号
6.5 其他普通字符集及其替换
列子:
1.手机号码
得出结论:[3579] 只有第二位数字是3或者5或者7或者9就可以。千万不要认为匹配3479。
2.非零的正整数
3.非零开头的最多带两位小数的数字
4.由数字和26位字母组成的字符串
5.QQ号,从10000开始
6.IP地址
\d+\.\d+\.\d+\.\d+
7.判断账号是否合法
^[a-zA-Z0-9][a-zA-Z0-9_]{4,15}$
8.日期格式
^\d{4}-\d{1,2}-\d{1,2}
9.其他匹配
regex = "(^[a-z]+)([0-9]+)([A-Z]+$)";
和 regex = "([a-z]+)([0-9]+)([A-Z]+)";匹配a1234A, 都是True.
至此就了解到正则表达式。
7、nginx flag标记有哪些
11111
8、nginx根据不同端展示不同展示(电脑端和手机端)
server {
listen 80;
server_name dev.www.stone-123.com;
#rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。 location / {
root /mnt/vdb1/web_project/dev/official-website-web;
if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(
WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(Sony
Ericsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Mot
orola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)
|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)
|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" )
{
root /mnt/vdb1/web_project/dev/official-website-h5;
}
try_files $uri $uri/ @router; # 配置使用路由
index index.html index.htm;
error_page 404 /index.html;
}
location @router {
rewrite ^.*$ /index.html last;
}
}
9、反向代理
(1)、前端项目server {
listen 80;
server_name dev.stone.com;
#rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。 location / {
root /usr/local/web_project/dev/background-qbank-web;
index index.html index.htm;
try_files $uri $uri/ @router; # 配置使用路由
error_page 404 /index.html;
}
location /ueditor/ {
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://localhost:8081/;
}
location @router {
rewrite ^.*$ /index.html last;
}
}
10、解决后台管理系统--访问后台api接口跨域问题server {
listen 80;
server_name test-stone.com;
#rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。 location / {
root /usr/local/web_project/test_ticket_admin;
index index.html index.htm;
error_page 404 /index.html;
}
location /api/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://120.270.161.102:9202/;
} }
重点:如果proxy_pass http://120.270.161.102:9202/; 后面带斜杠会导致多加一层
可能会转发过去会多加一层(api) 如下所示:www.test.com/api/api/test
如果去掉斜杠 如下所示:www.test.com/api/test11、nginx中配置域名名字过长会报错,启动失败:could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
解决方法:
http {
server_names_hash_bucket_size 64;
}
12、nginx后端服务超时时间设置
重点:光设置keepalive_timeout参数没有用
只有设置这两个才行,如下设置2小时
proxy_send_timeout 7200s;
proxy_read_timeout 7200s;
location / {
add_header Strict-Transport-Security "max-age=31536000";
proxy_buffer_size 64k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 128k;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade-Insecure-Requests 1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
#proxy_connect_timeout 180;
proxy_send_timeout 7200s;
proxy_read_timeout 7200s;
proxy_cache_bypass $http_upgrade;
proxy_pass http://localhost:8311;
}