Nginx优化与防盗链
admin
2023-03-29 21:01:38
0

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=nginx

make  &&  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
Nginx优化与防盗链
7.修改/etc/security/limits.conf
Nginx优化与防盗链
8.查看现在是否没变
Nginx优化与防盗链
9.注销一下,重新查看一下,是否变化
Nginx优化与防盗链
10.开启nginx,查看nginx变化
Nginx优化与防盗链

Nginx优化与防盗链
Nginx优化与防盗链
11.页面访问一下nginx
Nginx优化与防盗链

二、部署 LNMP (部署环境需求:nginx和php在一台,再开启一台apache做防盗链)
1.安装php
首先安装libmcrypt 和依赖包
Nginx优化与防盗链
Nginx优化与防盗链

编译安装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 配置文件
[root@bogon php-5.6.27]# cp php.ini-production /etc/php.ini

为 php-fpm 提供脚本
Nginx优化与防盗链

提供 php-fpm 配置文件并编辑
Nginx优化与防盗链
修改/usr/local/php5.6/etc/php-fpm.conf

启动php 查看9000端口

/etc/init.d/php-fpm  start
[root@bogon 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
Nginx优化与防盗链
Nginx优化与防盗链

创建/usr/local/nginx1.14/html/index.php
Nginx优化与防盗链
Nginx优化与防盗链

测试
Nginx优化与防盗链

内核参数优化 添加 /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 使内核修改生效

Nginx优化与防盗链
查看更新值
Nginx优化与防盗链

修改/usr/local/nginx1.14/conf/nginx.conf
Nginx优化与防盗链

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测试页注释掉

Nginx优化与防盗链

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

测试

Nginx优化与防盗链

三、验证、压力测试
查看ab的安装包
yum provides ab

安装httpd-tools
yum -y install httpd-tools

压力测试
ab -c 2000 -n 10000 192.168.1.50/index.html
Nginx优化与防盗链

ab -c 2000 -n 10000 192.168.1.50/index.php
Nginx优化与防盗链
两次压力测试做对比

验证防盗链
使用 apache 做为一个测试站点,域名为 www.test.com,在测试页上做一个超链接,链接 nginx

在apache主机上创建vim /var/www/html/index.html
Nginx优化与防盗链
开启httpd
systemctl start httpd

测试

Nginx优化与防盗链
Nginx优化与防盗链

论坛
Php服务器解压 压缩包
unzip Discuz_7.0.0_FULL_SC_UTF8.zip
cd Discuz_7.0.0_FULL_SC_UTF8/
ls
ls upload/
Nginx优化与防盗链
修改/etc/php.ini
Nginx优化与防盗链

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
Nginx优化与防盗链

相关内容

热门资讯

【今日要闻】“小猪对对碰开挂器... 您好:小猪对对碰这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款游...
玩家分享攻略“新老夫子牛牛究竟... 玩家分享攻略“新老夫子牛牛究竟有挂吗?”(透视曝光猫腻)您好,新老夫子牛牛这个游戏其实有挂的,确实是...
终于了解“新九五真的有挂吗?”... 家人们!今天小编来为大家解答新九五透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买很多...
终于懂了“江西南昌麻将究竟有挂... 有 亲,根据资深记者爆料江西南昌麻将是可以开挂的,确实有挂(咨询软件无需...
浙江:为符合条件的青年创客提供... 财联社12月26日电,浙江省文化广电和旅游厅近日印发《浙江省青创文旅发展实施方案》,充分激发广大青年...
台当局拟限制民代、公务员赴陆,... 海峡导报综合报道 台行政机构今日(26日)通过“两岸人民关系条例”修正草案,民代赴陆须经过许可,所有...
今日重磅消息“皇豪互娱牛牛开挂... 网上科普关于“皇豪互娱牛牛有没有挂”话题很是火热,小编也是针对皇豪互娱牛牛作*弊开挂的方法以及开挂对...
时隔近10年,故宫养心殿再度开... 编辑 | ‍‍高语阳新华社报道,经过近10年的研究性保护和系统性修缮,故宫博物院养心殿于12月26日...
【第一资讯】“人海炸/金/花是... 您好:人海炸/金/花这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这...
今日重大消息“潘潘讲故事怎么装... 您好:潘潘讲故事这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款游...