Nginx优化与防盗链
Nginx 是俄罗斯人编写的十分轻量级的 HTTP 服务器,Nginx, 它的发音为"engine X", 是一个高
性能的 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 等依赖包
[root@bogon ~]# [root@bogon ~]# tar zxf nginx-1.14.0.tar.gz [root@bogon ~]# cd nginx-1.14.0/[root@bogon 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=nginxmake && make install
3.创建所需的用户
[root@bogon nginx-1.14.0]# useradd nginx -s /sbin/nologin -M
4.链接命令目录
[root@bogon nginx-1.14.0]#ln -s /usr/local/nginx1.14/sbin/nginx /usr/local/sbin/
5.开启nginx,查看端口
nginx
[root@bogon 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-ztsmake && make intall
提供 php 配置文件
[root@bogon 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[root@bogon etc]# netstat -anpt | grep 9000tcp 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.conffs.file-max = 999999net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296net.ipv4.tcp_max_tw_buckets = 6000net.ipv4.tcp_sack = 1net.ipv4.tcp_window_scaling = 1net.ipv4.tcp_rmem = 10240 87380 12582912net.ipv4.tcp_wmem = 10240 87380 12582912net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.core.netdev_max_backlog = 262144net.core.somaxconn = 40960net.ipv4.tcp_max_orphans = 3276800net.ipv4.tcp_max_syn_backlog = 262144net.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_fin_timeout = 1net.ipv4.tcp_keepalive_time = 30net.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:2keys_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/javascriptapplication/x-javascript application/xml;gzip_vary on;gzip_proxied anylocation ~* ^.+\.(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 root@'192.168.1.%' identified by '123.com';
测试 浏览器输入 http://自己的IP地址/bbs/install