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

测试

原文地址:http://blog.51cto.com/8043410/2160488

时间: 2024-10-28 09:24:24

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

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

nginx防盗链 配置如下,可以和不记录静态文件配置结合起来 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; #不过不是白名单的refer就403 } access_log

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

Nginx防盗链 编辑虚拟主机配置文件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) { ret

LNMP(nginx防盗链,访问控制,解析php相关配置,Nginx代理,常见502问题)

一.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 ;      

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

一.Nginx的防盗链在配置文件里面增加以下代码:(/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; }

2018-3-1512周4次课 Nginx防盗链、访问控制、配置PHP解析、代理

12.13 Nginx防盗链 [[email protected] test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf ~* 表示不区分大小写 白名单 *.test.com,如果不是白名单,则返回403 [[email protected] test.com]# curl -e "http://www.baidu.com"-x127.0.0.1:80 test.com/1.gif -I HTTP/1.1 403 Forbid

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

12.13 Nginx防盗链cd /usr/local/nginx/conf/vhostvi test.com.conf将以上内容复制到下图位置测试,成功前提data/wwwroot/test.com目录下要有1.gif12.14 Nginx访问控制cd /usr/local/nginx/conf/vhostvi test.com.confFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=" alt="Nginx

Nginx配置:防盗链、访问控制、解析PHP以及代理

一.Nginx防盗链 防盗链是指一个网站的资源(图片或附件)未经允许在其它网站提供浏览和下载,尤其热门资源的盗链,对网站带宽的消耗非常大,设置防盗链以节省资源. 1.修改虚拟主机配置文件 [[email protected] vhost]# vim linuxtest.conf server { listen 80; server_name linuxtest.com; index index.html index.htm index.php; root /data/wwwroot/linuxt

Nginx防盗链以及访问控制,Nginx解析php配置和代理

Nginx防盗链 1.编辑配置文件: [[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_refer

92.Nginx配置:防盗链、访问控制、解析PHP以及代理

一.Nginx防盗链 防盗链是指一个网站的资源(图片或附件)未经允许在其它网站提供浏览和下载,尤其热门资源的盗链,对网站带宽的消耗非常大,设置防盗链以节省资源. 1.修改虚拟主机配置文件 [[email protected] vhost]# vim linuxtest.conf server { listen 80; server_name linuxtest.com; index index.html index.htm index.php; root /data/wwwroot/linuxt

Nginx防盗链与访问控制

防盗链 1.编辑配置文件 [[email protected] ~]# vi /usr/local/nginx/conf/vhost/default.conf server { listen 80 default_server; server_name aaa.com; index index.html index.htm index.php; root /data/wwwroot/default; access_log /tmp/default.log juispan; location ~*