您的位置 首页 php

Nginx防盗链\访问控制,Nginx解析PHP的相关配置,Nginx代理

nginx 防盗链

Nginx防盗链也是使用location板块,和不记录静态文件和过期时间写在一起。

打开虚拟主机配置文件

[root@shuai-01 ~]# vim /usr/local/nginx/conf/vhost/ test .com.conf 
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;
}
#不是白名单的referer ,返回403
access_log off;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
 

注意:location ~* ^.+.这里匹配到的后面的内容是不区分大小写。

测查配置文件语法并重新加载:

[root@shuai-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@shuai-01 ~]# /usr/local/nginx/sbin/nginx -s reload
1
2
3
4
5
 

测试:

[root@shuai-01 ~]# curl -e "" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 403 Forbidden
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 11:00:19 GMT
Content-Type: text/ html 
Content-Length: 169
Connection: keep-alive
[root@shuai-01 ~]# curl -e "" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 11:01:15 GMT
Content-Type: image/gif
Content-Length: 12
Last-Modified: Mon, 08 Jan 2018 16:38:47 GMT
Connection: keep-alive
 ETag : "5a539e97-c"
Expires: Tue, 16 Jan 2018 11:01:15 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 

Nginx的 访问控制

关于做防盗链和访问控制的原因我在httpd做访问控制和防盗链时已经说得比较清楚了。

需求:访问/admin/目录,只允许那几个IP进行访问。

打开虚拟主机配置文件

location /admin/
{
 allow 127.0.0.1;
 allow 192.168.176.135;
 deny all;
}
1
2
3
4
5
6
7
 

这里Nginx设置访问控制和 Apache 是有很大的不同,关于这个allow,deny。Apache是有一个顺序的,Nginx没有。

测查配置文件语法并重新加载:

[root@shuai-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@shuai-01 ~]# /usr/local/nginx/sbin/nginx -s reload
1
2
3
4
5
 

检测:

[root@shuai-01 ~]# curl -e "" -x127.0.0.1:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 11:23:23 GMT
Content-Type: text/html
Content-Length: 5
Last-Modified: Tue, 09 Jan 2018 11:23:20 GMT
Connection: keep-alive
ETag: "5a54a628-5"
Accept-Ranges: bytes
[root@shuai-01 ~]# curl -x192.168.176.135:80 test.com/admin/ -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 09 Jan 2018 11:25:02 GMT
Content-Type: text/html
Content-Length: 5
Last-Modified: Tue, 09 Jan 2018 11:23:20 GMT
Connection: keep-alive
ETag: "5a54a628-5"
Accept-Ranges: bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 

针对正则进行访问控制:

location ~ .*(abc|image)/.*\.php$
{
 deny all;
}
1
2
3
4
5
 

针对user_agent进行限制:

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
 return 403;
}
1
2
3
4
5
 

return 403 和deny all 效果是一样的。

Nginx解析PHP的相关配置

核心配置:

location ~ \.php$
 {
 include fastcgi_params;
 fastcgi_pass unix:/ tmp /php-fcgi.sock;
 # fastcgi_pass 127.0.0.1:9000; 如果 php-fpm 配置监听配置的是IP就写这个
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
 }
1
2
3
4
5
6
7
8
9
 

Nginx要想解析PHP就要将这段核心配置写入配置文件中去。

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

如果是用的sock那么一定要放开php配置中的listen.mode=666(sock的权限位一定要有写的权限)

unix:/tmp/php-fcgi.sock这里的sock文件是php-fpm.conf中定义的

cat /usr/local/php-fpm/etc/php-fpm.conf配置文件中写什么就定义什么

如果php监听的是ip和端口,nginx中的配置文件就要改成

fastcgi_pass 127.0.0.1:9000;

fastcgi_param 中的路径也需要跟上面对应起来

Nginx502问题出现及解决方法:

Nginx代理

Nginx代理分正向代理和反向代理。

Nginx代理是在一台代理服务器中自定义一个域名(这个域名基本上跟web服务器的域名相同),该域名指向一个IP(web服务器),然后将用户的请求通过这台代理服务器访问指定的IP所对应的web服务器。

做代理服务器要先做一个新的虚拟主机配置文件。

[root@shuai-01 ~]# cd /usr/local/nginx/conf/vhost/
[root@shuai-01 vhost]# 
[root@shuai-01 vhost]# vim proxy.conf
1
2
3
4
 

配置文件:

server
{
 listen 80;
 server_name ask.apelearn.com;#定义代理服务器域名,一般的和web服务器域名相同
 location /
 {
 proxy_pass #指定web服务器IP
 proxy_set_header Host $host;#$host就是server_name
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 

保存退出并检查配置文件语法,重新加载

[root@shuai-01 vhost]# curl -x127.0.0.1:80 ask.apelear#.com/robots.txt
# robots.txt for MiWen
#
User-agent: *
Disallow: /?/admin/
Disallow: /?/people/
Disallow: /?/question/
Disallow: /account/
Disallow: /app/
Disallow: /cache/
Disallow: /install/
 

文章来源:智云一二三科技

文章标题:Nginx防盗链\访问控制,Nginx解析PHP的相关配置,Nginx代理

文章地址:https://www.zhihuclub.com/153840.shtml

关于作者: 智云科技

热门文章

网站地图