49.Nginx防盗链 访问控制 解析php相关 代理服务器

12.13 Nginx防盗链

12.14 Nginx访问控制

12.15 Nginx解析php相关配置(502的问题)

12.16 Nginx代理

扩展

502问题汇总 http://ask.apelearn.com/question/9109

location优先级 http://blog.lishiming.net/?p=100

 

 

 

 

12.13 Nginx防盗链:

 

 

 

配置如下,可以和上面的配置结合起来

vim /usr/local/nginx/conf/vhost/test.com.conf

# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 把之前配置的过期时间注释掉。在第二个location开始写(因为同样用到了location)

# {

# expires 7d;

# access_log off;

# }

location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ ~*不区分大小写

{

expires 7d; 过期时间是7天

valid_referers none blocked server_names *.test.com ; 关于防盗链的是这部分(意思是关于白名单的referer是什么)

if ($invalid_referer) { 意思是如果不是白名单的,就会返回403

return 403;

}

access_log off; 访问日志是不记录

}

 

 

实例:

[root@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

# {

# expires 7d;

# access_log off;

# }

location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$

{

expires 7d;

valid_referers none blocked server_names *.test.com ;

if ($invalid_referer) {

return 403;

}

access_log off;

}

location ~ .*\.(js|css)$

{

expires 12h;

access_log off;

}

}

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/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

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@axinlinux-01 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/2.gif 测试防盗链,要指定referer

HTTP/1.1 403 Forbidden 指定referer为百度,跳转过来就是403

Server: nginx/1.8.0

Date: Wed, 15 Aug 2018 14:44:38 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive

[root@axinlinux-01 ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/2.gif

HTTP/1.1 200 OK 指定referer为test.com跳转过来就是200

Server: nginx/1.8.0

Date: Wed, 15 Aug 2018 14:43:29 GMT

Content-Type: image/gif

Content-Length: 19

Last-Modified: Tue, 14 Aug 2018 14:33:26 GMT

Connection: keep-alive

ETag: "5b72e836-13"

Expires: Wed, 22 Aug 2018 14:43:29 GMT

Cache-Control: max-age=604800

Accept-Ranges: bytes

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

 

12.14 Nginx访问控制:

 

 

 

 

平常在运维网站的时候,经常会有一些请求不正常或是故意的去做一些限制,比如有一些机密的不想让别人访问。就可以做一个白名单,只允许自己的公网IP或是公司的内部公网IP去访问

 

 

~~1.

需求:访问/admin/目录的请求,只允许某几个IP访问,配置如下:

~1.vim /usr/local/nginx/conf/vhost/test.com.conf

location /admin/

{

allow 192.168.133.1; 这个IP允许。跟apache有点区别,没有order。哪个在前哪个就优先生效。比如这个IP192.168.159.128访问过来,是allow(允许),就到此为止了,也就是允许的。不会再去执行下面的deny。而apache是谁在后最终执行的是哪一个

allow 127.0.0.1; 这个IP允许

deny all; 其他的全部deny(也就是以上两个IP是允许的,其他的都deny)

}

~2. mkdir /data/wwwroot/test.com/admin/

~3.echo “test,test”>/data/wwwroot/test.com/admin/1.html

~4.-t && -s reload

~5.curl -x127.0.0.1:80 test.com/admin/1.html -I

~6.curl -x192.168.133.130:80 test.com/admin/1.html -I

 

~~2.

可以匹配正则(也就是在能上传图片的目录里,禁止解析php):

~1.location ~ .*(upload|image)/.*\.php$ 只要是匹配upload的这个目录,以php结尾的

{

deny all; 满足以上条件的,全部deny

}

 

~~3.

根据user_agent限制(防止cc攻击。或是禁掉某些蜘蛛,不想被搜索掉,就可以吧一些网站封掉,没有任何一个网站能爬到你的网站,就相当于你的网站被隐藏了一样)

~1.if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato') 匹配(~)后面加*代表忽略大小写

{

return 403;

}

 

~~4. deny all和return 403效果一样(根据~~2与~~3里的配置语句)

 

 

 

 

实例:

~~1.

[root@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

location /admin/

{

allow 192.168.159.128;

allow 127.0.0.1;

deny all;

}

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/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

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@axinlinux-01 ~]# curl -e "http://www.baidu.com" -x127.0.0.1:80 test.com/admin/1.html -I

HTTP/1.1 200 OK

Server: nginx/1.8.0

Date: Wed, 15 Aug 2018 15:17:05 GMT

Content-Type: text/html

Content-Length: 13

Last-Modified: Wed, 15 Aug 2018 15:17:00 GMT

Connection: keep-alive

ETag: "5b7443ec-d"

Accept-Ranges: bytes

[root@axinlinux-01 ~]# curl -e "http://www.baidu.com" -x192.168.159.128:80 test.com/admin/1.html -I

HTTP/1.1 200 OK

Server: nginx/1.8.0

Date: Wed, 15 Aug 2018 15:18:40 GMT

Content-Type: text/html

Content-Length: 13

Last-Modified: Wed, 15 Aug 2018 15:17:00 GMT

Connection: keep-alive

ETag: "5b7443ec-d"

Accept-Ranges: bytes

~~2.

[root@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

location ~ .*(upload|image)/.*\.php$

{

deny all;

}

location ~ .*\.(js|css)$

{

expires 12h;

access_log off;

}

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/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

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@axinlinux-01 ~]# curl -x127.0.0.1:80 test.com/upload/1.php -I

HTTP/1.1 403 Forbidden 为403,被拒绝

Server: nginx/1.8.0

Date: Wed, 15 Aug 2018 15:35:39 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive

[root@axinlinux-01 ~]# curl -x127.0.0.1:80 test.com/upload/1.txt -I

HTTP/1.1 200 OK 访问1.txt就可以。代表设置成功

~~3.

[root@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')

{

return 403;

}

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/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

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@axinlinux-01 ~]# curl -x127.0.0.1:80 test.com/upload/1.txt -I

HTTP/1.1 200 OK 现在是可以访问的

Server: nginx/1.8.0

Date: Wed, 15 Aug 2018 15:49:40 GMT

Content-Type: text/plain

Content-Length: 9

Last-Modified: Wed, 15 Aug 2018 15:36:44 GMT

Connection: keep-alive

ETag: "5b74488c-9"

Accept-Ranges: bytes

[root@axinlinux-01 ~]# curl -A "Tomatojlknkljn" -x127.0.0.1:80 test.com/upload/1.php -I -A模仿一个user_agent

HTTP/1.1 403 Forbidden 这时候被403了

Server: nginx/1.8.0

Date: Wed, 15 Aug 2018 15:51:05 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive

 

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

12.15 Nginx解析php相关配置:

 

 

!!!注意:php-fpm配置文件中sock的定义是什么,Nginx的sock就要是什么。不然会502

配置如下:

~1.

location ~ \.php$

{

include fastcgi_params;

fastcgi_pass unix:/tmp/php-fcgi.sock; 这个地方需要注意!!在cat /usr/local/php-fpm/etc/php-fpm.conf里定义的“listen = /tmp/php-fcgi.sock”的路径写的是什么,在现在的这个地址里就要写什么,不然会502。也就是说,php-fpm定义的sock地址是什么,nginx的sock就要是什么,不然就会提示502

!还有一种可能会报502.是我们之前在定义php-fpm的时候sock的下面一行是不是定义了listen.mode=666权限

!除了以上两种,php-fpm的资源耗尽也会502。比如有个mysql查询的很慢,卡死了,就要去优化了

(~2的实例是为了证明php-fpm与Nginx的sock一致,此处与上面是整体的配置)

如果在php-fpm里的listen的sock是IP,那么这里就要写成 fastcgi_pass 192.168.159.128:9000

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;

注意:这里的路径/data/wwwroot/test.com要和上面的root路径对应起来

}

 

~3.

fastcgi_pass 用来指定php-fpm监听的地址或者socket

 

 

 

 

 

实例:

~1.

[root@axinlinux-01 ~]# cat /usr/local/php-fpm/etc/php-fpm.conf

[global]

pid = /usr/local/php-fpm/var/run/php-fpm.pid

error_log = /usr/local/php-fpm/var/log/php-fpm.log

[www]

listen = /tmp/php-fcgi.sock 首先查看sock的路径

#listen = 127.0.0.1:9000

listen.mode = 666

user = php-fpm

group = php-fpm

[root@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

location ~ \.php$

{

include fastcgi_params;

fastcgi_pass unix:/tmp/php-fcgi.sock; 跟上面的sock路径要是一样的

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;

}

[root@axinlinux-01 ~]# vim /data/wwwroot/test.com/1.php 先不reload,我们先vim一个php,做测试

[root@axinlinux-01 ~]# curl -x192.168.159.128:80 test.com/1.php 发现并没有解析phpinfo()

<?php

phpinfo();

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -t 这时候我们在-t / -s reload

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

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@axinlinux-01 ~]# curl -x192.168.159.128:80 test.com/1.php 再curl发现可以解析了

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head>

<style type="text/css">

~2.

[root@axinlinux-01 ~]# vim /usr/local/php-fpm/etc/php-fpm.conf 我们先更改php-fpm的sock监听为IP

[global]

pid = /usr/local/php-fpm/var/run/php-fpm.pid

error_log = /usr/local/php-fpm/var/log/php-fpm.log

[www]

#listen = /tmp/php-fcgi.sock 将之前的sock注释掉

listen = 127.0.0.1:9000 改为IP端口一般就为9000

[root@axinlinux-01 ~]# /etc/init.d/php-fpm reload 把php-fpm重新加载(也支持reload)

[root@axinlinux-01 ~]# /usr/local/php-fpm/sbin/php-fpm -t

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -t 把Nginx也重新加载

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

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@axinlinux-01 ~]# curl -x192.168.159.128:80 test.com/1.php 再来测试.php,就不能解析了

<html>

<head><title>502 Bad Gateway</title></head>

<body bgcolor="white">

<center><h1>502 Bad Gateway</h1></center>

<hr><center>nginx/1.8.0</center>

</body>

</html>

[root@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

location ~ \.php$

{

include fastcgi_params;

#fastcgi_pass unix:/tmp/php-fcgi.sock;

fastcgi_pass 127.0.0.1:9000; 记得加分号,阿鑫在做的时候忘加,-t的时候导致报错

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;

}

[root@axinlinux-01 ~]# /etc/init.d/php-fpm -t php-fpm测试和加载

Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status}

[root@axinlinux-01 ~]# /etc/init.d/php-fpm reload

Reload service php-fpm done

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -t Nginx测试和加载

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

[root@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload

[root@axinlinux-01 ~]# !curl php-fpm和Nginx全部修改之后,测试成功。解析成功

curl -x192.168.159.128:80 test.com/1.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head>

<style type="text/css">

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

12.16 Nginx代理:

 

 

比如,像访问一个服务器但是这个服务器只有一个私网,这是不可能访问到的。如果想访问有一个办法,有一个中间者,这个中间者有一个特性,和web服务器能互通也能和用户互通。那么就能作为Web服务器和用户之间的一个代理者。那么这个就是代理服务器,如下图:

应用在用户与Web服务器不能互通,或者互通太慢(比如访问美国的网站)的场景

 

~1. cd /usr/local/nginx/conf/vhost 需要配置一个新的虚拟主机配置文件

~2.vim proxy.conf //加入如下内容 名字叫做 proxy.conf

server

{

listen 80;

server_name ask.apelearn.com; 定义域名

没有root,因为是代理的,所以不需要

location /

{

proxy_pass http://121.201.9.155/; 真正的web服务器IP(也就是远程服务端,Web服务器的IP)

proxy_set_header Host $host; 要访问的域名是上面定义的server_name。也就是这里的$host是上面的server_name

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3866149/blog/1928637

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值