玖零定制问题修复的gravatar头像
玖零定制问题修复 2017-11-09 13:52:15
Nginx(九):搭建静态资源下载服务器

最近小编项目上下,由于是APP的 所有对于下载需求比较大,主要来源还是扫码下载,原先用户少的时候还可以通过服务转发模式进行下载,发现当下载人数增加到一定数量后对于服务器的压力还是蛮大的,导致服务有时候会延迟或者崩溃,为了尽快改善当前状况,小编决定采用Nginx做为静态资源下载服务器,这样可以在负载后把下载的压力移植到其他带宽较好的服务器上,而不需要占用主服务器的资源。

废话不多说直接撸代码

user  root;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

worker_rlimit_nofile 65535;


events {
    use epoll;#
    worker_connections  65535;

}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream picture {
      server 123.56.76.187  ;
    }
    
    upstream downloadserver{
      server 123.58.76.187;
    }	



    server {
        listen       80;
        server_name  localhost;
        
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


  server {     
         listen   80;
         charset  utf-8;
         server_name   downloadserver.sht18.com;
         location / {    
          root   /root/app/;
          if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|apk)$){
            add_header Content-Disposition: 'attachment;';
            }        
         autoindex     on;
         autoindex_exact_size   off;
         autoindex_localtime    on;
       }
}
}

最主要的就是配置文件的最后一个server服务的配置.监听80端口,因为是线上服务器都是域名访问模式,所以直接监听80端口就可以,如果是测试服务器通过ip访问的,可以做代理转发,也可以直接修改监听端口号,

在location块中配置下文件服务存放地址,以便Nginx在下载的时候直接通过文件名从文件目录下查找文件信息

正则匹配的时候是把已这些为结尾的文件直接通过浏览器下载就可以。

我们看下下载结果,下载我们产品商互通商家端,令掌柜

Nginx(九):搭建静态资源下载服务器

Nginx(九):搭建静态资源下载服务器

好了 以上Nginx 静态资源服务器就搭建完成了,后续会为大家继续分享关于Nginx静态资源服务器限速下载等内容


打赏
最近浏览
xuan0422  LV6 2023年6月5日
fgd1202 2021年4月20日
暂无贡献等级
yzyqyywe  LV1 2020年6月18日
275127546  LV7 2018年5月28日
玖零定制问题修复  LV34 2018年5月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友