server {
listen 6521;
server_name www.example.com;
location / {
root /data/www.example.com/webui;
try_files $uri $uri/ index.html;
}
location /adm/ {
root /data/www.example.com/admui/;
rewrite ^/adm/(.*)$ /index.html break;
}
location /appapi/ {
proxy_pass http://127.0.0.1:8876/;
}
location /admapi/ {
proxy_pass http://127.0.0.1:8875/;
}
}
方式二
server {
listen 6521;
server_name sec.tonyhr.cn;
location / {
root /data/baoan/webui;
# index.html 前面不要加 "/"
try_files $uri $uri/ index.html;
}
location /adm {
#rewrite ^/adm/?(.*)$ /$1;
alias /data/baoan/admui/;
# index.html 前面不要加 "/"
try_files $uri $uri/ index.html /404;
}
location /appapi/ {
proxy_pass http://127.0.0.1:8876/;
}
location /admapi/ {
proxy_pass http://127.0.0.1:8875/;
}
}
root 与aslias的区别
location /i/ {
root /data/w3; # 访问/i/top.html => /data/w3/i/top.html
}
location /i/ {
alias /data/w3/files/; # 访问/i/top.html => /data/w3/files/top.html
}
try_files是nginx的一个指令,它的作用是:按顺序查找多个文件是否存在,并返回第一个找到的文件。语法为:
try_files file1 file2 ... fileN;
例如:
location / {
try_files $uri $uri/ /index.html;
}
这个配置的作用是: 1. 首先查找$uri是否匹配到一个实际文件,如果是则返回该文件; 2. 其次查找$uri/目录下是否存在index.html文件,如果是则返回该文件; 3. 最后返回/index.html文件。 所以,try_files可以实现一个“搜索-回退”的文件路径查找逻辑。它会按顺序检查每个文件或路径是否存在,并返回第一个找到的文件,如果所有的文件或路径都不存在则返回最后一个参数。常见的使用场景是: 1. 根据文件是否存在判断是返回文件还是目录; 2. 根据后缀名判断是否重定向到默认文件(如index.html); 3. 实现一个搜索路径的文件回退机制。 try_files指令通过这种逐一尝试的方式,可以实现判断文件存在与类型的效果。这是一个nginx中非常有用的指令。
快速域名验证
location ~ \.txt$ {
return 200 '4pR1rmHc62RwD5Hj';
}