兰色精灵 发表于 2015-1-14 20:30:09

带来一篇20步打造最宁静的Nginx Web办事器

欢迎大家来到仓酷云论坛!Nginx是一个轻量级的,高功能的Web办事器和反向代办署理和邮箱(IMAP/POP3)代办署理办事器。它运转在UNIX,GNU/Linux,BSD各类版本,MacOSX,Solaris和Windows。依据查询拜访统计,6%的网站利用NginxWeb办事器。Nginx是多数能处置C10K成绩的办事器之一。跟传统的办事器分歧,Nginx不依附线程来处置哀求。相反,它利用了更多的可扩大的事务驱动(异步)架构。Nginx为一些高流量的网站供应动力,好比WordPress,大家网,腾讯,网易等。这篇文章次要是先容怎样进步运转在Linux或UNIX体系的NginxWeb办事器的宁静性。

默许设置文件和Nginx端口


[*]/usr/local/nginx/conf/-Nginx设置文件目次,/usr/local/nginx/conf/nginx.conf是主设置文件
[*]/usr/local/nginx/html/-默许网站文件地位
[*]/usr/local/nginx/logs/-默许日记文件地位
[*]NginxHTTP默许端口:TCP80
[*]NginxHTTPS默许端口:TCP443
你可使用以下下令来测试Nginx设置文件正确性。
1
/usr/local/nginx/sbin/nginx-t



将会输入。
theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisok
configurationfile/usr/local/nginx/conf/nginx.conftestissuccessful
实行以下下令来从头加载设置文件。
1
/usr/local/nginx/sbin/nginx-sreload



实行以下下令来中断办事器。
1
/usr/local/nginx/sbin/nginx-sstop



1、设置SELinux

宁静加强型Linux(SELinux)的是一个Linux内核的功效,它供应撑持会见把持的宁静政策回护机制。它能够年夜局部的打击。上面我们来看怎样启动基于CentOS/RHEL体系的SELinux。
装置SELinux
1
rpm-qa|grepselinux



libselinux-1.23.10-2
selinux-policy-targeted-1.23.16-6
假如没有前往任何了局,代表没有装置SELinux,假如前往了相似下面的了局,则申明体系装置了SELinux。
布什值锁定
运转下令getsebool-a来锁定体系。
1
2
3
getsebool-a|less
getsebool-a|grepoff
getsebool-a|grepo



2、经由过程分区挂载同意起码特权

办事器上的网页/html/php文件独自分区。比方,新建一个分区/dev/sda5(第一逻辑分区),而且挂载在/nginx。确保/nginx是以noexec,nodevandnosetuid的权限挂载。以下是我的/etc/fstab的挂载/nginx的信息:
LABEL=/nginx/nginxext3defaults,nosuid,noexec,nodev12
注重:你必要利用fdisk和mkfs.ext3下令创立一个新分区。
3、设置/etc/sysctl.conf强化Linux宁静

你能够经由过程编纂/etc/sysctl.conf来把持和设置Linux内核、收集设置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#Avoidasmurfattack
net.ipv4.icmp_echo_ignore_broadcasts=1

#Turnonprotectionforbadicmperrormessages
net.ipv4.icmp_ignore_bogus_error_responses=1

#TurnonsyncookiesforSYNfloodattackprotection
net.ipv4.tcp_syncookies=1

#Turnonandlogspoofed,sourcerouted,andredirectpackets
net.ipv4.conf.all.log_martians=1
net.ipv4.conf.default.log_martians=1

#Nosourceroutedpacketshere
net.ipv4.conf.all.accept_source_route=0
net.ipv4.conf.default.accept_source_route=0

#Turnonreversepathfiltering
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.default.rp_filter=1

#Makesurenoonecanaltertheroutingtables
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.conf.all.secure_redirects=0
net.ipv4.conf.default.secure_redirects=0

#Dontactasarouter
net.ipv4.ip_forward=0
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.send_redirects=0

#Turnonexecshild
kernel.exec-shield=1
kernel.randomize_va_space=1

#TuenIPv6
net.ipv6.conf.default.router_solicitations=0
net.ipv6.conf.default.accept_ra_rtr_pref=0
net.ipv6.conf.default.accept_ra_pinfo=0
net.ipv6.conf.default.accept_ra_defrtr=0
net.ipv6.conf.default.autoconf=0
net.ipv6.conf.default.dad_transmits=0
net.ipv6.conf.default.max_addresses=1

#OptimizationforportuseforLBs
#Increasesystemfiledescriptorlimit
fs.file-max=65535

#AllowformorePIDs(toreducerolloverproblems);maybreaksomeprograms32768
kernel.pid_max=65536

#IncreasesystemIPportlimits
net.ipv4.ip_local_port_range=200065000

#IncreaseTCPmaxbuffersizesetableusingsetsockopt()
net.ipv4.tcp_rmem=4096873808388608
net.ipv4.tcp_wmem=4096873808388608

#IncreaseLinuxautotuningTCPbufferlimits
#min,default,andmaxnumberofbytestouse
#setmaxtoatleast4MB,orhigherifyouuseveryhighBDPpaths
#TcpWindowsetc
net.core.rmem_max=8388608
net.core.wmem_max=8388608
net.core.netdev_max_backlog=5000
net.ipv4.tcp_window_scaling=1



4、删除一切不必要的Nginx模块

你必要间接经由过程编译Nginx源代码使模块数目起码化。经由过程限定只同意web办事器会见模块把风险降到最低。你能够只设置装置nginx你所必要的模块。比方,禁用SSL和autoindex模块你能够实行以下下令:
1
2
3
./configure--without-http_autoindex_module--without-http_ssi_module
make
makeinstall



经由过程以下下令来检察当编译nginx办事器时哪一个模块能开户或封闭:
1
./configure--help|less



禁用你用不到的nginx模块。
(可选项)变动nginx版本称号。
编纂文件/http/ngx_http_header_filter_module.c:
1
vi+48src/http/ngx_http_header_filter_module.c



找到行:
1
2
staticcharngx_http_server_string[]="Server:nginx"CRLF;
staticcharngx_http_server_full_string[]="Server:"NGINX_VERCRLF;



依照以下行修正:
1
2
staticcharngx_http_server_string[]="Server:NinjaWebServer"CRLF;
staticcharngx_http_server_full_string[]="Server:NinjaWebServer"CRLF;



保留并封闭文件。如今你能够编纂办事器了。增添以下代码到nginx.conf文件来封闭nginx版本号的显现。
1
server_tokensoff



5、利用mod_security(只合适后端Apache办事器)

mod_security为Apache供应一个使用步伐级的防火墙。为后端ApacheWeb办事器装置mod_security,这会制止良多注进式打击。
6、装置SELinux战略以强化NginxWeb办事器

默许的SELinux不会回护NginxWeb办事器,可是你能够装置和编译回护软件。
1、装置编译SELinux所需情况撑持
1
yum-yinstallselinux-policy-targetedselinux-policy-devel



2、下载SELinux战略以强化NginxWeb办事器。
1
2
cd/opt
wgethttp://downloads.sourceforge.net/project/selinuxnginx/se-ngix_1_0_10.tar.gz?use_mirror=nchc



3、解压文件
1
tar-zxvfse-ngix_1_0_10.tar.gz



4、编译文件
1
2
cdse-ngix_1_0_10/nginx
make



将会输入以下:
Compilingtargetednginxmodule
/usr/bin/checkmodule:loadingpolicyconfigurationfromtmp/nginx.tmp
/usr/bin/checkmodule:policyconfigurationloaded
/usr/bin/checkmodule:writingbinaryrepresentation(version6)totmp/nginx.mod
Creatingtargetednginx.pppolicypackage
1
rmtmp/nginx.mod.fctmp/nginx.mod



5、装置天生的nginx.ppSELinux模块:
1
/usr/sbin/semodule-inginx.pp



7、基于Iptables防火墙的限定

上面的防火墙剧本制止任何除同意:


[*]来自HTTP(TCP端口80)的哀求
[*]来自ICMPping的哀求
[*]ntp(端口123)的哀求输入
[*]smtp(TCP端口25)的哀求输入
<divstyle="background-color:#FFFFFF;border:none;margin:0px;padding:0px;font-family:微软雅黑;color:#222222;font-size:13px;"><divid="highlighter_549270"class="syntaxhighlighterbash"style="border:none;padding:0px;margin:1em0px!important;font-size:1em!important;">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

飘灵儿 发表于 2015-1-16 18:55:10

带来一篇20步打造最宁静的Nginx Web办事器

不同于Windows?系统需要花钱购买,因为Linux的核心是免费的,自由使用的,核心源代码是开放的。

因胸联盟 发表于 2015-1-28 13:45:38

Linux?最大的优点在于其作为服务器的强大功能,同时支持多种应用程序及开发工具,所以Linux操作系统有着广泛的应用空间。

柔情似水 发表于 2015-2-5 22:42:47

下面看看一个让人无法回答的问题:“救命各位高手,向你们请教一些问题:如何在Linux下配制HTTP、FTP、Samba、DNS、DHCP、Sendmail服务器,谢谢”这样的问题。

简单生活 发表于 2015-2-14 00:42:06

就这样,我们一边上OS理论课,一边上这个实验,这样挺互补的,老师讲课,一步一步地布置任务

若相依 发表于 2015-3-4 03:57:42

为什么要学Linux呢?每个人都有不同的看法,下面我说说自己的感想吧。?

海妖 发表于 2015-3-11 16:12:46

对Linux命令熟悉后,你可以开始搭建一个小的Linux网络,这是最好的实践方法。Linux是网络的代名词,Linux网络服务功能非常强大,不论是邮件服务器、Web服务器、DNS服务器等都非常完善。

精灵巫婆 发表于 2015-3-19 06:24:59

任何人都可以根据自己的喜好来定制适合自己的操作系统,Linux?是抢占式多任务多用户操作系统.

莫相离 发表于 2015-3-27 11:09:54

另外Linux上也有很多的应用软件,安装运行了这些软件后,你就可以在Linux上编辑文档、图?片,玩游戏、上网、播放多媒体文件等。
页: [1]
查看完整版本: 带来一篇20步打造最宁静的Nginx Web办事器