草庐IT

一步一步教你Nginx优化和防盗链

邂逅云边的落日 2023-03-28 原文

       Nginx优化和防盗链

一、在Centos01上安装Nginx,设置根目录/www/使用域名www.stz.con访问

1、在Centos01上安装Nginx依赖程序

1)挂载系统光盘配置本地yum仓库

[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos01 ~]# ls /etc/yum.repos.d/
local.repo
2)安装依赖程序

[root@centos01 ~]# yum -y install pcre-devel zlib-devel
3)创建管理Nginx用户

[root@centos01 ~]# useradd -M -s /sbin/nologin nginx
[root@centos01 ~]# umount /mnt/

2、配置安装Nginx服务

1)切换光盘

2)解压配置安装Nginx

[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# tar zxf /mnt/nginx-1.6.0.tar.gz -C /usr/src/
[root@centos01 ~]# cd /usr/src/nginx-1.6.0/
[root@centos01 nginx-1.6.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
3)编译安装

[root@centos01 nginx-1.6.0]# make && make install
4)优化nginx命令检查配置文件

[root@centos01 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos01 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf
syntax is ok
nginx: configuration file
/usr/local/nginx/conf/nginx.conf test is successful

3、设置Nginx服务器网站根目录设置主页修改配置文件

1)创建网站根目录设置主页

[root@centos01 ~]# mkdir /www
[root@centos01 ~]# echo "www.stz.com" > /www/index.html
2)备份主配置文件

[root@centos01 ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
3)修改Nginx主配置文件

[root@centos01 ~]# vim
/usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
}
4)启动Nginx服务监听端口

[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp
0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3758/nginx: master

二、在Centos02上安装DNS使用域名访问Centos01上使用域名www.stz.com访问,Centos03使用yum安装apache使用域名www.benet.con访问

1、安装DNS服务设置开机自动启动

1)挂载系统盘安装DNS服务

[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# ls /mnt/
CentOS_BuildTag images
repodata
EFI isolinux RPM-GPG-KEY-CentOS-7
EULA LiveOS RPM-GPG-KEY-CentOS-Testing-7
GPL Packages TRANS.TBL
[root@centos02 ~]#
2)安装DNS服务

[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-9.9.4-50.el7.x86_64.rpm
[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-chroot-9.9.4-50.el7.x86_64.rpm
3)设置服务开机自启

[root@centos02 ~]# systemctl start named
[root@centos02 ~]# systemctl enable named

2、修改主配置文件和区域配置文件

1)修改DNS主配置文件

[root@centos02 ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
directory "/var/named";
};
zone "stz.com" IN {
type master;
file "/var/named/stz.com.zone";
};
zone "benet.com" IN {
type master;
file "/var/named/benet.com.zone";
};
2)检查主配置文件是否错误

[root@centos02 ~]# named-checkconf /etc/named.conf
3)修改区域配置文件

[root@centos02 ~]# vim /var/named/stz.com.zone
$TTL 86400
@ SOA stz.com. root.stz.co
m. (
2022021910
1H
15M
1W
1D
)
@ NS centos02.stz.com.
centos02 A 192.168.100.20
www A 192.168.100.10
[root@centos02 ~]# vim /var/named/benet.com.zone
$TTL 86400@ SOA benet.com. root.benet.com. (
2022021910
1H
15M
1W
1D
)
@ NS centos02.benet.com.
centos02 A 192.168.100.20
www A 192.168.100.30
4)检查区域配置文件是否错误

[root@centos02 ~]# named-checkzone stz.com /var/named/stz.com.zone
zone stz.com/IN: loaded serial 2022021910
OK
[root@centos02 ~]# named-checkzone benet.com /var/named/benet.com.zone
zone benet.com/IN: loaded serial 2022021910
OK

3、启动DNS服务查看服务运行状态

1)启动DNS服务

[root@centos02 ~]# systemctl start named
[root@centos02 ~]# systemctl enable named
2)查看服务运行状态

[root@centos02 ~]# netstat -anptu | grep named
tcp
0 0 192.168.100.20:53 0.0.0.0:* LISTEN 2251/named

配置DNS在Centos01、Centos02、Centos03

[root@centos01 ~]# vin /etc/sysconfig/network-scripts/ifcfg-ens32
TYPE=Ethernet
BOOTPROTO=static
NAME=ens32
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
DNS1=192.168.100.20
[root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
TYPE=Ethernet
BOOTPROTO=static
NAME=ens32
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.100.20
NETMASK=255.255.255.0
DNS1=192.168.100.20
[root@centos03 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
TYPE=Ethernet
BOOTPROTO=static
NAME=ens32
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.100.30
NETMASK=255.255.255.0
DNS1=192.168.100.20
重启网卡

[root@centos01 ~]# systemctl restart network
[root@centos02 ~]# systemctl restart network
[root@centos03 ~]# systemctl restart network

4、在Centos03上使用yum安装apache设置网站主页客户端访问nginx和apache

1)挂载光盘配置本地yum仓库

[root@centos03 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos03 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos03 ~]# ls /etc/yum.repos.d/
local.repo
3)安装apache服务设置开机自动启动

[root@centos03 ~]# yum -y install httpd
[root@centos03 ~]# systemctl start httpd
[root@centos03 ~]# systemctl enable httpd
4)设置主页启动服务

[root@centos03 ~]# echo "www.benet.com" > /var/www/html/index.html
[root@centos03 ~]# systemctl restart httpd
5)查看apache服务运行状态

[root@centos03 ~]# netstat -anptu | grep httpd
tcp6
0 0 :::80 :::* LISTEN 2039/httpd

给客户端配置ip

6)客户端访问Nginx

7)客户端访问apache

三、在Nginx上配置 网站隐藏 Nginx 版本、查看 Nginx 管理用户、设置网页缓存、 配置日志切割、配置 Nginx 日志切割、设置超时时间

1、在Nginx网站服务配置将Nginx版本位置位IIS不显示版本号,查看Nginx进程用户

1)配置修改Nginx源代码位置版本信息‘

[root@centos01 ~]# vim/usr/src/nginx-1.6.0/src/core/nginx.h
13 #define NGINX_VERSION "7.0"
14 #define NGINX_VER "IIS/" NGINX_VERSION
15
16 #define NGINX_VAR "IIS"
[root@centos01 ~]# vim /usr/src/nginx-1.6.0/src/http/ngx_http_header_filter_module.c
49 static char ngx_http_server_string[] = "Server: IIS" CR LF;
2)关闭Nginx

[root@centos01 ~]# killall -I -QUIT nginx
3)重新配置Nginx编译安装nginx

[root@centos01 ~]# cd /usr/src/nginx-1.6.0/
[root@centos01 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
--with-http_stub_status_module
[root@centos01 nginx-1.6.0]# make
&& make install
4)启动Nginx

[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp
0 0 0.0.0.0:80 0.0.0.0:* LISTEN 45793/nginx: master
6)查看伪装的Nginx版本

7)修改Nginx主配置隐藏版本

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
worker_connections 1024;
}
http {

include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server_tokens off;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log
logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
}
  

8)停止服务在启动Nginx

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx
9)客户端验证

10)管理Nginx服务进程用户

[root@centos01 ~]# ps -ef | grep nginx root
45889 1 0 20:06 ? 00:00:00 nginx: master process nginx
nginx
45890 45889 0 20:06 ? 00:00:00 nginx: worker process
root
45912 941 0 20:08 pts/1 00:00:00 grep --color=auto nginx

2、在Nginx网站服务器配置网页缓存时间

1)修改nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.(gif|jpg|jpeg|png|bmp|swf)$ {
root /www;
index index.html index.htm;
expires 1d;
}
2)上传一张图片

2)客户端访问验证

 

3、配置Nginx日志切割

1)创建目录存储切割日志数据

[root@centos01 ~]# mkdir /nginx_log
2)创建日志切割脚本

[root@centos01 ~]# vim /opt/nginx_log_bak.sh
#!/bin/bash
# Filename: fenge.sh
d=$(date -d "-1 day" "+%Y%m%d")
logs_path="/nginx_log/"pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path
mv /usr/local/nginx/logs/www.stz.com.access.log ${logs_path}/www.stz.com.access.log-$d
kill -USR1 $(cat $pid_path)
find $logs_path -mtime +30 |xargs rm -rf
3)添加执行权限

[root@centos01 ~]# chmod +x /opt/nginx_log_bak.sh
4)设置计划任务备份nginx日志,每间隔两分钟备份一次日志

[root@centos01 ~]# crontab -e
*/2 * * * * /opt/nginx_log_bak.sh
5)查看生成的日志

4、设置nginx网站服务器保持时间和进程优化

1)修改Nginx主配置文件保持时间和进程优化

[root@centos01 ~]# vim
/usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 2;
pid logs/nginx.pid;
events {
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 10;
client_header_timeout 15;
client_body_timeout 15;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
}
2)重新启动Nginx

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx
3)查看启动nginx进程·

[root@centos01 ~]# ps -ef | grep nginx root
46684 1 0 20:55 ? 00:00:00 nginx: master process nginx
nginx
46685 46684 0 20:55 ? 00:00:00 nginx: worker process
nginx
46686 46684 0 20:55 ? 00:00:00 nginx: worker process
root
46688 941 0 20:55 pts/1 00:00:00 grep --color=auto nginx

5、配置nginx压缩数据

1)修改主配置文件压缩数据

[root@centos01 ~]# vim
/usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 2;
pid logs/nginx.pid;
events {
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 10;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
}
2)重新启动nginx服务

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx
3)查看配置压缩数据

6、配置Nginx防盗链

1)上传错误图片

2)设置nginx网站图片

[root@centos01 www]# vim index.html
www.stz.com <img src="./src.jpg" />
3)访问nginx图片

4)设置apache盗用Nginx链接

[root@centos03 ~]# vim /var/www/html/index.html
www.benet.com<br/>
<img src="http://www.stz.com/logo.jpg" />
5)访问apache

6)配置nginx配置防盗链

[root@centos01 ~]# vim
/usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 2;
pid logs/nginx.pid;
events {
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
location ~* \.(jpg|gif|swf)$ {
valid_referers none blocked *.stz.com stz.com;
if ($invalid_referer) {
rewrite ^/ http://www.stz.com/th.jfif;
}
}

}
}
7)重启服务

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx
8)客户端验证

有关一步一步教你Nginx优化和防盗链的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby-on-rails - 如何用不同的用户运行nginx主进程 - 2

    A/ctohttp://wiki.nginx.org/CoreModule#usermaster进程曾经以root用户运行,是否可以以不同的用户运行nginxmaster进程? 最佳答案 只需以非root身份运行init脚本(即/etc/init.d/nginxstart),就可以用不同的用户运行nginxmaster进程。如果这真的是你想要做的,你将需要确保日志和pid目录(通常是/var/log/nginx&/var/run/nginx.pid)对该用户是可写的,并且您所有的listen调用都是针对大于1024的端口(因为绑定(

  3. Ruby 缺少常量表达式优化? - 2

    我希望Ruby的解析器会进行这种微不足道的优化,但似乎并没有(谈到YARV实现,Ruby1.9.x、2.0.0):require'benchmark'deffib1a,b=0,1whileb由于这两种方法除了在第二种方法中使用预定义常量而不是常量表达式外是相同的,因此Ruby解释器似乎在每个循环中一次又一次地计算幂常数。是否有一些Material说明为什么Ruby根本不进行这种基本优化或只在某些特定情况下进行? 最佳答案 很抱歉给出了另一个答案,但我不想删除或编辑我之前的答案,因为它下面有有趣的讨论。正如JörgWMittag所说,

  4. ruby-on-rails - 优化读取数据库和写入csv文件 - 2

    我正在尝试从数据库中读取大量单元格(超过100.000个)并将它们写入VPSUbuntu服务器上的csv文件。碰巧服务器没有足够的内存。我正在考虑一次读取5000行并将它们写入文件,然后再读取5000行,等等。我应该如何重构我当前的代码以使内存不会被完全消耗?这是我的代码:defwrite_rows(emails)File.open(file_path,"w+")do|f|f该函数由sidekiqworker调用:write_rows(user.emails)感谢您的帮助! 最佳答案 这里的问题是,当您调用emails.each时,

  5. ruby-on-rails - Websocket-rails 不适用于 Nginx 和 Unicorn 的生产环境 - 2

    我有带有gemwebsocket-rails0.7的Rails3.2应用程序。在开发机上,一切正常在生产环境中,我使用Nginx/1.6作为代理服务器,Unicorn作为http服务器。Thin用于独立模式(在https://github.com/websocket-rails/websocket-rails/wiki/Standalone-Server-Mode之后)。nginx配置:location/websocket{proxy_passhttp://localhost:3001/websocket;proxy_http_version1.1;proxy_set_headerUp

  6. ruby - 没有 nginx 的 Puma - 同一 IP 上的多个 ruby​​ 应用程序 :PORT - 2

    Nginx在生产中的重要性通常基于它为慢速客户端提供服务的能力;在RESTfulAPI的设置中,它似乎是生产堆栈的一个不必要的层,尤其是Puma(不像广泛使用的unicorn可以处理nginx工作)。Pumacanallowmultipleslowclientstoconnectwithoutrequiringaworkertobeblockedontherequesttransaction.Becauseofthis,Pumahandlesslowclientsgracefully.HerokurecommendsPumaforuseinscenarioswhereyouexpect

  7. ruby - 使用 ruby​​-vips 的第一步 - 2

    我正在尝试实现/转换daltonize将色盲人的图像校正为ruby的算法。在javascript中编写了两个主要的引用实现和python+我不熟悉的语言/环境中的其他实现。我几乎没有图像处理方面的经验,更不用说VIPS/ruby​​-vips了。我想知道如何迈出第一步。该文档似乎主要使用C/C++,而在ruby​​方面很少。它也非常详细。我什至不确定要使用哪些基本操作。看起来lin函数是一个很好的起点,但我不确定如何应用它。任何具有VIPS经验的人都可能在几分钟内算出整个算法。我想知道是否有人可以给我一些关于从哪里开始的指示。具体来说:如何访问单个(R/G/B)元素?是否有基于道尔顿化

  8. ruby-on-rails - Foreman无法启动Nginx,但我可以手动启动。为什么? - 2

    我目前正在运行Foreman在暂存(Ubuntu)上,一旦我开始工作,就会切换到使用upstart。我的Procfile.staging看起来像这样:nginx:sudoservicenginxstartunicorn:bundleexecunicorn-c./config/unicorn.rbredis:bundleexecredis-serversidekiq:bundleexecsidekiq-v-C./config/sidekiq.yml我可以使用以下方法成功启动nginx:$sudoservicenginxstart然而,当我运行$foremanstart时,当其他三个进程成

  9. 软约束、硬约束、Minimum Snap的轨迹优化方法 - 2

    文章目录前言约束硬约束的轨迹优化Corridor-BasedTrajectoryOptimizationBezierCurveOptimizationOtherOptions软约束的轨迹优化Distance-BasedTrajectoryOptimization优化方法前言可以看看我的这几篇Blog1,Blog2,Blog3。上次基于MinimumSnap的轨迹生成,有许多优点,比如:轨迹让机器人可以在某个时间点抵达某个航点。任何一个时刻,都能数学上求出期望的机器人的位置、速度、加速度、导数。MinimumSnap可以把问题转换为凸优化问题。缺点:MnimumSnap可以控制轨迹一定经过中间的

  10. ruby-on-rails - 负载测试期间 Unicorn CPU 使用率激增,优化方法 - 2

    我对为我的RubyonRails3.1.3应用优化我的Unicorn设置的方法很感兴趣。我目前正在高CPU超大实例上生成14个工作进程,因为我的应用程序在负载测试期间似乎受CPU限制。在模拟负载测试中,每秒大约20个请求重放请求,我的实例上的所有8个内核都达到峰值,盒子负载飙升至7-8个。每个unicorn实例使用大约56-60%的CPU。我很好奇可以通过哪些方式对其进行优化?我希望能够每秒将更多请求汇集到这种大小的实例上。内存和所有其他I/O一样完全正常。在我的测试过程中,CPU越来越低。 最佳答案 如果您受CPU限制,您希望使用

随机推荐