zno2

nginx 配置图片服务器 (window版本)

下载  https://nginx.org/download/nginx-1.22.0.zip

配置nginx二级域名

①找到配置文件

例如:%nginx_home%/conf/nginx.conf

②配置

#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

    # Serving Static Content
    server {
        listen       8080;
        server_name  localhost;
        location /pic/ {
            root D:;
        }
    }
}

红色字部分为新增内容

③重载nginx

%nginx_home%/nginx.exe -s reload

查看帮助

%nginx_home%/nginx.exe -h

nginx version: nginx/1.6.1
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

 

④放置123.png 到D:\pic 文件夹下

⑤访问 http://localhost:8080/pic/123.png

 

补充:如何设置响应头,使请求的文件按指定名称下载

    server {
        listen       8080;
        server_name  localhost;
        location /pic/ {
            add_header Content-Disposition "attachment;filename=$arg_filename";
            root D:;
        }
    }

 

参考 内置变量 

https://nginx.org/en/docs/http/ngx_http_core_module.html#server

$arg_name
  argument name in the request line 
$args
  arguments in the request line

 

举例:http://localhost:8080/pic/test.png?filename=aaa.png

$arg_<这里是请求参数名>

需要注意attachment;filename= 这个格式中含有分号,与nginx语法冲突,需要用双引号括起来

 

补充:

线上发现一个问题,如果filename中包含英文逗号,文件无法下载并提示 ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION

 

配置追加的header是 Content-Disposition ,因为英文逗号被认为这个header设置了多个值,才出现的错误

举例来说:

Cache-Control: no-cache, no-store, must-revalidate

但是Content-Disposition是不支持多个值的,它的格式是:

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Disposition

 更健壮的写法(文件名用双引号):

    server {
        listen       8080;
        server_name  localhost;
        location /pic/ {
            add_header Content-Disposition 'attachment;filename="$arg_filename"';
            root D:;
        }
    }

 

posted on 2016-08-02 21:46  zno2  阅读(420)  评论(0编辑  收藏  举报

导航