[nginx]站点目录及文件访问控制
nginx.conf配置文件
http ->多个server -> 多个location ->可限制目录和文件访问(根据i扩展名限制或者rewrite.)

根据目录或扩展名,禁止用户访问指定数据信息
禁止访问目录下的某些扩展名文件
这次我测一下,禁止访问网站目录下的 html/images/*.txt文件
[root@n1 nginx]# tree html/
html/
├── 50x.html
├── images
│ └── maotai.txt
└── index.html

- 设置禁止访问
location ~ ^/images/.*\.(txt|php|php5|sh|pl|py|html)$
{
deny all;
}

- 日志查看
- access.log: 允许访问
192.168.2.1 - - [11/Mar/2018:10:58:06 +0800] "GET /images/maotai.txt HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
- access.log: 禁止访问
192.168.2.1 - - [11/Mar/2018:10:59:10 +0800] "GET /images/maotai.txt HTTP/1.1" 403 563 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
- error.log
2018/03/11 10:59:10 [error] 28357#0: *16 access forbidden by rule, client: 192.168.2.1, server: localhost, request: "GET /images/maotai.txt HTTP/1.1", host: "192.168.2.11"
附录: nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~ ^/images/.*\.(txt|php|php5|sh|pl|py|html)$
{
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
当访问禁止的数据信息时,进行页面跳转(rewrite)
访问http://www.maotai.com/images/1.png -> http://www.baidu.com/images/1.png
location ~* \.(txt|doc)$ {
if (-f $request_filename){
root html/images/;
#rewrite …..可以重定向到某个URL
rewrite ^/(.*) http://www.baidu.com/$1 permanent;
break;
}
}
根据IP地址或网络进行访问策略控制
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
deny all;
}
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~* \.(txt|doc)$ {
if (-f $request_filename){
root html/images/;
#rewrite …..可以重定向到某个URL
rewrite ^/(.*) http://www.nmtui.com/$1 permanent;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
采用if判断方式,进行访问控制
if ($remote_addr = 192.168.2.1) {
return 403;
}
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
if ($remote_addr = 192.168.2.1) {
return 403;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[nginx]站点目录及文件访问控制的更多相关文章
- nginx站点目录及文件URL访问控制
一.根据扩展名限制程序和文件访问 利用nginx配置禁止访问上传资源目录下的PHP.Shell.Perl.Python程序文件. 配置nginx,禁止解析指定目录下的指定程序. location ~ ...
- Nginx的站点目录及文件URL的访问控制
1.根据扩展名限制程序和文件访问: web2.0时代,绝大多数网站都是以用户为中心的,这些产品有一些共同点,就是不允许用户发布内容到服务器,还允许用户发图片甚至附件上传到服务器上,给用户开启了上传的功 ...
- nginx——优化 Nginx 站点目录
1. 禁止解析指定目录下的指定程序 location ~ ^/data/.*.(php|php5|sh|pl|py)$ { # 根据实际来禁止哪些目录下的程序,且该配置必须写在 Nginx 解析 PH ...
- Nginx 站点设置目录列表显示
Nginx 站点目录列表显示. 可以编辑添加在 server { } 模块 或者 location { } 模块下. autoindex on; # 开启目录文件列表 autoindex_exact_ ...
- nginx+php使用open_basedir限制站点目录防止跨站
以下三种设置方法均需要PHP版本为5.3或者以上.方法1)在Nginx配置文件中加入 fastcgi_param PHP_VALUE "open_basedir=$document_root ...
- tomcat相关配置技巧梳理 (修改站点目录、多项目部署、限制ip访问、大文件上传超时等)
tomcat常用架构:1)nginx+tomcat:即前端放一台nginx,然后通过nginx反向代理到tomcat端口(可参考:分享一例测试环境下nginx+tomcat的视频业务部署记录)2)to ...
- Nginx+Php中限制站点目录防止跨站的配置方案记录
Nginx+Php中限制站点目录防止跨站的配置方案记录(使用open_basedir)-------------------方法1)在Nginx配置文件中加入: 1 fastcgi_param PH ...
- nginx其他目录下上传站点
1.查看主配置文件 [root@bogon ~]# cat /etc/nginx/nginx.conf user root root; worker_processes auto; worker_rl ...
- web站点放在nginx其他目录下
web站点放在nginx其他目录下 .查看主配置文件 [root@bogon mysql]# cat /etc/nginx/nginx.conf user root root; worker_proc ...
随机推荐
- V-rep学习笔记:外部函数调用方式
The remote API functions are interacting with V-REP via socket communication in a way that reduces l ...
- django之创建第3个项目:编写第一个模板文件
1.django结构 2.在站点blog下创建templates文件夹,专门用于存放模板文件 3.在templates文件夹下创建index.html文件 #index.html <!DOCTY ...
- Redis从入门到精通:中级篇(转)
原文链接:http://www.cnblogs.com/xrq730/p/8944539.html,转载请注明出处,谢谢 本文目录 上一篇文章以认识Redis为主,写了Redis系列的第一篇,现在开启 ...
- vijos 1006 晴天小猪历险记之Hill——数字三角形的终极变化
题目链接:https://vijos.org/p/1006 数字三角形原题看这里:http://www.cnblogs.com/huashanqingzhu/p/7326837.html 背景 在很久 ...
- 一颗可靠的时间胶囊:苹果AirPort Time Capsule测评
http://sspai.com/24181/ 如何从 Time Machine 备份恢复数据? AirPort Time Capsule能轻松完成备份,自然也少不了方便地恢复备份.一般常见的恢复备份 ...
- Mybaits 使用多数据库源错误 --MapperScannerConfigurer配置
我在配置文件里面配置了一个数据源,数据源参数是根据配置文件加载.数据源在mybaits和自定义数据工具类中使用,但是启动工程后,报错如下: ### Cause: org.springframewor ...
- [转载]Ubuntu 安装 万能五笔 输入法
原文地址:Ubuntu 安装 万能五笔 输入法作者:庖丁解牛 paul@paul-desktop:~/scripts$ cat ins-ibus-wnwb.sh #!/bin/sh set -e cd ...
- HDUOJ-----A == B ?
A == B ? Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- UCCI协议[转]
象棋百科全书 中国象棋电脑应用规范(五) 中国象棋通用引擎协议 版本:3.0 象棋百科全书网 (webmaster@xqbase.com) 2004年12月初稿,2007年11月修订 一.概述 中国象 ...
- NSOperation, NSOperationQueue 原理探析
通过GNUstep的Foundation来尝试探索下NSOperation,NSOperationQueue 示例程序 写一个简单的程序 - (void)viewDidLoad { [super vi ...