#############################################################################

我们在使用的时候会遇到很多的恶意IP攻击,这个时候就要用到Nginx 禁止IP访问了。下面我们就先看看Nginx的默认虚拟主机在用户通过IP访问,或者通过未设置的域名访问(比如有人把他自己的域名指向了你的ip)的时 候生效最关键的一点是,在server的设置里面添加这一行:
listen 80 default;
后面的default参数表示这个是默认虚拟主机。
Nginx 禁止IP访问这个设置非常有用。
比如别人通过ip或者未知域名访问你的网站的时候,你希望禁止显示任何有效内容,可以给他返回500.目前国内很多机房都要求网站主关闭空主机头,防止未备案的域名指向过来造成麻烦。就可以这样设置:
server {
listen 80 default;
return 500;
}
也可以把这些流量收集起来,导入到自己的网站,只要做以下跳转设置就可以:
server {
listen 80 default;
rewrite ^(.*) http://www.example.com permanent;
}
按照如上设置后,确实不能通过IP访问服务器了,但是在应该用中出现当server_name后跟多个域名时,其中一个域名怎么都无法访问,设置如下:
server {
listen 80;
server_name www.example.com example.com
}
没更改之前,通过server_name 中的www.example.com example.com均可访问服务器,加入Nginx 禁止IP访问的设置后,通过example.com无法访问服务器了,www.example.com可以访问,用 Nginx -t 检测配置文件会提示warning:
[warn]: conflicting server name “example.com” on 0.0.0.0:80,
ignored
the configuration file /usr/local/Nginx/conf/
Nginx.conf syntax is ok
configuration file /usr/local/Nginx/conf/Nginx.
conf test is successful
最后通过在listen 80 default;后再加server_name _;解决,形式如下:

#禁止IP访问
server {
listen 80 default;
server_name _;
server_name www.example.com example.com
return 500;
}
这样,通过example.com就能访问服务器了。

#########################################################################################

nginx 的proxy_intercept_errors 和error_page的搭配使用,

proxy_intercept_errors

当上游服务器响应头回来后,可以根据响应状态码的值进行拦截错误处理,与error_page 指令相互结合。用在访问上游服务器出现错误,由nginx直接返回自定义的错误页面

location /houtai {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/houtai;
proxy_intercept_errors on;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

location = /404.html {
root /usr/share/nginx/html;
internal;
}

location = /50x.html {
root /usr/share/nginx/html;
internal;

参考页面:https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-to-use-custom-error-pages-on-ubuntu-14-04

http://blog.csdn.net/hellolingyun/article/details/37934815

#######################################################################

对于有些服务端接口返回是固定值的json,可通过配置nginx直接返回json,减少程序的加载对资源的占用,减少接口响应时间
通过return,指定页面返回的内容
 location ~* (request/update)$ {
     default_type application/json;
     return 200 '{"update":"no"}';
 }
记得加default_type application/json ,不然浏览器打开时,是个文件。

#########################################################################

nginx 代理https后,应用redirect https变成http

原因分析:

浏览器到nginx是https,nginx到应用服务器变成http,

应用服务器,再做302 redirect的时候,返回的redirect 地址就好变成http的地址;

原因是spring mvc的servlet的secheme取值,request.getScheme()

是取请求里的一个scheme值,所有这个值在nginx代理时要设置成https

其中: request.getScheme() return http but not https.

解决方法:

目前是

proxy_redirect http:// $scheme://;

The same server needs to serve both HTTP and HTTPS, however, when the upstream issues a redirect (for instance, after a form is processed), all HTTPS requests are redirected to HTTP. The only thing I have found that will correct this issue is changing proxy_redirect to the following:

proxy_redirect http:// https://;

That works wonderfully for requests coming from HTTPS, but if a redirect is issued over HTTP it also redirects that to HTTPS, which is a problem.

Out of desperation, I tried:

if ($scheme = 'https') {
proxy_redirect http:// https://;
}

########################################################################

网站它可能不希望被网络爬虫抓取,例如测试环境不希望被抓取,以免对用户造成误导,那么需要在该网站中申明,本站不希望被抓取。有如下方法:

server {

listen 80;

server_name 127.0.0.1;

#添加如下内容即可防止爬虫

if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") 

return 403;

}

#########################################################################

Nginx设置HTTP认证:

1、安装htpassword来创建和生成加密的用户用于基础认证(Basic Authentication)
    sudo apt-get install apache2-utils

2、在Nginx托管的网站目录下生成一个.htpasswd文件。如下的命令可以创建文件同步增加用户和加密的密码到文件中

     sudo htpasswd -c /etc/nginx/.htpasswd exampleuser
3、网站的Nginx配置文件增加如下两行:

auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;

ngnix之笔记的更多相关文章

  1. Ngnix学习笔记

    一.Ngnix介绍 1.概念 一个强大的Web服务器软件. 2.功能 1)处理高并发的http请求. 2)作为反向代理服务器来进行负载均衡. 3)数据压缩和解压缩处理 3.优势 高性能,轻量级,内存消 ...

  2. ngnix笔记

    ngnix可通过-s 参数控制,如quit正常退出:reload重载配置文件,具体参考:http://nginx.org/en/docs/switches.html ngnix的指令解释请参考这里:h ...

  3. Web负载均衡学习笔记之K8S内Ngnix微服务服务超时问题

    0x00 概述 本文是从K8S内微服务的角度讨论Nginx超时的问题 0x01 问题 在K8S内部署微服务后,发现部分微服务链接超时,Connection Time Out. 最近碰到了一个 Ngin ...

  4. django学习笔记

    django笔记----建立疾病预测系统的web应用                            @buyizhiyou               工作环境:ubuntu16.04LTS+ ...

  5. Ngnix 安装、信号量、虚拟主机配置

    ngnix的安装很简单 1.先从ngnix官网下载ngnix压缩包 wget http://nginx.org/download/nginx-1.6.2.tar.gz 2.解压并进入其目录 tar - ...

  6. 开源项目SuperSocket的学习笔记

    近几日想在一个项目中引进一个Socket Server,用来接收客户端发送的命令消息并根据具体的业务逻辑对消息进行处理,然后转发给其它在线的客户端.因为以前在博客园关注过江大渔开源的SuperSock ...

  7. 基于windows的ngnix基础使用

    前言 今天组长一大早心血来潮的跟我说,我希望我们小组电脑做web站点的服务器集群,你搞一搞,就用ngnix吧. 君要臣死,臣不得不死.顺便写个文章做个笔记. 简介 Nginx 是一个高性能的HTTP和 ...

  8. Nginx学习笔记4 源码分析

    Nginx学习笔记(四) 源码分析 源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_ ...

  9. nginx笔记5-双机热备原理

    1动静分离演示: 将笔记3的Demo改造一下,如图所示: 改造完成后,其实就是在网页上显示一张图片 现在启动Tomcat运行起来,如图: 可以看到图片的请求是请求Tomcat下的图片. 现在,通过把静 ...

随机推荐

  1. 多线程基础知识---join方法

    join方法的作用 thread.join()方法用于把指定的线程加入到当前线程中,把当前线程的CPU执行时间让给另一个线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继 ...

  2. 网络流三大算法【邻接矩阵+邻接表】POJ1273

    网络流的基本概念跟算法原理我是在以下两篇博客里看懂的,写的非常好. http://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html http://www.cnb ...

  3. SpringBoot默认日志的使用方法及常用配置

    SpringBoot默认采用slf4j+logback 的组合形式,但也提供对JUL.log4j2.Logback提供了默认配置. 我们使用IDEA的spring初始化创建一个springboot项目 ...

  4. poj3122 Pie (二分)

    题目链接:https://vjudge.net/problem/POJ-3122 题意:有n块饼,m+1个人,将饼均分成m+1块,求每块最大的大小. 思路:水二分,显然每块的大小与可以给多少人吃具有单 ...

  5. 【hash】珍珠

    [来源] https://loj.ac/problem/2427 [参考博客] LOJ#2427. 「POI2010」珍珠项链 Beads [题解]: 复杂度计算: 暴力枚举k每次计算是n/2+n/3 ...

  6. Docker——桥接网络配置

    [root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# ls ifcfg-et ...

  7. 10.Bash的安装

    10.Bash的安装本节提供了在 Bash支持的不同系统上的基本安装指导.本版本支持 GNU操作系统,几乎每个 UNIX版本,以及几个非 UNIX 系统,例如 BeOS 和 Interix.还有针对 ...

  8. java lesson14Homework

    /** * 1. 本程序找出字符串数组 String[] arr = {“welcome”, “china”, “hi”, “congratulation”, “great”} 中的长度最大的元素,并 ...

  9. C#Linq之求和,平均值,最大值,最小值

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  10. .NET监视程序运行时间

    使用Stopwatch类(命名空间:System.Diagnostics;) 示例: using System; using System.Collections.Generic; using Sys ...