Nginx带你轻松搭建高效文件服务器 (使用nginx搭建文件服务器)

在当今互联网时代,文件传输和存储已成为人们生活中必不可少的一部分。建立一个高效快速的文件服务器不仅可以方便用户上传和下载各种文件,同时还能节省成本和提高工作效率。本文将带领大家一步步使用Nginx来搭建高效文件服务器。

什么是Nginx?

Nginx是一款轻量级的Web服务器软件,由俄罗斯的Igor Sysoev开发,其特点是占用资源少,能够支持一般的HTTP服务,同时还提供了负载均衡、反向代理等功能。Nginx因其出色的性能和较佳的稳定性成为了Web服务器软件领域中的佼佼者。

为什么选择Nginx?

使用Nginx搭建文件服务器有以下几个优点:

1. 高效快速:Nginx简洁高效,对处理静态文件和动态文件的支持非常好,可以提供更高效的性能表现。

2. 安全可靠:Nginx的配置文件比较直观,降低了被攻击的可能性,同时也增加了服务器的稳定性。

3. 低成本:Nginx是开源软件,使用成本较低,同时也节约了硬件成本,减少了机房空间的使用。

搭建步骤:

1. 安装Nginx

进入官网http://nginx.org/en/download.htmldownload,下载最新稳定版本的Nginx

将下载后的tar.gz文档解压到目标目录:tar -zxvf nginx-xx.tar.gz

切换到Nginx目录:cd nginx-xx

执行configure命令:./configure

执行make和make install命令,将Nginx安装到指定目录:make && make install

启动Nginx:/usr/local/nginx/in/nginx

2. 配置Nginx

默认情况下,Nginx配置文件在/usr/local/nginx/conf/nginx.conf,打开此文件并按照以下步骤进行配置:

在http块中增加:

server {

listen 80;

server_name 127.0.0.1:80;

root /var/www;

autoindex on;

location / {

autoindex on;

}

error_page 404 /404.html;

location = /40x.html {

root /usr/share/nginx/html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}

}

其中,listen指定Nginx监听的端口,server_name表示指定的域名或者IP地址,root为网站的根目录,autoindex on表示开启索引文件的功能。

如果需要上传文件,还需要增加以下配置:

http {

client_max_body_size 20M;

server {

listen 80;

server_name 127.0.0.1;

root /var/www;

index index.html index.htm;

location /upload {

client_max_body_size 20m;

alias /var/www/upload;

autoindex on;

autoindex_exact_size off;

autoindex_localtime on;

}

}

}

其中,client_max_body_size指定上传文件的大小,location指定上传文件的目录。

3. 部署文件

进入刚刚配置的/var/www目录,在其中创建upload目录用于存放上传的文件。

将需要上传的文件上传到/upload目录中。上传时如果出现如下错误提示:

client intended to send too large body: bytes inludeing…

可以将Nginx的配置文件文件中的client_max_body_size参数适当增大。

4. 访问文件

在浏览器中输入http://localhost/upload/即可访问到/upload目录中上传的文件。

通过本文的讲解,相信大家已经掌握了使用Nginx来搭建高效文件服务器的方法。Nginx以其高效性、安全性和低成本成为了众多企业和网站的首选服务器。使用Nginx搭建文件服务器不仅能够方便地存储和分享各类文件,同时还具有学习和研究的价值。让我们一起来体验Nginx带来的高效、稳定、安全和愉悦的文件分享体验吧!

相关问题拓展阅读:

Nginx+php(FastCGI)高性能服务器怎么搭建呢?

Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码册陆以类BSD许可证的形式发布。在高并发连接的情况下,Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。根据我的测试结果,Nginx 0.6.31 + PHP 5.2.6 (FastCGI) 可以承受3万以上的并发连接数,相当于同等环境下Apache的10倍。

【感兴趣的话点击此处,免费了解一下】

4GB内存的服务器+Apache(prefork模式)一般只能处理3000个并发连接,因为它们将占用3GB以上的内存,还得为系统预留1GB的内存。我曾经就有两台Apache服务器,因为在配置文件中设置的MaxClients为4000,当Apache并发连接数达到3800时,导致服务器内存和Swap空间用满而崩溃。而这台 Nginx 0.6.31 + PHP 5.2.6 (FastCGI) 服务器在3万并发连接下,开启的10个Nginx进程消耗150M内存(15M*10=150M),开启的64个php-cgi进程消耗1280M内存(20M*64=1280M),加上系统自身消耗的内存,总共消耗不到2GB内存。仔激如果服务器内存较小,完全可以只开启25个php-cgi进程,这样php-cgi消耗的总内存数才500M。

亿万克作为中国战略性新兴产业领军品牌,拥有中国之一、世界前二的行业领先技术,致力于新型数据中心建设,构筑云端安全数字底座,为客户提供集产品研发、生产、部署、运维于一体的服务器及IT系统解决方案业务,所有产品和技术完全拥有自主知识产权,应用领域州戚顷涵盖云计算、数据中心、边缘计算、人工智能、金融、电信、教育、能源等,为客户提供全方位安全自主可控技术服务保障。

Nginx+php(FastCGI)+Memcached+Mysql+APC Nginx高性能web服务器安装 APC模块安装 linux apc 配置安装

参考资料

问题补充:

不是 主要是百度不收录 他们说经常在百度知道什么的提问题,然后发表一些好文章附带自己的网站,这样会收录快一些,谢谢一粗搏或楼!谢谢大家的支持!

更佳答案 Nginx+php(FastCGI)+Memcached+Mysql+APC Nginx高性能web服务器安装 APC模块安装 linux apc 配置 fastCGI安装配置3前言*

Nginx+php(FastCGI)+Memcached+Mysql+APC 是目前主流的高性能服银握务器搭建方式!适合大中型网站,小型站长也可以采用这种组合!

Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括国内更大的电子地图MapBar、新浪博客、新浪播客、网易新闻等门户网站频道,六间房、56.com等视频分享网站,Discuz!官方论坛、水木社区等知名论坛,豆瓣、YUPOO相册、海内SNS、迅雷在线等新兴Web 2.0网站,更多的网站都在使用Nginx配置。

【Nginx+php(FastCGI)+Memcached+Mysql+APC Nginx高性能web服务器安装 APC模块安装 linux apc 配置 fastCGI安装配置】

下载所需的安装包:这里采用源码包编译安装:本博客集成下载

/download/nginx-0.7.61.tar.gz

/download/pcre-8.01.tar.gz

/download/memcache-2.2.5.tgz

/download/libevent-1.4.12-stable.tar.gz

/download/APC-3.1.4.tgz

下载到 /usr/src下

另外还有两个包mysql-5.1.41.tar.gz、php-5.3.5.tar.gz 【其他相似版本也可以!】可以在官网下载。

一、正式安装Nginx、【安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等】

(1)首先安装pcre:

cd /usr/src &&tar xzf pcre-8.01.tar.gz &&cd pcre-8.01 && ./configure –prefix=/usr/local/pcre &&make &&make install

(2)、然后再安装nginx :

useradd www && cd /usr/src && tar xzf nginx-0.7.61.tar.gz &&cd nginx-0.7.61 && ./configure –prefix=/usr/local/岩伍nginx –with-http_stub_status_module –with-openssl=/usr/ –with-pcre=/usr/src/pcre-8.01 –user=www –group=www &&make &&make install

【nginx注意* –with-pcre=/usr/src/pcre-8.01指向的是源码包解压的路径,而不是安装的路径,否则会报

make: Error 127 错误】

二、接下来安装mysql

cd /usr/src && tar xzf mysql-5.1.41.tar.gz && cd mysql-5.1.41 && ./configure –prefix=/usr/local/mysql/ –enable-assembler –with-extra-charsets=complex –enable-thread-safe-client –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile –with-plugins=innobase && make &&make install

(2)、mysql安装完毕,创建mysql用户和组并初始化数据库,并启动数据库。

cd /usr/local/mysql && useradd mysql && chown -R mysql:mysql /usr/local/mysql && /usr/local/mysql/bin/mysql_install_db –user=mysql && chown -R mysql:mysql var/ && ./bin/mysqld_safe –user=mysql &

即可。【如果mysql启动报错,请检查 /usr/local/mysql/var 目录,mysql是否有权限】

三、安装 php :

cd /usr/src &&tar xzf php-5.3.5.tar.gz && cd php-5.3.5 && ./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-mysql=/usr/local/mysql –with-mysqli=/u

sr/local/mysql/bin/mysql_config –with-iconv-dir=/usr/local –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-li

bxml-dir=/usr –enable-xml –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem

–enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fpm –enable-sockets && make &&make install

安装完毕!【注意这个参数在此可以不加–enable-fastcgi;其他之前版本需要加上,以上安装根据自己的选择添加,如果报错,根据具体报错找原因】

四、整合Nginx和php(FastCGI)安装完php-5.3.5后支持fastCGI

(1)、配置nginx ,拷贝nginx配置文件:

user www www;

worker_processes 8;

error_log /usr/local/logs/nginx/error.log crit;

pid/usr/local/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 51200;

events

{

use epoll;

worker_connections 51200;

}

http

{

includemime.types;

default_type application/octet-stream;

#charset gb2312;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;

client_max_body_size 8m;

sendfile on;

tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

gzip on;

gzip_min_length 1k;

gzip_buffersk;

gzip_http_version 1.0;

gzip_comp_level 2;

gzip_typestext/plain application/x-javascript text/css application/xml;

gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m;

server

{

listen;

server_name wgkgood.gicp.net;

index index.php index.htm index.html;

root /usr/webapps/www;

#limit_conn crawler 20;

location ~ .*\.(php|php5)?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expiresd;

}

location ~ .*\.(js|css)?$

{

expiresh;

}

log_format access ‘$remote_addr – $remote_user “$request” ‘

‘$status $body_bytes_sent “$http_referer” ‘

‘”$http_user_agent” $http_x_forwarded_for’;

access_log /usr/local/logs/nginx/access.log access;

}

}

Nginx配置完毕!启动nginx ;/usr/local/nginx/in/nginx 即可,重启nginx命令如下/usr/local/nginx/in/nginx –s reload

此配置文件仅供参考,感谢张宴老师!

(2)、配置fcgi.conf文件如下

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param QUERY_STRING$query_string;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param CONTENT_TYPE$content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME$fastcgi_script_name;

fastcgi_param REQUEST_URI$request_uri;

fastcgi_param DOCUMENT_URI$document_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR$remote_addr;

fastcgi_param REMOTE_PORT$remote_port;

fastcgi_param SERVER_ADDR$server_addr;

fastcgi_param SERVER_PORT$server_port;

fastcgi_param SERVER_NAME$server_name;

# PHP only, required if PHP was built with –enable-force-cgi-redirect

fastcgi_param REDIRECT_STATUS;

五、配置php配置文件:

cd /usr/local/php5/etc/ && cp

php-fpm.conf.default php-fpm.conf 然后根据提示修改php-fpm.conf里面的选项。

配置完毕后,启动php-fpm

cp /usr/src/php-5.3.5/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 然后启动 /etc/init.d/php-fpm start 即可。

六、安装apc配置:

cd /usr/src && tar xzf APC-3.1.4.tgz &&cd APC-3.1.4

/usr/local/php5/bin/phpize 【增加扩展模块】

./configure –enable-apc –enable-apc-mmap –with-php-config=/usr/local/php5/bin/php-config

&&make&& make install

安装完后会生成一个apc.so在/usr/local/php5/lib/php/extensions/no-debug-non-zts/里面。

七、安装memcached,使fastcGI支持memcached

首先安装libevent,

cd /usr/src && tar xzf libevent-1.4.12-stable.tar.gz && cd libevent-1.4.12-stable && ./configure –prefix=/usr/local/libevent &&make && make install

然后安装memcached

tar xzf memcache-2.2.5.tar.gz && cd memcache-2.2.5 && /usr/local/php5/bin/phpize && ./configure –prefix=/usr/local/memcached –with-libevent=/usr/local/libevent –with-php-config=/usr/local/php5/bin/php-config &&make &&make install

安装完后,会在/usr/local/php5/lib/php/extensions/no-debug-non-zts/里生成一个memcache.so 这个模块:

八、接下来修改php.ini

默认的php.ini在/usr/local/php5/lib/php.ini 你也可以指定:

extension_dir = “./”

修改为

extension_dir=”/usr/local/php5/lib/php/extensions/no-debug-non-zts〃

把下面这些添加到最后:

extension = apc.so

extension=memcache.so //这里引用缓存模块

apc.enabled = 1

apc.shm_segments = 1

apc.shm_size = 64M

apc.optimization = 1

apc.num_files_hint = 0

apc.ttl=7200

apc.user_ttl=7200

apc.gc_ttl = 3600

apc.cache_by_default = on

安装到此已经完成!

重新启动nginx和php-fpm ,用测试页面访问。

此文章仅供参考!有不妥之处欢迎指正!共同学习!

本博客采用的nginx+fastcGI+mysql+memcached+apc完美架构整合!

感谢张宴老师

关于使用nginx搭建文件服务器的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » Nginx带你轻松搭建高效文件服务器 (使用nginx搭建文件服务器)