nginx做静态文件下载服务器之安装与配置(含vsftp安装使用)

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

服务器软件nginx 1.8.0
操作系统:Centos 7 64bit
————————————————————-
一、准备篇
1、先新建screen,以免编译中断
screen

2、添加用户组
/usr/sbin/groupadd -f www;/usr/sbin/useradd -g www www
查看所有用户组和用户命令
cat /etc/group
文件包含所有组
cat /etc/shadow或者cat /etc/passwd
系统存在的所有用户名

3、更新系统,安装必要的编译工具、依赖包、工具
yum -y update
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel zip unzip net-snmp snmp-mibs-utils vsftp

二、nginx及模块安装篇
1、下载nginx第三方防盗链模块Nginx-accesskey、nginx服务器并解压
mkdir /DLserver;cd /DLserver
wget http://wiki.nginx.org/images/5/51/Nginx-accesskey-2.0.3.tar.gz;tar -xzvf Nginx-accesskey-2.0.3.tar.gz;rm -f Nginx-accesskey-2.0.3.tar.gz
编辑nginx-accesskey的配置文件config
vi nginx-accesskey-2.0.3/config
vim切换成insert模式,将$HTTP_ACCESSKEY_MODULE修改为ngx_http_accesskey_module
保存并退出vim编辑
:wq

2、安装Nginx-limit-traffic-rate-module
wget https://github.com/bigplum/Nginx-limit-traffic-rate-module/archive/master.zip;unzip master.zip;rm -f master.zip
第三方限速模块nginx Nginx-limit-traffic-rate-module
项目地址:https://github.com/bigplum/Nginx-limit-traffic-rate-module
该模块是按来路IP限制速度,而不是按连接限制,以下配置暂时按后者方式设置,该方式有弊端即同一IP有多人连接时(例如共用一个共享出口ip),速度被限制(例如我开的限制连接数为每个ip 1个连接,这不科学。。。。,但如果把连接数调大,又无法限制迅雷等多线下载器,故而有必要启用按)
按连接来控制下载速度的模块nginx默认已经安装
ngx_http_limit_conn_module
ngx_http_limit_req_module

若作为专门的图片服务器还可以使用ngx_http_image_filter_module限制图片大小
若需要开启http上传功能,需要使用Nginx upload module 官方文档: http://www.grid.net.ru/nginx/upload.en.html
2015-8-25备注:官方的版本编译出错,使用github上的

3、安装Nginx upload module 上传模块
wget https://github.com/vkholodkov/nginx-upload-module/archive/2.2.zip;unzip 2.2.zip;rm -f 2.2.zip
配置参考http://www.ttlsa.com/nginx/nginx-modules-upload-module/
安装方法:
添加以下参数然后编译
--add-module=/DLserver/nginx-upload-module-2.2 --with-http_secure_link_module
(2015-8-21:该模块编译失败,暂时未处理)

4、编译安装nginx
下载nginx服务器并解压进入目录
wget http://nginx.org/download/nginx-1.8.0.tar.gz;tar -xzvf nginx-1.8.0.tar.gz;rm -f nginx-1.8.0.tar.gz;cd nginx-1.8.0

由于是作为纯静态文件的下载服务器,所以可以在编译时去掉不必要的模块
编译参数如下:
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --user=www --group=www --without-http_fastcgi_module --without-http_autoindex_module --without-http_ssi_module --without-http_memcached_module --without-http_scgi_module --without-http_uwsgi_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --without-http_memcached_module --with-http_stub_status_module --with-http_realip_module --with-threads --add-module=/DLserver/nginx-accesskey-2.0.3 --add-module=/DLserver/Nginx-limit-traffic-rate-module-master --add-module=/DLserver/nginx_upload_module-2.2.0 --with-http_secure_link_module

注意当中的:
–with-http_realip_module
若需要反向代理时,获取后端获取真实ip用的
–with-http_stub_status_module
若要使用监控宝等的nginx监控,需要开启stub_status模块
——————————————————-
若自己编译单独安装snmp时应该添加以下参数
–with-mib-modules=ucd-snmp/diskio
选项,可以让服务器支持磁盘I/O监控。
–enable-mfd-rewrites
选项,允许用新的MFD重写mid模块,这样编译的snmp就能支持64位的计数器,能正常采集到流量。

5、编译安装
make && make install

6、相关目录与文件位置
nginx安装目录
/usr/local/nginx
nginx配置文件
/usr/local/nginx/conf/nginx.conf
日志文件
/usr/local/nginx/logs
7、相关命令
(1)查看编译参数:
/usr/local/nginx/sbin/nginx -V

(2)nginx管理命令,首先进入nginx安装目录
cd /usr/local/nginx/sbin
启动命令:
./nginx
停止命令:
./nginx -s stop
重新加载配置:
./nginx -s reload
或者直接使用
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s reload

(3)查询进程pid
ps -ef | grep nginx

8、nginx配置修改(修改后reload平滑重启)
修改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 {
use epoll;
worker_connections 2048;
}

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;
tcp_nodelay on;

#keepalive_timeout 0;
keepalive_timeout 65;

#以下为启用限速相关模块分别为,限制连接数(在location中配置,此处为基本设置),限制请求数为每个ip 1次/s,限制每个ip的下载速度(未启用)
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
#limit_traffic_rate_zone rate $remote_addr 16m;

#gzip on;

server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;

location / {
root /这里填写刚才配置ftp时新建用户xxx的文件放置目录;
index index.html index.htm;

#internal;

accesskey on;
accesskey_hashmethod md5;
accesskey_arg "key";
accesskey_signature "mypass$remote_addr";

#限制每个ip连接数,超出请求数限制的最大请求数,按ip限速(未启用,未实验与连接数限制如何配合,待研究)
limit_conn addr 1;
limit_req zone=one burst=5;
#limit_traffic_rate rate 128k;

#并限制每个连接的下载达到500K后(未启用)限制速度为128K/s
#limit_rate_after 500k;
limit_rate 128k;

sendfile_max_chunk 1024k;
aio threads;
directio 5120k;

}

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

}

其中服务器根据自己的情况调整,一般一个cpu一个worker_processes
自行调整服务器工作端口,默认为80,如果做为下载服务器,建议指定其他端口,并且在iptable中将80等web服务器需要的端口都关闭。
accesskey_signature “mypass$remote_addr”;中的mypass可任意设置(自己的应用与之相匹配即可)
以下参数,个人根据需求自行调优
#limit_rate_after 500k;
limit_rate 128k;
sendfile_max_chunk 256k;
aio threads;
directio 1024k;
output_buffers 1 128k;

mark:在修改nginx.conf配置启用线程池aio threads后提示错误,查看官方文档发现需要在编译nginx的时候加入–with-threads,可能许多同学都忽略了这一步。
9、nginx日志文件access.log按天切割(自动删除待后续添加)
参照nginx按天切割access.log日志存放

10、配置nginx开启启动
cd /usr/local/nginx/sbin
./nginx
echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local

安全重启linux系统(立刻重启(root用户使用))
shutdown -r now
扩展:
shutdown -r 10 过10分钟自动重启(root用户使用)
shutdown -r 20:35 在时间为20:35时候重启(root用户使用)

重启后,查看当前所有运行的服务,看看nginx开机自动启动是否成功
ps auxw
shutdown -r now
———————————————————————————————————–
三、服务器监控方案
1、snmp篇
yum -y install net-snmp net-snmp-devel net-snmp-utils
net-snmp snmp-mibs-utils是监控宝等snmp所需
清空并编辑snmp配置
> /etc/snmp/snmpd.conf;vi /etc/snmp/snmpd.conf
添加以下内容,其中public为密匙,修改为自己的
rocommunity public 127.0.0.1
rocommunity public 被监控服务器ip
rocommunity public 192.168.1.2
rocommunity public 60.195.252.107
rocommunity public 60.195.252.110

####若采用360监控,配置如下(若两边都用,也可以把配置都添加进去,随时切换到另一家监控)
rocommunity public default
rocommunity public 101.199.100.150
rocommunity public 220.181.150.98
rocommunity public 180.153.229.230
rocommunity public 220.181.150.125
rocommunity public 103.28.11.237
rocommunity public 103.28.10.244
rocommunity public 103.28.10.245

cp /etc/snmp/snmpd.conf /usr/share/snmp/snmpd.conf

snmp控制管理命令
service snmpd {start|stop|status|restart|condrestart|reload|force-reload}
若无法管理,先杀死已经运行snmpd进程
查看snmpd进程
ps aux | grep snmpd
杀死进程
kill id号
查看snmp服务是否运行
service snmpd status

2、OneApm篇
由oneapm.com开发的监控服务非常不错,特别适合商业用户,功能非常强悍,免费用户有功能限制,不是使用snmp,需要安装他们自己开发的程序

超级牛逼的功能:
(1)应用监控:支持php ruby js py等等各种程序的应用
例如:oneapm.com可以监控你的php执行详细情况,可以很清楚的开到哪个php文件执行慢、执行了多少秒、进行了哪些操作(最关键的就是这个,可以找到是php执行什么操作导致该文件执行速度慢,对于优化非常有帮助)、进行了哪些数据库操作
(2)数据库监控:当然支持主流的几个数据库啦
例如:管理界面可以清楚看到数据库什么操作最慢,来源于那个程序文件执行的这个操作。。。。
具体安装及使用详见oneapm.com

唯一缺点:免费版数据只能保存3天,而且功能上有限制,例如应用监控等这些高级功能付费用户才能用,而且不便宜,一个月接近1K,对个人站长来说有点高了,不过好像在管理首页可以看到摘要,看不到详细分析信息
目前新注册可以免费试用高级功能15天,用于网站测试分析够了
—————————————————————————————
四、FTP安装篇
1、安装vsftpd(FTP用于文件批量上传)
yum -y install vsftpd
2、测试是否安装成功
service vsftpd start
3、修改vsftpd配置文件(配置为指定用户可以访问,匿名不能访问)
vsftpd的配置文件有三个,分别是
/etc/vsftpd/vsftpd.conf
/etc/vsftpd/ftpusers
/etc/vsftpd/user_list

其中
/etc/vsftpd/vsftpd.conf是主配置文件
/etc/vsftpd/ftpusers配置哪些用户不能访问FTP服务器,应该将root用户加入
/etc/vsftpd/user_list该文件里的用户账户在默认情况下也不能访问FTP服务器,仅当vsftpd .conf配置文件里设置userlist_enable=NO选项时才允许该名单中的用户访问FTP。

通过搭配/etc/vsftpd/ftpusers 和 /etc/vsftpd/user_list可以限制指定用户可以访问。

首先编辑
vi /etc/vsftpd/vsftpd.conf
去掉以下注释符并设置:
local_enable=YES#允许本机使用者可以登入
write_enable=YES
#以下三条是禁止匿名使用者登入上传修改文件
anonymous_enable=NO
anon_upload_enable=NO
anon_mkdir_write_enable=NO
chown_uploads=NO
xferlog_file=/var/log/vsftpd.log
xferlog_file=/var/log/xferlog
chroot_list_enable=NO
chroot_list_file=/etc/vsftpd/chroot_list
userlist_enable=NO

保存退出
关于vsftpd.conf的其他提示
/var/ftp —- 匿名用户主目录
/var/ftp/pub —- 匿名用户的下载目录
如果要更改默认下载目录,修改/etc/vsftpd/vsftpd.conf,加入如下三行:
local_root=/
chroot_local_user=YES
anon_root=/
local_root表示使用本地用户登录到ftp时的默认目录
anon_root表示匿名用户登录到ftp时的默认目录
你上面的chroot_list_file是设定锁定登陆用户在其home目录的列表,要在chroot_list_enable=YES情况下才生效。
另外,最好不要设置默认目录为/,使用建议使用mount –bind来挂载需要的目录。

创建新用户
安装ftp时实际已经创建了名为ftp的用户,需要使用passwd修改密码后,添加到userlist中区
若使用root不用新建,不过不推荐这种危险方式

新建用户按一下步骤:
这里以用户名xxx,目录/home/xxxfile为例(xxxfile即为我们做下载服务器保存的目录)
adduser -d /home/xxxfile -g ftp -s /sbin/nologin xxx
设置密码
passwd xxx
输入密码即可

再次编辑
vi /etc/vsftpd/user_list
加入刚才新建的用户xxx
保存退出

vi /etc/vsftpd/chroot_list
输入给xxx用户指定的文件目录/home/xxxfile
:wq保存退出
重启vsftpd

4、控制命令(修改配置后应重启服务)
/etc/init.d/vsftpd start
/etc/init.d/vsftpd stop
/etc/init.d/vsftpd restart

将vsftpd加入开机运行
chkconfig vsftpd on
关闭开机运行
chkconfig vsftpd off

5、添加ftp防火墙规则(参照博客内关于iptable的文章):
FTP服务器iptable规则
iptables -A INPUT -p tcp --dport 21 -j ACCEPT;iptables -A INPUT -p tcp --dport 20 -j ACCEPT;iptables -A INPUT -p tcp --dport 45465:57656 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart

保存后重启服务
service iptables restart
查看端口是否正常启动
netstat -an | grep 21
若vsftp无法正常使用“227 Entering Passive Mode。。。” 在配置文件中加入pasv_enable=NO
若vsftp提示530错误则:
1、查看/etc/ftpusers ,确保账号没有在这个文件内。
2、修改/etc/pam.d/vsftpd
将auth required pam_shells.so修改为->auth required pam_nologin.so 即可
3、重启vsftpd
————————————————————————————————————

五、绑定域名至服务器,禁止IP或非绑定域名访问
参照禁止通过IP或未绑定的域名访问Nginx服务器

六、关闭不必要的端口,修改iptable规则
参照
Centos下lnmp正确iptables配置规则
Linux iptables 配置详解

注意别忘了添加以上每一个步骤中ftp、snmp监控、nginx负载均衡及反向代理所需要的端口

—————————————————
六、linux centos通过ftp上传的中文文件名乱码
此问题不影响下载,主要因为win系统默认编码gb2132,而centos服务器设置为utf-8,
解决该问题方法参照
linux系统(本例为Centos)ftp上传中文名文件乱码或者显示问号解决方法
—————————————————
七、服务器安全策略
1、使用尽量复杂的密码
2、修改ssh端口(老牛第一天忙的事太多,没来得及修改,默认的22端口一直有人暴力破解,几十个ip。。。最多的一个尝试了4k多次,查了下大部分都是国内IP或者香港IP,国人这素质。。。这心态。。。你有本事去黑Google 黑微软啊 黑国安啊;所以大家的独服或者vps上线后一定马上修改SSH端口)
(必要情况下采用密匙登陆,禁止root登陆)
3、启用iptable,修改iptable规则
PS:关于centos7–CentOS7用firewall命令“替代”了iptables。在这里我们需要区分“iptables服务”和“iptables命令”。虽然firewalld是替代提供的防火墙管理iptables服务,但是它仍然使用iptables对内核命令动态通信包过滤。所以它只是iptables服务代替,而不是iptables命令。
如果想切换centos 7的firewall为iptable,依次执行以下命令,注意如果不用ip6可以不用添加ip6的
systemctl stop firewalld
systemctl disable firewalld

yum install iptables-services

touch /etc/sysconfig/iptables
systemctl start iptables
systemctl enable iptables

touch /etc/sysconfig/ip6tables
systemctl start ip6tables
systemctl enable ip6table

使用service iptables status看看状态吧

(1)以下为snmp的规则
监控宝snmp iptable规则

iptables -I INPUT -p udp -s 60.195.252.107 --dport 161 -j ACCEPT;iptables -I INPUT -p udp -s 60.195.252.110 --dport 161 -j ACCEPT;iptables -I INPUT -p udp -s 127.0.0.1 --dport 161 -j ACCEPT;iptables -I INPUT -p udp -s 45.63.121.42 --dport 161 -j ACCEPT;iptables -I INPUT -p udp -s 192.168.1.2 --dport 161 -j ACCEPT

###若采用360监控,添加规则如下:
iptables -A INPUT -i eth0 -p udp -s 101.199.100.150 --dport 161 -j ACCEPT;iptables -A INPUT -i eth0 -p udp -s 220.181.150.98 --dport 161 -j ACCEPT;iptables -A INPUT -i eth0 -p udp -s 180.153.229.230 --dport 161 -j ACCEPT;iptables -A INPUT -i eth0 -p udp -s 220.181.150.125 --dport 161 -j ACCEPT;iptables -A INPUT -i eth0 -p udp -s 103.28.11.237 --dport 161 -j ACCEPT;iptables -A INPUT -i eth0 -p udp -s 103.28.10.244 --dport 161 -j ACCEPT;iptables -A INPUT -i eth0 -p udp -s 103.28.10.245 --dport 161 -j ACCEPT

(2)FTP服务器iptable规则
iptables -A INPUT -p tcp --dport 15313 -j ACCEPT;iptables -A INPUT -p tcp --dport 13888:15899 -j ACCEPT

(3)以下为常规端口设置(注意修改SSH等端口为你想要的)
iptables -A INPUT -i lo -j ACCEPT;iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT;iptables -A OUTPUT -j ACCEPT;iptables -A INPUT -p tcp --dport 22 -j ACCEPT;iptables -A INPUT -p tcp --dport 80 -j ACCEPT;iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT;iptables -A INPUT -j REJECT;iptables -A FORWARD -j REJECT

chkconfig --level 345 iptables on
service iptables save

—————————————————
八、nginx静态文件服务器使用中出现的问题及解决方法
2015-8-17
问题:重新加载nginx.conf后重启nginx报错
nginx: [error] open() "/usr/local/nginx/nginx.pid" failed (2: No such file or directory)
解决方法,输入:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

出现以下提示的处理方法
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
先查看端口占用情况
netstat -ntpl
查找到nginx的进程ID后kill掉
kill id号
然后重新按开头来

—————————————————
2015-8-23
问题:重启nginx时 提示 no such file

—————————————————
2015-8-23
问题:
如何禁止使用多线程下载工具多线程下载?
以下配置方法见官方文档
启用nginx的限制并发数模块 ngx_http_limit_zone_module (生产服务器已启用)
ngx_http_limit_conn_module 模块虽说可以解决当前面临的并发问题,但是会引入另外一些问题的。如前端如果有做LVS或反代,而我们后端启用了该模块功能,那不是非常多503错误了?这样的话,可以在前端启用该模块,要么就是设置白名单
补充:查阅“运维生存时间”上相关文章,知晓该模块只是限制连接数,结合limit_rate控制每个连接速度,但是并不是基于ip或者url控制下载速度,意思是同一个ip来的多个
—————————
2015-8-23
如何限制请求数,设置每个ip请求频率
启用限制请求数模块 ngx_http_limit_req_module (生产服务器暂时未启用,Mark一下)
可能要对某些IP不做限制,需要使用到白名单

—————————
2015-8-23
若要启用上传功能
可开启nginx第三方模块Limit Upload Rate限制上传速度(暂未使用,mark一下)

—————————
2015-8-23
安装snmp 添加360监控

Leave a Comment