Nginx防盗链,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;
}

这里写图片描述

注意: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

测试:

[root@shuai-01 ~]# curl -e "http://www.qq.com/1.txt" -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 "http://www.test.com/1.txt" -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

Nginx的访问控制

关于做防盗链和访问控制的原因我在httpd做访问控制和防盗链时已经说得比较清楚了。 
http://blog.csdn.net/aoli_shuai/article/details/78895746

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

打开虚拟主机配置文件

location /admin/
{
    allow 127.0.0.1;
    allow 192.168.176.135;
    deny all;
}

这里写图片描述 
这里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

检测:

[root@shuai-01 ~]# curl -e "http://www.test.com/1.txt" -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

针对正则进行访问控制:

location ~ .*(abc|image)/.*\.php$
{
        deny all;
}

针对user_agent进行限制:

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
      return 403;
}

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;
    }

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问题出现及解决方法: 
http://ask.apelearn.com/question/9109

Nginx代理

Nginx代理分正向代理和反向代理。 
http://blog.csdn.net/zjf280441589/article/details/51501408 
这里写图片描述

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

配置文件:

server
{
    listen 80;
    server_name ask.apelearn.com;#定义代理服务器域名,一般的和web服务器域名相同

    location /
    {
        proxy_pass      http://121.201.9.155/;#指定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;
    }
}

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

[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/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值