配置nginx的文件下载

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {

    worker_connections 1024;

}

http {

    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;

    tcp_nodelay         on;

    keepalive_timeout   65;

    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;

    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.

    # See http://nginx.org/en/docs/ngx_core_module.html#include

    # for more information.

    include /etc/nginx/conf.d/*.conf;

    server {

        listen       80 default_server;

        listen       [::]:80 default_server;

        server_name  _;

        # Load configuration files for the default server block.

        include /etc/nginx/default.d/*.conf;

        location / {

            root    /usr/share/nginx/html/download;

        autoindex on;    #开启索引功能

        autoindex_exact_size off;  #关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)

        autoindex_localtime on;   #显示本机时间而非 GMT 时间

        }

        error_page 404 /404.html;

            location = /40x.html {

        }

        error_page 500 502 503 504 /50x.html;

            location = /50x.html {

        }

    }

}

创建下载路径/usr/share/nginx/html/download

上传文件并授权755

 

在使用nginx做静态资源服务器时,配置完成后通过浏览器访问一直报404 Not Found错误,本人nginx配置信息如下:

location /images/ { root /mnt/upload/files; }

所有文件存放在/mnt/upload/files

分析:

发现是配置的问题,配置静态路径有两种方式,之前配置的是直接在URL里写根目录,而现在配置是一个有前缀的URL,所以报404 Not Found错误了。

root配置会在配置的目录后跟上URL,组成对应的文件路径,即想访问的地址是:

nginx根据配置走的文件路径是

/mnt/upload/files/images/a.png

而我需要的是

/mnt/upload/files/a.png

而Nginx提供了另外一个静态路径配置:alias配置

官方root配置

Sets the root directory for requests. For example, with the following configuration
location /i/ {
    root /data/w3;
}
The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request

官方alias配置

Defines a replacement for the specified location. For example, with the following configuration
location /i/ {
    alias /data/w3/images/;
}
on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.

当访问/i/top.gif时,root是去/data/w3/i/top.gif请求文件,alias是去/data/w3/images/top.gif请求,也就是说

root响应的路径:配置的路径+完整访问路径(完整的location配置路径+静态文件)

alias响应的路径:配置路径+静态文件(去除location中配置的路径)

解决办法:

location /images/ {  
     alias /mnt/upload/files/;  
}

注意:使用alias时目录名后面一定要加“/”;一般情况下,在location/中配置root,在location /*中配置alias。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值