详细指南:如何安装Nginx并进行服务器文件代理下载

485d8910df3fbf7b9f672ac86c854397.jpeg

主旨

nginx服务,现在是最好用的一种代理服务,日常我们代理的一般都是端口转发和流量控制,那么我们如何通过nginx来实现文件下载呢?就是通过nginx服务,通过url来下载文件。

Nginx是一款轻量级的web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。

环境

linux服务器
nginx软件

软件下载

公众号“运维家”后台回复“nginx”,即可获取下载地址。

nginx安装

1、安装编译工具及库文件

[yunweijia@localhost jier]$ sudo yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

2、安装 PCRE

[yunweijia@localhost jier]$ cd /usr/local/src/
[yunweijia@localhost src]$ sudo wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz  下载
[yunweijia@localhost src]$ sudo tar xf pcre-8.35.tar.gz  # 解压
[yunweijia@localhost src]$ cd pcre-8.35/
[yunweijia@localhost pcre-8.35]$ sudo ./configure  # 编译
[yunweijia@localhost pcre-8.35]$ sudo make && make instal  # 安装
[yunweijia@localhost pcre-8.35]$ pcre-config --version  # 版本查看
8.32
[yunweijia@localhost pcre-8.35]$

3、安装nginx

[yunweijia@localhost jier]$ pwd
/home/yunweijia/jier
[yunweijia@localhost jier]$ ls
nginx-1.14.2.tar.gz
[yunweijia@localhost jier]$ tar xf nginx-1.14.2.tar.gz 
[yunweijia@localhost jier]$ cd nginx-1.14.2/
[yunweijia@localhost nginx-1.14.2]$ sudo ./configure --prefix=/home/yunweijia/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
[yunweijia@localhost nginx-1.14.2]$ sudo make && make install

4、配置调整

[yunweijia@localhost nginx-1.14.2]$ cd /home/yunweijia/nginx/
[yunweijia@localhost nginx]$ ls
conf  html  logs  sbin
[yunweijia@localhost nginx]$ vim conf/nginx.conf
# 以下内容是配置文件的第二行进行配置
user root;
# 以下配置是唉server中进行新增
        location ~* (.*.txt) {  # 代理后缀为txt的
            add_header  Content-Type    "application/octet-stream";
            if ( $args ~ ^filename=(.*) ) {
                add_header  Content-Disposition "attachment; filename=$1";
            }
            yunweijia /home/yunweijia/txt/;  # 在这个目录下
        }

5、启动nginx

[yunweijia@localhost nginx]$ sudo ./sbin/nginx -t  # 检查配置
nginx: the configuration file /home/yunweijia/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/yunweijia/nginx/conf/nginx.conf test is successful
[yunweijia@localhost nginx]$ sudo ./sbin/nginx  # 启动

6、验证

在"/home/yunweijia/txt/"中新建文件,测试下载。

下载url为:http://ip地址:端口号/文件名

[yunweijia@localhost nginx]$ mkdir /home/yunweijia/txt/
[yunweijia@localhost nginx]$ cd /home/yunweijia/txt/
[yunweijia@localhost txt]$ ls
[yunweijia@localhost txt]$ echo ceshi > 1.txt
[yunweijia@localhost txt]$ cat 1.txt 
ceshi
[yunweijia@localhost txt]$

浏览器输入地址为:http://192.168.112.130/1.txt

文件下载成功。

至此,用nginx代理文件下载完毕。

32ddd9cc66562f67791f5aec4975cc3c.jpeg

如果文章内容对你有所帮助,请关注并分享我的公众号【运维家】。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值