且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-08-14 19:02:48

Nginx防盗链




1、[root@centos7 test.com]# vi /usr/local/nginx/conf/vhost/test.com.conf 

#+表示1或者多个,+前面的字符 

    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 ;

    #定义referer白名单

    if ($invalid_referer) {

        return 403;

    #if函数的意思是:如果不是白名单内的域名,返回值:403

    }

    access_log off;

}



验证:

curl -e:指定referer链接


[root@centos7 test.com]# curl -e "http://123123asdsd.test.com/asdas" -x127.0.0.1:80 -I test.com/baidu.png

HTTP/1.1 200 OK

Server: nginx/1.12.1


使用非白名单内的referer进行访问,被拒绝。


Nginx访问控制

需求:访问admin目录,只能允许几个指定IP可以访问,其他禁止

1、[root@centos7 test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf 


server

{

    listen 80;

    server_name test.com  test2.com test3.com;

    index index.html index.htm index.php;

    access_log /tmp/test.com.log combined_realip;

    root /data/wwwroot/test.com;

    location /admin/

    {

    allow 192.168.3.74;

    allow 127.0.0.1;

    deny all;

    #从上至下的执行权限

    }

验证:

[root@centos7 test.com]# curl -x127.0.0.1:80  test.com/admin/admin.html

“admin root”

[root@centos7 test.com]# curl -x192.168.3.74:80  test.com/admin/admin.html

“admin root”


Nginx访问控制

可以匹配正则

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

{

        deny all;

}

根据user_agent限制

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

{

      return 403;

}

 deny all和return 403效果一样


Nginx解析php相关配置


location ~ \.php$

        #匹配以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;

            #这里的路径要和root路径一致

        }



Nginx代理

工作模式:比如当用户访问真实主机192.168.1.100:8080时,不直接访问,通过nginx代理http://test.com访问,然后nginx跳转到真实主机上去,实现了nginx代理访问


1、

[root@centos7 vhost]# vi /usr/local/nginx/conf/vhost/tomcat.conf


server

{

    listen 80;

    server_name www.test-tomcat.com;

    #定义域名

    location /

    {

        proxy_pass      http://192.168.3.83:8080;

        #指定被代理(被访问)的IP(web服务器IP)

        proxy_set_header Host   $host;

        #$host指的是代理服务器的servername(也是被代理IP的域名)

        proxy_set_header X-Real-IP      $remote_addr;

#这个是在web端获取真实的IP,其中这个X-Real-IP是一个自定义的变量名,名字可以随意取

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

}


验证是否跳转:

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










本文转自 iekegz 51CTO博客,原文链接:http://blog.51cto.com/jacksoner/1981991,如需转载请自行联系原作者