Apache
<IfModule mod_rewrite.c>
RewriteEngine On
# '-s' (is regular file, with size)
# '-l' (is symbolic link)
# '-d' (is directory)
# 'ornext|OR' (or next condition)
# 'nocase|NC' (no case)
# 'last|L' (last rule)
# 'QSA' 追加请求字符串
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-s
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://img.xx.com/$1 [NC,L]
</IfModule>
nginx
设置文件不存在时 直接302到线上
location ~ .*$
{
if (!-e $request_filename) {
rewrite ^/(.*)$ http://img.55.com/$1 last;
}
}
坑点1
if 后面必须有空格
正则{}太难使用了
location ~ (.+)$
{
#http://cc.xxx.com.cn/abc.png => http://static.lingmax.top/xxx.com.cn/abc.png
set $tohost "static.lingmax.top";#配置域名
set $path "";
set $pathpp $1;
set $paths "/www/wwwroot";#配置主目录 安装域名命名规范去即可
set $path1 "$paths/dasawdsdaww";#配置一个不存在的路径
#return 502 "$1";
#匹配结果[a-z]{3,}\.[a-z.]{2,} 取出主域名
if ( $host ~* ([a-z0-9-][a-z0-9-][a-z0-9-]+\.[a-z][a-z][a-z]?(\.[a-z][a-z]+)?)$) {
#return 200 "$1";
set $path $1;
}
#排除 ip暂时没有关联
#if ($host ~* [0-9]+\.[0-9]+$ ) {
# set $path "";
#}
#文件是否存在
if ( -d "$paths/$host" ) {
set $path $host;
}
#排除 跳转到目的地
if ($host = $tohost) {
set $path "";
}
#return 200 "path: $path <br> path1: $path1 <br> pathpp: $pathpp host: $host";
#排除 SEO和浏览器主入口文件
if ( $pathpp ~* .*(sitemap\.xml|sitemap\.txt|robots\.txt|.+\.html|.+\.htm|.+\.php|/|/[^./?#]+)([?#].*)?$) {
set $path "";
}
#判断是否需要生成文件夹
if ($path != "") {
set $path1 "$paths/$path";
}
#return 200 "path: $path <br> path1: $path1 <br> pathpp: $pathpp host: $host";
#文件是否存在
if ( -d $path1 ) {
#return 301 http://$tohost/$path/$pathpp;
rewrite ^/(.*)$ "http://$tohost/$path/$1" permanent;
}
}
反向代理的使用
location ^~ /xhxx {
index index.html;
alias /www/wwwroot/fhexam/html;
#return 200 123$request_filename;
#try_files $request_filename $request_filename/index.html ;
}
#这里是考试系统需要调用的接口
location / {
if ( -e $request_filename ){
rewrite ^(.*)$ /xhxx$1 last;
return 200 $request_filename;
}
#return 200 $request_filename;
#proxy_redirect ~^https?://[^/]/api/(.*) http://127.0.0.1:8081/$1;
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_set_header Connection "colse";
proxy_pass http://127.0.0.1:8081/;
}