48次课(Nginx防盗链Nginx访问控制Nginx解析php相关配置Nginx代理)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了48次课(Nginx防盗链Nginx访问控制Nginx解析php相关配置Nginx代理)相关的知识,希望对你有一定的参考价值。

nginx防盗链

技术分享图片

编辑虚拟配置文件

[[email protected] ~]# 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;
    }
    access_log off;
}

添加配置内容解释

技术分享图片

检测配置和重新加载配置

[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

测试

技术分享图片

Nginx访问控制

技术分享图片
技术分享图片

编辑虚拟配置文件允许ip和不允许ip访问这是针对目录的

[[email protected] ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location /admin/
{
    allow 192.168.133.1;
    allow 127.0.0.1;
    deny all;
}

添加规则和解释

技术分享图片

检查配置和加载

[[email protected] ~]# /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
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

测试

[[email protected] ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/admin/    ##是白名单里的ip可以访问
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Wed, 15 Aug 2018 09:08:28 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Tue, 14 Aug 2018 03:25:24 GMT
Connection: keep-alive
ETag: "5b724ba4-12"
Accept-Ranges: bytes
[[email protected] ~]# curl -x192.168.63.100:80 -I test.com/admin/      ##是白名单里的ip可以访问
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Wed, 15 Aug 2018 09:09:38 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Tue, 14 Aug 2018 03:25:24 GMT
Connection: keep-alive
ETag: "5b724ba4-12"
Accept-Ranges: bytes
[[email protected] ~]# curl -x192.168.0.110:80 -I test.com/admin/     ##不是白名单的ip不能访问
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Wed, 15 Aug 2018 09:23:08 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

编辑虚拟配置文件这是针对正则的

[[email protected] ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location ~ .*(abc|image)/.*.php$
{
        deny all;
}

技术分享图片

检查配置和加载

[[email protected] ~]# /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
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

测试

[[email protected] ~]# mkdir /data/wwwroot/test.com/upload     ##做一个模拟创建一个upload目录
[[email protected] ~]# echo "111" > /data/wwwroot/test.com/upload/1.php     ##在upload里面创建个PHP文件
[[email protected] ~]# curl -x127.0.0.1:80 test.com/upload/1.php     ##测试访问upload/1.php是被拒绝的
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>

根据user_agent限制配置虚拟文件

[[email protected] ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
if ($http_user_agent ~ ‘Spider/3.0|YoudaoBot|Tomato‘)
{
      return 403;
}

技术分享图片

检查配置和加载

[[email protected] ~]# /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
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

模拟测试

[[email protected] ~]# curl -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Wed, 15 Aug 2018 10:12:25 GMT
Content-Type: text/plain
Content-Length: 4
Last-Modified: Wed, 15 Aug 2018 10:12:22 GMT
Connection: keep-alive
ETag: "5b73fc86-4"
Accept-Ranges: bytes
[[email protected] ~]# curl -A "Tomatoalsdkflsd" -x127.0.0.1:80 test.com/upload/1.txt -I     ##模拟user_agent
HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Wed, 15 Aug 2018 10:14:28 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

技术分享图片

Nginx解析php相关配置

技术分享图片

因为现在配置虚拟文件里还不能解析php所以配置

[[email protected] ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

需要添加的内容

location ~ .php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
    }

做个PHP

[[email protected] ~]# vim /data/wwwroot/test.com/3.php
<?php                     ##添加到3.php里边
phpinfo();

做一下测试

[[email protected] ~]# curl -x127.0.0.1:80 test.com/3.php     ##不能解析直接显示源码
<?php
phpinfo();

之前做了配置没有重新加载所以上边的解析没有成功下面加载

[[email protected] ~]# /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
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

**加载完成后在做测试可以解析php

技术分享图片**

解释配置的信息报错502

技术分享图片

技术分享图片

下面把php-fpm.conf配置做下更改我要监听ip端口

技术分享图片

[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload     ##重新加载
[[email protected] ~]# /etc/init.d/php-fpm reload      ##重启php

技术分享图片

Nginx代理

技术分享图片

写个新的配置文件

[[email protected] ~]# cd /usr/local/nginx/conf/vhost/   ##进入vhost
[[email protected] vhost]# vim proxy.conf            ##编辑添加内容
server              添加这些东西
{
    listen 80;
    server_name ask.apelearn.com;

    location /
    {
        proxy_pass      http://121.201.9.155/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

技术分享图片

检查配置和加载

[[email protected] ~]# /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
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

测试

技术分享图片

以上是关于48次课(Nginx防盗链Nginx访问控制Nginx解析php相关配置Nginx代理)的主要内容,如果未能解决你的问题,请参考以下文章

Nginx优化与防盗链

Nginx的防盗链Nginx的访问控制Nginx解析php的配置Nginx代理

四十九Nginx防盗链Nginx访问控制Nginx解析PHP相关配置Nginx代理

nginx:默认网站 访问控制 登录验证 日志管理 防盗链

Nginx防盗链 Nginx访问控制 Nginx解析php相关配置 Nginx代理

Nginx防盗链 Nginx访问控制 Nginx解析php相关配置 Nginx代理