Nginx主配置文件中各个参数详解以及部分实例

本篇讲解一些nginx配置文件中参数的详解以及部分实例:

user nginx nginx ;

Nginx用户及组:用户 组。window下不指定

worker_processes 8;

工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。

error_log  logs/error.log;  

error_log  logs/error.log  notice;  

error_log  logs/error.log  info;  

错误日志:存放路径。

pid logs/nginx.pid;

pid(进程标识符):存放路径。

worker_rlimit_nofile 204800;

指定进程可以打开的最大描述符:数目。

这个指令是指当一个nginx进程打开的最多文件描述符数目,
理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,
但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。

events

{

use epoll;

使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,
window下不指定。

worker_connections 204800;

没个工作进程的最大连接数量。根据硬件调整,和前面工作进程
配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许
的最多连接数,理论上每台nginx服务器的最大连接数为。worker_processes*worker_connections

keepalive_timeout 60;

keepalive超时时间。

client_header_buffer_size 4k;

客户端请求头部的缓冲区大小。这个可以根据你的系统分页
大小来设置,一般一个请求头的大小不会超过1k,不过由于
一般系统分页都要大于1k,所以这里设置为分页大小。

分页大小可以用命令getconf PAGESIZE 取得。

但也有client_header_buffer_size超过4k的情况,但是
client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。

open_file_cache max=65535 inactive=60s;

这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,
建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。

open_file_cache_valid 80s;

这个是指多长时间检查一次缓存的有效信息。

open_file_cache_min_uses 1;

open_file_cache指令中的inactive参数时间内文件的最少
使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,
如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。

日志格式设置。

$remote_addr$http_x_forwarded_for用以记录客户端的ip地址;

$remote_user:用来记录客户端用户名称;

$time_local: 用来记录访问时间与时区;

$request: 用来记录请求的url与http协议;

$status: 用来记录请求状态;成功是200$body_bytes_sent :记录发送给客户端文件主体内容大小;

$http_referer:用来记录从那个页面链接访问过来的;

$http_user_agent:记录客户浏览器的相关信息;

通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,
通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理
服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,
用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。

access_log  logs/host.access.log  main;

access_log  logs/host.access.404.log  log404;

用了log_format指令设置了日志格式之后,需要用access_log
指令指定日志文件的存放路径;

client_header_buffer_size 4k;

客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,
一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,
所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。

large_client_header_buffers 8 128k;

客户请求头缓冲大小。nginx默认会用client_header_buffer_size
这个buffer来读取header值,如果header过大,它会使用
large_client_header_buffers来读取。

open_file_cache_errors on;

open_file_cache_errors
语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.

open_file_cache_min_uses

语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 
使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的
参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache
中总是打开状态.
open_file_cache_valid

语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.

client_max_body_size 300m;设定通过nginx上传文件的大小

sendfile on;

sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)
来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO
重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。

tcp_nopush on;

此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的
时候使用

proxy_connect_timeout 90; 
后端服务器连接的超时时间_发起握手等候响应超时时间

proxy_read_timeout 180;

连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候
处理(也可以说是后端服务器处理请求的时间)

proxy_send_timeout 180;

后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据

proxy_buffer_size 256k;

设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小

proxy_buffers 4 256k;

设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况
也为分页大小,根据操作系统的不同可能是4k或者8k

proxy_temp_file_write_size 256k;

设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长

proxy_temp_path /data0/proxy_temp_dir;

proxy_temp_path和proxy_cache_path指定的路径必须在同一分区

proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#设置内存缓存空间大小为200MB,1天没有被访问的内容自动清除,
硬盘缓存空间大小为30GB。

keepalive_timeout 120;keepalive超时时间。

client_body_buffer_size 512k;
如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是
IE浏览器,来提交任意小于256k的图片,都很正常。如果注释该指令,
使用默认的client_body_buffer_size设置,也就是操作系统页面大
小的两倍,8k或者16k,问题就出现了。
无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,
都返回500 Internal Server Error错误

轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

upstream bakend{#定义负载均衡设备的Ip及设备状态}{
ip_hash;   算法
server 172.25.38.6:8080;
server 172.25.38.7:8080;
}
在需要使用负载均衡的server中增加
proxy_pass http://bakend/;   

每个设备的状态设置为:
1.down表示单前的server暂时不参与负载
2.weight为weight越大,负载的权重就越大。
3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only设置为On 可以讲client post过来的数据记录
到文件中用来做debug
client_body_temp_path设置记录文件的目录 可以设置最多3层目录

location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

##配置虚拟机
server

{   
    listen 80;
配置监听端口

server_name image.***.com;
配置访问域名

location ~* \.(mp3|exe)$ {
对以“mp3或exe”结尾的地址进行负载均衡

proxy_pass http://img_relay$request_uri;
设置被代理服务器的端口或套接字,以及URL

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上

}

实验环境:

server6 nginx端  172.25.38.7
server5 apache端  172.25.38.6

可以下载图片:

[root@server6 logs]# cd /usr/local/lnmp/nginx/conf/
[root@server6 conf]# ls
cert.pem              fastcgi_params.default  mime.types.default  scgi_params.default
fastcgi.conf          koi-utf                 nginx.conf          uwsgi_params
fastcgi.conf.default  koi-win                 nginx.conf.default  uwsgi_params.default
fastcgi_params        mime.types              scgi_params         win-utf
[root@server6 conf]# vim nginx.conf
limit_conn_zone $binary_remote_addr zone=addr:10m;
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            #root /usr/local/tomcat/webapps/ROOT/;
            index  index.php index.html index.htm;
        }
        location /download/ {   建立一个访问目录
        limit_conn addr 1;
        }
[root@server6 conf]# nginx -s reload

这里写图片描述

[root@server6 conf]# cd ..
[root@server6 nginx]# cd html/
[root@server6 html]# cd download/   在默认访问目录下放一个图片
[root@server6 download]# ls
vim.jpg

这里写图片描述
在网页测试:
这里写图片描述
做一个压力测试:

[root@foundation38 Desktop]# ab -c1 -n 10 http://172.25.38.7/download/vim.jpg  单线程测试
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.25.38.7 (be patient).....done


Server Software:        nginx/
Server Hostname:        172.25.38.7
Server Port:            80

Document Path:          /download/vim.jpg
Document Length:        453575 bytes

Concurrency Level:      1
Time taken for tests:   0.011 seconds
Complete requests:      10
Failed requests:        0     没有失败的请求
Write errors:           0
Total transferred:      4538080 bytes
HTML transferred:       4535750 bytes
Requests per second:    885.90 [#/sec] (mean)
Time per request:       1.129 [ms] (mean)
Time per request:       1.129 [ms] (mean, across all concurrent requests)
Transfer rate:          392604.43 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     1    1   0.2      1       1
Waiting:        0    0   0.0      0       0
Total:          1    1   0.2      1       1

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      1
 100%      1 (longest request)
[root@foundation38 Desktop]# curl -I http://172.25.38.7/download/vim.jpg
HTTP/1.1 200 OK
Server: nginx/
Date: Wed, 08 Aug 2018 02:27:33 GMT
Content-Type: image/jpeg
Content-Length: 453575
Last-Modified: Wed, 08 Aug 2018 02:15:53 GMT
Connection: keep-alive
ETag: "5b6a5259-6ebc7"
Accept-Ranges: bytes

由于线程设置是一所以均可以过去:

[root@server6 nginx]# cd logs/
[root@server6 logs]# cat access.log   查看日志所有数据均过去了

这里写图片描述
用十个线程进行测试:

[root@foundation38 Desktop]# ab -c10 -n 10 http://172.25.38.7/download/vim.jpg
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.25.38.7 (be patient).....done


Server Software:        nginx/
Server Hostname:        172.25.38.7
Server Port:            80

Document Path:          /download/vim.jpg
Document Length:        537 bytes

Concurrency Level:      10
Time taken for tests:   0.002 seconds
Complete requests:      10
Failed requests:        1     有失败的请求
   (Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Write errors:           0
Non-2xx responses:      9
Total transferred:      460333 bytes
HTML transferred:       458408 bytes
Requests per second:    4587.16 [#/sec] (mean)
Time per request:       2.180 [ms] (mean)
Time per request:       0.218 [ms] (mean, across all concurrent requests)
Transfer rate:          206212.82 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    1   0.4      1       2
Waiting:        0    1   0.4      1       1
Total:          1    1   0.4      1       2

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      2
  90%      2
  95%      2
  98%      2
  99%      2
 100%      2 (longest request)

查看日志十个线程只可以过去一个其他的均报错503:
这里写图片描述
设置带宽:

[root@server6 conf]# vim nginx.conf
#gzip  on;
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;


        server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location /download/ {
        limit_conn addr 1;
        limit_rate 50k;   设置带宽每秒50k下载
        limit_req zone=one burst=5;
        }
[root@server6 conf]# nginx -s reload

真机测试加入了带宽下载时间会相应的变化图片大小440k:

[root@foundation38 Desktop]# time wget http://172.25.38.7/download/vim.jpg  没有加入带宽测试
--2018-08-08 10:52:32--  http://172.25.38.7/download/vim.jpg
Connecting to 172.25.38.7:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 453575 (443K) [image/jpeg]
Saving to: ‘vim.jpg.1100%[=======================================================>] 453,575     --.-K/s   in 0.001s  

2018-08-08 10:52:32 (350 MB/s) - ‘vim.jpg.1’ saved [453575/453575]


real    0m0.316s  时间很短就下载好了
user    0m0.000s
sys 0m0.004s
[root@foundation38 Desktop]# time wget http://172.25.38.7/download/vim.jpg  加入带宽之后
--2018-08-08 11:05:17--  http://172.25.38.7/download/vim.jpg
Connecting to 172.25.38.7:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 453575 (443K) [image/jpeg]
Saving to: ‘vim.jpg.2100%[=======================================================>] 453,575     55.3KB/s   in 8.0s   

2018-08-08 11:05:25 (55.3 KB/s) - ‘vim.jpg.2’ saved [453575/453575]


real    0m8.017s  时间相应的变化
user    0m0.003s
sys 0m0.004s

这里写图片描述
加入访问控制:

[root@server6 conf]# vim nginx.conf
location /admin/ {
        allow 172.25.38.250;  仅仅允许本机访问
        deny all;
        }
[root@server6 conf]# nginx -s reload

这里写图片描述
只有本机可以访问:
这里写图片描述
加入访问控制同一个网段可以访问:

[root@server6 conf]# vim nginx.conf
location /admin/ {
        allow 172.25.38.0/24;  仅仅允许本个网段访问
        deny all;
        }
[root@server6 conf]# nginx -s reload

这里写图片描述
静态文件的时间:

[root@server6 conf]# vim nginx.conf
location ~ .*\.(gif|jpg|png)$ {
                expires 30d;   默认存在时间30天
        }
[root@server6 conf]# nginx -s reload
[root@server6 conf]# cd ..
[root@server6 nginx]# cd html/
[root@server6 html]# ls
50x.html  bbs       example.php  index.php     readme
admin     download  index.html   memcache.php  utility
[root@server6 html]# cd download/
[root@server6 download]# ls
vim.jpg
[root@server6 download]# cp vim.jpg .. 更改图片路径直接为默认路径
[root@server6 download]# cd ..
[root@server6 html]# ls
50x.html  bbs       example.php  index.php     readme   vim.jpg
admin     download  index.html   memcache.php  utility

这里写图片描述
在真机测试与设置的30天相符合:

[root@foundation38 Desktop]# curl -I http://172.25.38.7/vim.jpg
HTTP/1.1 200 OK
Server: nginx/
Date: Wed, 08 Aug 2018 04:54:53 GMT
Content-Type: image/jpeg
Content-Length: 453575
Last-Modified: Wed, 08 Aug 2018 04:54:13 GMT
Connection: keep-alive
ETag: "5b6a7775-6ebc7"
Expires: Fri, 07 Sep 2018 04:54:53 GMT   九月七号失效
Cache-Control: max-age=2592000
Accept-Ranges: bytes

这里写图片描述
访问本机时直接报错500:

[root@server6 conf]# vim nginx.conf
server {
        listen       80;
        server_name  _;
        return 500;   直接返回550
[root@server6 conf]# nginx -s reload

这里写图片描述
在网页访问就会报错550:
这里写图片描述
重定向:

[root@server6 conf]# vim nginx.conf
server {
        listen 80;
        server_name www.westos.org westos.org bbs.westos.org;

        #rewrite ^(.*)$ https://www.westos.org$1 permanent;

        #rewrite ^/bbs$ http://bbs.westos.org$1 permanent;
        #rewrite ^/bbs(.*)$ http://bbs.westos.org$1 permanent;
        if ($host = "bbs.westos.org" ){
        rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent;  访问bbs.westos.org重定向到http://www.westos.org/bbs
        }
        location / {
                root /www1;
                index index.html;
        }
}
[root@server6 conf]# nginx -s reload

这里写图片描述
访问bbs.westos.org会重定向:
这里写图片描述
中文乱码问题:

[root@server6 html]# ls
50x.html  bbs       example.php  index.php     readme   vim.jpg
admin     download  index.html   memcache.php  utility
[root@server6 html]# cd /www1/
[root@server6 www1]# ls
bbs  index.html
[root@server6 www1]# vim index.html
[root@server6 www1]# cat index.html 
www.westos.org 你好

这里写图片描述
在网页测试会乱码:
这里写图片描述
修改配置文件:

[root@server6 www1]# vim /usr/local/lnmp/nginx/conf/nginx.conf
[root@server6 www1]# nginx -s reload
     server {
        listen 80;
        server_name www.westos.org westos.org bbs.westos.org;
        charset utf-8;   写入中文编码字符集
        #rewrite ^(.*)$ https://www.westos.org$1 permanent;

        #rewrite ^/bbs$ http://bbs.westos.org$1 permanent;
        #rewrite ^/bbs(.*)$ http://bbs.westos.org$1 permanent;
        if ($host = "bbs.westos.org" ){
        rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent;
        }
        location / {
                root /www1;
                index index.html;
        }
}

这里写图片描述
在网页测试已经解决乱码:
这里写图片描述
复制日志,将访问图片等日志关闭:

[root@server6 www1]# vim /usr/local/lnmp/nginx/conf/nginx.conf 当访问图片等格式关闭日志
[root@server6 www1]# nginx -s reload

这里写图片描述

[root@server6 www1]# cd /usr/local/lnmp/nginx/
[root@server6 nginx]# cd logs/
[root@server6 logs]# ls
access.log  error.log  nginx.pid
[root@server6 logs]# ll
total 312
-rw-r--r-- 1 root root 274451 Aug  8 14:37 access.log
-rw-r--r-- 1 root root  27179 Aug  8 14:44 error.log
-rw-r--r-- 1 root root      5 Aug  8 10:12 nginx.pid
[root@server6 logs]# du -h access.log 
276K    access.log
[root@server6 logs]# date
Wed Aug  8 14:45:10 CST 2018
[root@server6 logs]# date +%F -d -1day
2018-08-07
[root@server6 logs]# cd /opt/
[root@server6 opt]# ls
[root@server6 opt]# vim nginx_log.sh
[root@server6 opt]# cat nginx_log.sh  编写脚本每晚十二点移动日志重载服务
#!/bin/bash

cd /usr/local/lnmp/nginx/logs && mv access.log access.log_$(date +%F -d -1day)
/usr/local/lnmp/nginx/sbin/nginx -s reload
[root@server6 opt]# crontab -e
no crontab for root - using an empty one

这里写图片描述

crontab: installing new crontab
[root@server6 opt]# chmod +x /opt/nginx_log.sh   赋予执行权限
[root@server6 opt]# /opt/nginx_log.sh   调用脚本
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
[root@server6 opt]# cd /usr/local/
[root@server6 local]# cd lnmp/nginx/logs/
[root@server6 logs]# ls   已经生成了复制日志
access.log  access.log_2018-08-07  error.log  nginx.pid
[root@server6 logs]# ll
total 312
-rw-r--r-- 1 root root      0 Aug  8 14:49 access.log
-rw-r--r-- 1 root root 274451 Aug  8 14:37 access.log_2018-08-07
-rw-r--r-- 1 root root  27596 Aug  8 14:52 error.log
-rw-r--r-- 1 root root      5 Aug  8 10:12 nginx.pid

这里写图片描述
重新打开一个虚拟机阿帕奇作提供访问界面:

[root@server5 ~]# yum install -y httpd  安装阿帕其
[root@server5 ~]# cd /var/www/html/
[root@server5 html]# ls
[root@server5 html]# vim index.html
[root@server5 html]# cat index.html   写入一个默认访问
www.westos.org

这里写图片描述
在网页测试可以看到默认访问页记得打开阿帕奇:
这里写图片描述
在nginx端默认访问放入照片:

[root@server6 www1]# pwd
/www1
[root@server6 www1]# ls
bbs  index.html  jisuxz_Dilraba_27.jpg
[root@server6 www1]# 

在网页测试可以访问:
这里写图片描述

什么是盗链?

盗链是指服务提供商自己不提供服务的内容,通过技术手段绕过其它有
利益的最终用户界面(如广告),直接在自己的网站上向最终用户提供
其它服务提供商的服务内容,骗取最终用户的浏览和点击率。受益者不
提供资源或提供很少的资源,而真正的服务提供商却得不到任何的收益。

盗链的特点:

网站盗链会大量消耗被盗链网站的带宽,而真正的点击率也许会很小,
严重损害了被盗链网站的利益。早期的盗链一般是一些比较小的网站盗
取一些有实力的大网站的地址,盗链的目标比较有针对性,现如今,一
些大型的网站也已经开始把盗链的目光锁定在了整个互联网上,窃取整
个互联网上的其它机器的带宽。常见的盗链有以下几种:图片盗链、音频
盗链、视频盗链、文件盗链。
一般要被浏览的页面并不是一次全部传送到客户端的。如果客户请求的
是一个带有许多图片和其它信息的页面,那么最先的一个HTTP 请求被
传送回来的是这个页面的HTML文本,客户端浏览器对这段文本解释执行
后,发现其中还有其它文件,客户端浏览器会再发送一条或者更多HTTP
请求,当这些请求被处理后其它文件才被传送到客户端,然后浏览器将
这些文件放到页面的正确位置。一个完整的页面要经过发送多条HTTP 
请求才能够被完整地显示。基于这样的机制,盗链就成为可能,服务提
供商完全可以在自己的页面中嵌入别人的链接,显示在自己的页面上,
以达到盗链的目的。
根据盗链的形式的不同,可以简单地把盗链分成两类:常规盗链和分布
式盗链。常规盗链比较初级,同时也比较常见,具有一定的针对性,只
盗用某个或某些网站的链接。技术含量不高,实现也比较简单,只需要
在自己的页面嵌入别人的链接即可。分布式盗链是盗链的一种新的形式,
系统设计复杂,难度相对较大。这种盗链一般不针对某一个网站,互联
网上任何一台机器都可能成为盗链的对象。服务提供商一般会在后台设
置专门程序(Spider)在Internet上抓取有用的链接, 然后存储到自己
的数据库中。而对于最终用户的每次访问,都将其转化为对已有数据库的
查询,被查询到的URL 就是被盗链的对象。由于对文件的访问已经被浏览
器屏蔽掉了,所以最终用户感觉不到所访问的链接是被盗取的链接。

在server5作盗链:

[root@server5 html]# vim index.html 
[root@server5 html]# cat index.html  写入由网页转换脚本
<html>

<body>

<img src="http://www.westos.org/jisuxz_Dilraba_27.jpg">

</body>

</html>
[root@server5 html]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.38.5 for ServerName
                                                           [  OK  ]

这里写图片描述
直接调用server5的ip就可以看到server6的内容:
这里写图片描述
真机加入一个解析:

[root@foundation38 Desktop]# vim /etc/hosts
[root@foundation38 Desktop]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.38.100 www.westos.org bbs.westos.org
172.25.38.6 www.westos.org bbs.westos.org bbs.westos.org daolian.westos.org
172.25.38.2 server1
172.25.38.3 server2
172.25.38.7  www.westos.org  www.linux.org bbs.westos.org

这里写图片描述
网页调用域名可以查看图片:
这里写图片描述
在server6查看日志的时候,显示数据是从server5获取的,但显然不是,这样server5就可以一直给server6的服务器增加负载用来或取数据这就是盗链的意思

[root@server6 ~]# cd /usr/local/lnmp/nginx/logs/
[root@server6 logs]# cat access.log
172.25.38.250 - - [08/Aug/2018:15:08:23 +0800] "GET /jisuxz_Dilraba_27.jpg HTTP/1.1" 200 914410 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"
172.25.38.250 - - [08/Aug/2018:16:34:45 +0800] "GET /jisuxz_Dilraba_27.jpg HTTP/1.1" 404 163 "http://172.25.38.6/" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"
172.25.38.250 - - [08/Aug/2018:16:35:34 +0800] "GET /jisuxz_Dilraba_27.jpg HTTP/1.1" 200 914410 "http://172.25.38.6/" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"

这里写图片描述
在server6配置防止盗链:

[root@server6 logs]# vim /usr/local/lnmp/nginx/conf/nginx.conf
if ($host = "bbs.westos.org" ){
        rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent;
        }
        location / {
                root /www1;
                index index.html;
        }
        location ~ \.(gif|jpg|png)$ {
                root /www1;
        valid_referers none blocked www.westos.org;  除了www.westos.org访问其他访问均返回403
        if ($invalid_referer) {
        return 403;
        }
        }
[root@server6 logs]# nginx -s reload
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored

这里写图片描述
在网页测试除了www.westos.org域名其他的均被阻挡:
这里写图片描述
给盗链用户回应一张图片:

[root@server6 logs]# cd ..
[root@server6 nginx]# cd /www2/
[root@server6 www2]# ls
index.html
[root@server6 www2]# pwd
/www2
[root@server6 www2]# ls  放一张图片
daolian.jpg  index.html

这里写图片描述

[root@server6 www2]# vim /usr/local/lnmp/nginx/conf/nginx.conf
    server {
        listen   80;
        server_name  localhost;

        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.pem;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   /www1;
            index  index.html index.htm;
        }
   }

server {
    listen 80;
    server_name www.westos.org westos.org;
    charset utf-8;

    location / {
        root /www1;
        index index.html;
    }
    location ~ \.(gif|jpg|png)$ {
        root /www1;
    valid_referers none blocked www.westos.org;
    if ($invalid_referer) {
    rewrite ^/ http://bbs.westos.org/daolian.jpg; 写入重定向
    }
    }
}

server {
        listen 80;
        server_name bbs.westos.org;
    #rewrite ^(.*)$ http://www.westos.org/bbs$1 permanent;
        location / {
                root /www2;
                index index.html;
        }
}

}
[root@server6 www2]# nginx -s reload

在网页测试其他主机访问的时候会回应一张照片:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/aaaaaab_/article/details/81504069
今日推荐