nginx 的 一些配置说明
安装默认包
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
80跳 443 301跳转
以下这段需要写在service{}里面
#301跳转
if ($server_port = 80 ) {
return 301 https://$host$request_uri;
}
就是这样
cat default.conf
server {
listen 80 default;
listen 443 ssl default;
server_name _;
ssl_certificate crt/......crt;
ssl_certificate_key crt/.......key;
access_log logs/default.log main;
#301跳转
if ($server_port = 80 ) {
return 301 https://$host$request_uri;
} location / {
return 403;
}
}
也可以写成这样
cat default.conf
server {
listen 80 default_server;
server_name _;
access_log /home/log.log; location / {
return 301 https://$host$request_uri;
} }
default 配置
参考
https://www.cnblogs.com/kuku0223/p/10740735.html
设置了default 除了指定的域名, 如果是没有配置的域名解析过来。不会乱跳。 可以直接使没有配置的域名跳过来变 404
1、nginx 隐式的 default server
http {
# 如果没有显式声明 default server 则第一个 server 会被隐式的设为 default server
server {
listen 80;
server_name _; # _ 并不是重点 __ 也可以 ___也可以
return 403; # 403 forbidden
} server {
listen 80;
server_name www.a.com;
...
} server {
listen 80;
server_name www.b.com;
...
}
}
很多人复制粘贴广泛传播 server_name 要设为 '_',其实一毛钱的关系也没有。'_' 只是作为一个和业务域名无关的请求回收服务而已,如果我们线上的业务都是明确的业务域名访问,那泛解析造成的一些非业务域名或ip访问都会被这个 sever 回收处理。
2、显示的定义一个 default server
http {
server {
listen 80;
server_name www.a.com;
...
} server {
listen 80;
server_name www.b.com;
...
} # 显示的定义一个 default server
server {
listen 80 default_server;
server_name _;
return 403; # 403 forbidden
} }
3、直接指定server_name 为 ip(只能禁止ip访问)
http {
server {
listen 80;
server_name www.a.com;
...
} server {
listen 80;
server_name www.b.com;
...
} # 直接指定 ip server_name
server {
listen 80;
server_name xxx.xxx.xxx.xxx;
return 403; # 403 forbidden
} }
测试:
可以使用本机的 host做测试,写三个server。 只配置 2个域名。
server {
listen 80 ;
server_name aaa.com;
root /usr/share/nginx/html; location / {
} } server {
listen 80 ;
server_name bbb.com;
root /usr/share/nginx/html; location / {
} } server {
listen 80 default_server;
server_name _;
return 403; # 403 forbidden
}
本地host写死
访问 aaa.com 和 bbb.com 都没有问题。 但是访问ccc.com 会直接报403。因为 只配置aaa.com 和 bbb.com2个域名。 其他域名都访问不过来
限制非本机配置域名, 但是不限制ip访问
我们要再配置一段ip的server
[root@localhost vhost]# sudo cat default.conf
# 配置2个域名
server {
listen 80 ;
server_name aaa.com
bbb.com;
location / {
root html;
index index index.jsp index.htm index.html;
} } # 将本机ip当做 servername 来配置 server {
listen 80 ;
server_name 192.168.144.174;
location / {
root html;
index index index.jsp index.htm index.html;
} } server {
listen 80 default_server;
server_name _;
return 403;
}
本地写host
本机只配置了
aaa.com 和 bbb.com
但是没有配置 ccc.com
然后查看
aaa 和 bbb 能正常访问
ip可以正常访问
但是 ccc 不行
然后将 ip那段注释掉,ip就不能访问了
[root@localhost vhost]# cat default.conf
server {
listen 80 ;
server_name aaa.com
bbb.com;
location / {
root html;
index index index.jsp index.htm index.html;
} } # 注释掉这段
#server {
# listen 80 ;
# server_name 192.168.144.174;
# location / {
# root html;
# index index index.jsp index.htm index.html;
# }
#
#} server {
listen 80 default_server;
server_name _;
return 403;
}
然后reload nginx后。
查看 。 ip访问 就被挡住了。
80 转 443
#301跳转
if ($server_port = 80 ) {
return 301 https://$host$request_uri;
}
如果找不到请求的页面,就返回首页
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?s=/$1 last;
}
}
支持 http2
nginx 配置支持http2
server {
listen ssl http2;
ssl_certificate xxxxx/xxxx.crt;
ssl_certificate_key xxxxx/xxxx.key;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
access_log xxx/aaa.log main;
location / { index index.jsp index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
若出现报错:
ERR_SPDY_INADEQUATE_TRANSPORT_SECURITY
是因为nginx默认是
ssl_ciphers HIGH:!aNULL:!MD5;
需要改成
EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
限制 ../的域名路径
这个需要注意, 这个if 需要现在server里面, 不能写在location里面。 因为 ../默认就是返回上级。 location里面会地址解析掉 。 server里面就是未解析的地址。
if ( $request ~ .*\.\..* ) {
return 403;
}
只能使用指令行 curl。 浏览器会自动解析的。
curl https://xxxx.com/vim/Memberday/../../index.jsp -H 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 unknown BingWeb/6.9.8.1'
限制ip访问频率的
参考
https://blog.csdn.net/hellow__world/article/details/78658041
http://nginx.org/en/docs/http/ngx_http_limit_req_module.html
当有大量ip特殊刷域名或接口时,可以使用此证方法
使用
ngx_http_limit_req
基础配置
imit_req_zone $binary_remote_addr zone=one:10m rate=,10r/m; server { location /search/ { limit_req zone=one burst=5 nodelay; }
简单解释下。
第一段的zone配置,定义策略,注意此段如果是在nginx.conf的文件里面, 需要写在service的外层 , 就是http{} 里面定义就行了。 如果是配置的虚拟主机,需要另起一个文件。然后include里面,包含就可以了。
在nginx的配置中 http===> server===>location
$binary_remote_addr :表示通过remote_addr这个标识来做限制,“binary_”的目的是缩写内存占用量,是限制同一客户端ip地址
zone=one:10m:表示生成一个大小为10M,名字为one的内存区域,用来存储访问的频次信息
rate=10r/m:表示允许相同标识的客户端的访问频次,这里限制的是没分钟10次,即每分钟只处理十个请求,相当于6秒一次。还可以有比如30r/m的,即限制每2秒访问一次,即每2秒才处理一个请求。
第二段location配置, 配置限制的接口路径
zone=one :设置使用哪个配置区域来做限制,与上面limit_req_zone 里的name对应
burst=5:重点说明一下这个配置,burst爆发的意思,这个配置的意思是设置一个大小为5的缓冲区当有大量请求(爆发)过来时,超过了访问频次限制的请求可以先放到这个缓冲区内等待,但是这个等待区里的位置只有5个,超过的请求会直接报503的错误然后返回。
nodelay:
- 如果设置,会在瞬时提供处理(burst + rate)个请求的能力,请求超过(burst + rate)的时候就会直接返回503,永远不存在请求需要等待的情况。(这里的rate的单位是:r/s)
- 如果没有设置,则所有请求会依次等待排队
实际演示下,查看下配置文件
[root@localhost vhost]# pwd
/opt/nginx/conf/vhost
[root@localhost vhost]# ls
include limit_req new1.conf
[root@localhost vhost]# cat include
# include vhost/upstream;
include vhost/*.conf;
include vhost/limit_req;
include vhost/*/*.conf;
[root@localhost vhost]# cat new1.conf
server {
listen 80;
server_name _;
access_log /opt/nginx/logs/new_access.log main;
location / {
root html;
index index index.jsp index.htm index.html;
proxy_redirect off;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /AAA/aaa.html { # 配置了一段location,来定义限制的路径
limit_req zone=one ;
root html;
index index index.jsp index.htm index.html;
proxy_redirect off;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } [root@localhost vhost]# cat limit_req # zone的文件要写在server的外层, 所以这边定义在另一个文件里面了
limit_req_zone $binary_remote_addr zone=one:10m rate=5r/m;
第一种情况 不加 nodelay和 burst
这边先不配置 nodelay和 burst的
[root@localhost vhost]# cat new1.conf
server {
listen 80;
server_name _;
access_log /opt/nginx/logs/new_access.log main;
location / {
root html;
index index index.jsp index.htm index.html;
proxy_redirect off;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /AAA/aaa.html {
limit_req zone=one ;
root html;
index index index.jsp index.htm index.html;
proxy_redirect off;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
然后我们使用ab工具测试
ab工具参数,可以参考
https://blog.csdn.net/jianghulangzhongshen/article/details/99866873
ab -n 10 -c 10 http://127.0.0.1/AAA/aaa.html
请求数 并发数
查看,我们可以看到,成功了一次,失败了9次。 测试完成在0.1秒内。
查看日志,就是第一次成功了。后面都是503,因为大并发的时候,虽然是 1分钟10次。但是相当于6s一次。所以后面都失败了。
第二种情况, 加上 burst=5
这边加上 burst=5
server {
listen 80;
server_name _;
access_log /opt/nginx/logs/new_access.log main;
location / {
root html;
index index index.jsp index.htm index.html;
proxy_redirect off;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /AAA/aaa.html {
limit_req zone=one burst=5 ;
root html;
index index index.jsp index.htm index.html;
proxy_redirect off;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
reload 之后测试,然后查看。
这次测试时间超过一分钟 , 然后成功了5次。 这是因为 burst
定义了一个缓存区域, 大并发是多的请求到缓存区等待。
我们看日志,当并发量大,超过缓存区的请求 会报503. 然后会有请求等待。 之后就会有成功。 499是客户端主动断开。
nginx的 报错代码 可以参考:
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
https://www.nginx.com/resources/wiki/extending/api/http/
第三种情况, 我们加上 nodelay
nodelay 就是只直接处理 burst
的内的请求。无需等待。
server {
listen 80;
server_name _;
access_log /opt/nginx/logs/new_access.log main;
location / {
root html;
index index index.jsp index.htm index.html;
proxy_redirect off;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /AAA/aaa.html {
limit_req zone=one burst=5 nodelay;
root html;
index index index.jsp index.htm index.html;
proxy_redirect off;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
reload 后 ,使用ab工具查看
看时间,瞬间完成。 然后失败了6次。
查看日志,前面4次都是成功的, 之后才是失败的。 是因为无需等待,直接就处理 brust的内容, 之后的请求都是超出限制的,直接就503了。
第四种情况 只加nodeplay
直加nodelay ,没有burst。
[root@localhost vhost]# cat new1.conf
server {
listen 80;
server_name _;
access_log /opt/nginx/logs/new_access.log main;
location / {
root html;
index index index.jsp index.htm index.html;
proxy_redirect off;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /AAA/aaa.html {
limit_req zone=one nodelay;
root html;
index index index.jsp index.htm index.html;
proxy_redirect off;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
reload 之后测试。查看接口,和第一种情况是一样的结果。
查看日志,和第一种情况 一样。
以上是主要的测试结果。
nginx 的 一些配置说明的更多相关文章
- Nginx location相关配置说明
Nginx location相关配置说明 基于不同的IP.不同的端口以及不用得域名实现不同的虚拟主机,依赖于核心模块ngx_http_core_module实现. 新建PC web站点 [ ...
- Nginx负载均衡配置说明
WEB服务做负载均衡的方法有很多种,但使用Nginx做负载均衡部署毫无疑问是非常高效也是非常流行的一种. 本人大多数做.NET开发,但部署负载却一直用Nginx,对其他的负载方式研究不多,只测试过一次 ...
- Nginx模块开发与架构解析(nginx安装、配置说明)
第一章 研究nginx前的准备工作 Linux操作系统需要2.6及其以上的内核(支持epoll) 使用nginx的必备软件 Linux内核参数优化方案 安装nginx 控制nginx 第二章 ngin ...
- 【nginx】详细配置说明
———————————————————————相关文章———————————————————————————— Centos之安装Nginx及注意事项 [Linux]nginx常用命令 ——————— ...
- nginx 超时时间配置说明
做excel文件导入时 报 504 错误 是nginx网关超时导致 下面几个参数貌似没效果,反正我配置不起作用 这是有问题的配置属性 ,注意 于是我换了配置 ,放在http块里 ,配置生效了 #读 ...
- Nginx代理功能与负载均衡详解
序言 Nginx的代理功能与负载均衡功能是最常被用到的,关于nginx的基本语法常识与配置已在上篇文章中有说明,这篇就开门见山,先描述一些关于代理功能的配置,再说明负载均衡详细. Nginx代理服务的 ...
- nginx正向代理,反向代理,透明代理(总结)
1正向代理 正向代理,也就是传说中的代理,他的工作原理就像一个跳板, 简单的说, 我是一个用户,我访问不了某网站,但是我能访问一个代理服务器 这个代理服务器呢,他能访问那个我不能访问的网站 于是我先连 ...
- 【转载】nginx 并发数问题思考:worker_connections,worker_processes与 max clients
注:这个文章主要是作者一直在研究nginx作为http server和反向代理服务器时候所谓最大的max_clients和 worker_connections的计算公式, 其实最后的结论也没有卡上公 ...
- Setup a private http/nginx based GIT server
原文:http://aaba.me/blog/2014/03/setup-a-private-http-nginx-based-git-server.html https://doomzhou.git ...
随机推荐
- C++ class外的==重载,判断相等,测试等于,重载示例。二元操作符
#include <iostream> // overloading "operator == " outside class // == 是二元操作符 /////// ...
- python中实现单例模式
单例模式的目的是一个类有且只有一个实例对象存在,比如在复用类的过程中,可能重复创建多个实例,导致严重浪费内存,此时就适合使用单例模式. 前段时间需要用到单例模式,就称着机会在网上找了找,有包含了__n ...
- POJ 2594 (传递闭包 + 最小路径覆盖)
题目链接: POJ 2594 题目大意:给你 1~N 个点, M 条有向边.问你最少需要多少个机器人,让它们走完所有节点,不同的机器人可以走过同样的一条路,图保证为 DAG. 很明显是 最小可相交路径 ...
- 聊聊Java 虚拟机的“那点事”
本文的使用方法: 这篇文章是一个总结性质的文章,是我在看完<深入理解 Java 虚拟机>后写的(里面可能会有些不准确的地方,欢迎大家指出),本文从头读到尾就是一个虚拟机大部分知识点的框架, ...
- [新概念英语] Lesson 12 : GOODBYE AND GOOD LUCK
Lesson 12 : GOODBYE AND GOOD LUCK New words and expressions : luck (n) 运气 例句 You're not having much ...
- [线段树]Luogu P3373 【模板】线段树 2
#include<cstdio> #include<cstring> #include<algorithm> #define R register #define ...
- [线段树]区间修改&区间查询问题
区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...
- Spring自动注入,类型注入、名称注入(两种方式)
参考: https://blog.csdn.net/qq_41767337/article/details/89002422 https://www.iteye.com/blog/breezylee- ...
- 2019-7-29-win10-UWP-使用-MD5算法
原文:2019-7-29-win10-UWP-使用-MD5算法 title author date CreateTime categories win10 UWP 使用 MD5算法 lindexi 2 ...
- 2019-11-29-win10-uwp-列表模板选择器
原文:2019-11-29-win10-uwp-列表模板选择器 title author date CreateTime categories win10 uwp 列表模板选择器 lindexi 20 ...