---
title: "Nginx服务器拒绝非GET方式请求保障安全性"
author: "sichunli_030"
date: "2017-03-06T22:27:47Z"
tags: []
categories: ["开源软件"]
slug: "nginx-get"
draft: false
lastmod: "2017-03-06T22:27:47Z"
id: 2360616
url: "https://rd-030.iteye.com/blog/2360616"
---

upstream tomcat {
ip_hash;
server 192.168.2.187:8080;
}
location ~* /html {
if ($request_method = PUT ) {
return 403;
}
if ($request_method = DELETE ) {
return 403;
}
if ($request_method = POST ) {
return 403;
}
proxy_method GET;
proxy_pass http://tomcat;
}
当路径包含/html的时候，则代理到server后端进行请求数据。这里屏蔽了PUT,DELETE,POST方法，只是使用了GET,主要目的是为了安全性，因为DELETE,POST,PUT是可以修改数据的。
或者：
limit_except GET {
allow 192.168.1.1; Linux学习，http:// linux.it.net.cn
deny all;
}
if ($request_filename ~ /test/index.html) {
# return 404;
rewrite ^/(.*) /index.html;                                                                        }; Linux学习，http:// linux.it.net.cn
nginx禁止访问txt|doc文件                              
方法一：全局设置，禁止访问任何以后缀txt|doc的文件
        location ~* \.(txt|doc)$ {
        deny all;
        }
方法二：只禁止访问某目录下的txt|doc
        location ~* \.(txt|doc)$ {
        if (-f $request_filename) {

        root html/job;
        break;
         }
   }
nginx禁止某中浏览器访问：#浏览器类型可从日志得知。
server
       {
               listen       80;
               server_name  test.domain.com;
               index index.php index.html;
               root   /opt/nginx/html/; 
               if ( $http_user_agent ~* "MSIE 6.0" ) {
               return 403;
                }
设置目录执行权限                                                                                  
在windows+iis下，可以设置上传目录，类似:upload,uploadfile,attachments，这样的目录下面无脚本执行权限，从而防止非法用户上传脚本得到webshell
nginx上也很简单，我们使用location 如下：
location ~ ^/upload/.*\.(php|php5)$
{
deny all;
}
其中upload换为你要设置的目录名字
这条规则的含义是匹配请求连接中开头是/upload/，中间匹配任意字符，结尾匹配.php或者.php5的页面，最后利用deny all禁止访问，这样就防止了上传目录的脚本执行权限

参考：<http://linux.it.net.cn/e/server/nginx/2015/0110/11642.html>