nginx?攔截指定ip訪問指定url的實現(xiàn)示例
這里需要注意的是一定要用$http_x_forwarded_for 這個變量
upstream myapp1 { # 定義一個名為myapp1的服務(wù)器組 server backend1.example.com weight=5; # 添加一個服務(wù)器,并設(shè)置權(quán)重為5 server backend2.example.com; # 添加另一個服務(wù)器,權(quán)重默認為1 server backend3.example.com down; # 將此服務(wù)器標記為down,不參與負載均衡 server backup1.example.com backup; # 將此服務(wù)器作為備份服務(wù)器 } location ^~ /api/ { #10\.182\.(?!25\.|26\.)[0-9.]+ if ($http_x_forwarded_for ~ "^10\.182\.(25\.|26\.)[0-9.]+") { # 如果是,返回403禁止訪問 return 403; } proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass myapp1; }
禁止指定網(wǎng)站來訪:
if ($http_referer ~* "要攔截的域名") { return 301 要跳轉(zhuǎn)的域名; }
限制指定目錄擴展名后綴
location ~ ^/images/.*\.(php|php5|sh|pl|py)$ { deny all; } location ~ ^/static/.*\.(php|php5|sh|pl|py)$ { deny all; }
禁止直接訪問txt和doc文件
location ~* \.(txt|doc)$ { if (-f $request_filename) { root /data/www/www; rewrite ^(.*) https://www.itbulu.com/ break; #可以重定向到某個URL; } } location ~* \.(txt|doc)$ { root /data/www/www; deny all; }
禁止訪問文件和目錄
#禁止訪問的文件或目錄 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; }
排除某個目錄不受限制
location ~ \.well-known{ allow all; }
禁止訪問單個目錄的命令
location ~ ^/(static)/ { deny all; } location ~ ^/static { deny all; }
禁止訪問多個目錄的配置
location ~ ^/(static|js) { deny all; }
禁止目錄讓外界訪問
location ~ ^/mysql_loging/ { allow 192.168.1.4; deny all; } location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } 說明:該配置只允許192.168.1.4IP訪問mysql_loging目錄
限制IP和IP段
location / { deny 192.168.0.4; allow 192.168.1.0/16; allow 10.0.0.0/24; deny all; } 說明:此限制是對某些IP做整個網(wǎng)站的限制訪問。
非指定域名訪問跳轉(zhuǎn)
if ($host !~ ^www/.itbulu/.com$) { rewrite ^(.*) http://www.baidu.com$1 permanent; }
到此這篇關(guān)于nginx 攔截指定ip訪問指定url的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)nginx攔截指定ip內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx、Apache、Lighttpd禁止目錄執(zhí)行php配置示例
這篇文章主要介紹了Nginx、Apache、Lighttpd禁止目錄執(zhí)行php配置示例,本文給出了單個目錄、多個目錄的禁止執(zhí)行PHP的方法,需要的朋友可以參考下2014-09-09CentOS 7.0下nginx實現(xiàn)每天定時分割日志
大家都知道Nginx產(chǎn)生的日志都是存在一個文件,隨著網(wǎng)站運行時間越長,日志文件的大小也在不斷增長,所以這個時候就需要實現(xiàn)定時分割,這篇文章主要介紹了在CentOS 7.0下nginx實現(xiàn)每天定時分割日志的相關(guān)資料,需要的朋友可以參考下。2017-04-04nginx支持codeigniter的pathinfo模式url重寫配置寫法示例
這篇文章主要介紹了nginx支持codeigniter的pathinfo模式url重寫配置寫法示例,pathinfo模式是一種開發(fā)框架都愛用的路由模式,需要的朋友可以參考下2014-07-07Nginx 配置TCP代理轉(zhuǎn)發(fā)的實現(xiàn)
本文主要介紹了使用Nginx新版的stream方式,實現(xiàn)TCP/UDP代理轉(zhuǎn)發(fā),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-10-10