Nginx优化与防盗链
性能的 HTTP 和反向代理服务器,同时也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由
俄罗斯人 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发.
Nginx 以事件驱动(epoll) 的方式编写,所以有非常好的性能,同时也是一个非常高效的反
向代理、负载平衡。 但是 Nginx 并不支持 cgi 方式运行,原因是可以减少因此带来的一些程
序上的漏洞。所以必须使用 FastCGI 方式来执行 PHP 程序。
由于 Nginx 本身的一些优点, 轻量,开源,易用,越来越多的公司使用 nginx 作为自己公司
的 web 应用服务器,本文详细介绍 nginx 源码安装的同时并对 nginx 进行优化配置。
一、 Nginx 的优化
1、 编译安装前优化
编译前的优化主要是用来修改程序名等等, 目的更改源码隐藏软件名称和版本号
安装 zlib-devel、 pcre-devel 等依赖包
[ ~]#
[ ~]# tar zxf nginx-1.14.0.tar.gz
[ ~]# cd nginx-1.14.0/
[ nginx-1.14.0]# yum -y install openssl-devel pcre-devel
2.编译安装nginx
./configure --prefix=/usr/local/nginx1.14 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx
make && make install
3.创建所需的用户
[ nginx-1.14.0]# useradd nginx -s /sbin/nologin -M
4.链接命令目录
[ nginx-1.14.0]#ln -s /usr/local/nginx1.14/sbin/nginx /usr/local/sbin/
5.开启nginx,查看端口
nginx
[ nginx-1.14.0]# netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5671/nginx: master
6.修改/usr/local/nginx1.14/conf/nginx.conf
7.修改/etc/security/limits.conf
8.查看现在是否没变
9.注销一下,重新查看一下,是否变化
10.开启nginx,查看nginx变化
11.页面访问一下nginx
二、部署 LNMP (部署环境需求:nginx和php在一台,再开启一台apache做防盗链)
1.安装php
首先安装libmcrypt 和依赖包
编译安装php
./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
make && make intall
提供 php 配置文件
[ php-5.6.27]# cp php.ini-production /etc/php.ini
为 php-fpm 提供脚本
提供 php-fpm 配置文件并编辑
修改/usr/local/php5.6/etc/php-fpm.conf
启动php 查看9000端口
/etc/init.d/php-fpm start
[ etc]# netstat -anpt | grep 9000
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN
修改/usr/local/nginx1.14/conf/nginx.conf
创建/usr/local/nginx1.14/html/index.php
测试
内核参数优化 添加 /etc//sysctl.conf
fs.file-max = 999999
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 10240 87380 12582912
net.ipv4.tcp_wmem = 10240 87380 12582912
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 40960
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
执行 sysctl -p 使内核修改生效
查看更新值
修改/usr/local/nginx1.14/conf/nginx.conf
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;
server_tokens off;
client_max_body_size 10m;
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /usr/local/nginx1.10/nginx_tmp;
fastcgi_intercept_errors on;
fastcgi_cache_path /usr/local/nginx1.10/fastcgi_cache levels=1:2
keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;
gzip on;
gzip_min_length 2k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/json application/javascript
application/x-javascript application/xml;
gzip_vary on;
gzip_proxied any
location ~* ^.+.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
valid_referers none blocked 192.168.1.50;
if ($invalid_referer) {
#return 302 http://www.benet.com/img/nolink.jpg;
return 404;
break;
}
access_log off;
}
location / {
root html;
index index.php index.html index.htm;
}
location /status {
stub_status on;
}
把之前php测试页注释掉
location ~ .*.(php|php5)?$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache cache_fastcgi;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key http://$host$request_uri;
}
重启php
nginx -s reload
测试
三、验证、压力测试
查看ab的安装包
yum provides ab
安装httpd-tools
yum -y install httpd-tools
压力测试
ab -c 2000 -n 10000 192.168.1.50/index.html
ab -c 2000 -n 10000 192.168.1.50/index.php
两次压力测试做对比
验证防盗链
使用 apache 做为一个测试站点,域名为 www.test.com,在测试页上做一个超链接,链接 nginx
在apache主机上创建vim /var/www/html/index.html
开启httpd
systemctl start httpd
测试
论坛
Php服务器解压 压缩包
unzip Discuz_7.0.0_FULL_SC_UTF8.zip
cd Discuz_7.0.0_FULL_SC_UTF8/
ls
ls upload/
修改/etc/php.ini
Mysql服务器
进入MySQL
mysql -u root -p123
创建数据库
create database bbs;
grant all on . to ‘192.168.1.%‘ identified by ‘123.com‘;
测试