天天看點

Nginx防盜鍊、Nginx通路控制、Nginx解析php相關配置、Nginx代理

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

可以比對正則

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

驗證是否跳轉:

<a href="https://s3.51cto.com/oss/201711/16/00701815d1a1b63b01532e44179951d3.png-wh_500x0-wm_3-wmp_4-s_3784558461.png" target="_blank"></a>

本文轉自 jiekegz  51CTO部落格,原文連結:http://blog.51cto.com/jacksoner/1981991