服务器为图片做反向代理配置


本实现的原理是:

A服务器上的图片资源,通过A服务器的nginx设置反向代理,让B服务器的cdn实现图片返回。
因有一些图片需要做自适应的尺寸变动,所以B服务器需要对一些不存在的图片让C服务器代理进行resize保存。

服务器A的配置:

server {
...
    # cdn file rewrite proxy
    location /temps/products/ {
        #expires      1d;
        proxy_pass  http://cdn.example.com/temps/products/;
    }
    location /temps/offline/ {
        #expires      1d;
        proxy_pass  http://cdn.example.com/temps/offline/;
    }
...
}

通过以上的配置,当A服务器访问/temps下的那两个文件夹,则会反向代理到B服务器cdn站点下的对应目录;

然后B服务器配置如下:

server {
    listen       0.0.0.0:80;
    server_name  cdn.example.com;
    root /datas/website/com.example.openapi/storage;
    #index index.php index.html index.htm;

    ## proxy pass if file not exists try_files
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        #expires      30d;
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index/image/make?path=$1  break;
            proxy_pass http://api.example.com;
        }
    }
    location ~ .*\.(js|css)?$ {
        #expires      12h;
    }
...
}

通过B服务器,如果对应文件存在,则会自动返回文件;
若B服务器不存在对应文件,则又会反向代理重写访问到一个后端域名C服务器,进行图片resize生成;

C服务器配置:

server {
    listen       localhost:80;
    server_name  api.example.com;

    root /datas/website/com.example.openapi/public;
    index index.php index.html index.htm;

    access_log  /datas/logs/nginx/access.com.example.api.log main;
    error_log   /datas/logs/nginx/error.com.example.api.log;
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    ##pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PHP_VALUE  "open_basedir=./:$document_root/../:/tmp/:/var/lib/php/";
        include        fastcgi_params;
    }

    ##framwork rewrite
    location / {
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=/$1  last;
        }
    }

}

版权声明,转载请附上原文链接及本声明: https://blog.yongit.com/note/1084468.html