first commit

This commit is contained in:
iProbe 2022-10-18 16:59:37 +08:00
commit ba848e218d
1001 changed files with 152333 additions and 0 deletions

Binary file not shown.

View file

@ -0,0 +1,109 @@
***注意本文档nginx版本为1.18.0根据系统中安装的nginx版本下载对应安装包***
## 1、查看nginx版本
```
# 切换到nginx二进制执行文件所在目录
# 假设nginx安装在/usr/local/nginx目录中
cd /usr/local/nginx/
./sbin/nginx -v
```
显示结果类似
```
nginx version: nginx/1.18.0
```
## 2、下载对应版本的nginx
```
wget http://nginx.org/download/nginx-1.18.0.tar.gz -O ~/nginx-1.18.0.tar.gz
```
## 3、下载geoip2识别库、第三方模块及geoip2地址库
```
# geoip2地址识别库
wget https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz -O ~/libmaxminddb-1.6.0.tar.gz
# 第三方模块
wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.3.tar.gz -O ngx_http_geoip2_module.tar.gz
# geoip2地址库需要注册登录下载
# 下载地址为https://www.maxmind.com/en/accounts/current/geoip/downloads
# 已下载,见文件[GeoLite2-Country_20210615.tar.gz]
```
## 4、安装识别库
```
cd ~
tar zxvf libmaxminddb-1.6.0.tar.gz
cd libmaxminddb-1.6.0
./configure && make && make install
echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
ldconfig
```
## 5、查看nginx编译参数
```
./sbin/nginx -V
```
复制编译参数
## 6、编译安装nginx
```
tar zxvf ngx_http_geoip2_module.tar.gz -C /etc/nginx/modules
tar zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure 【步骤5中的编译参数】 --add-dynamic-module=/etc/nginx/modules/ngx_http_geoip2_module-3.3
make && make install
# 检查/etc/nginx/modules/目录中是否存在ngx_http_geoip2_module.so与ngx_stream_geoip2_module.so
# 若不存在复制nginx-1.18.0/objs目录下对应的so文件到/etc/nginx/modules/中
```
## 7、配置nginx
##### nginx.conf添加以下两行配置与http配置块同级
```
...
load_module /etc/nginx/modules/ngx_http_geoip2_module.so;
load_module /etc/nginx/modules/ngx_stream_geoip2_module.so;
...
```
##### geoip2地址库配置
```
mkdir /etc/nginx/GeoIP
tar zxvf GeoLite2-Country_20210615.tar.gz -C /etc/nginx/GeoIP
tar zxvf GeoLite2-City_20210615.tar.gz -C /etc/nginx/GeoIP
ln -s /etc/nginx/GeoIP/GeoLite2-City_20210615 /etc/nginx/GeoIP/City
```
##### nginx.conf的http配置块中添加以下配置
```
http {
...
# 国家或地区
geoip2 /etc/nginx/GeoIP/Country/GeoLite2-Country.mmdb {
$geoip2_country_code source=$remote_addr country iso_code;
}
# 城市
geoip2 /etc/nginx/GeoIP/City/GeoLite2-City.mmdb {
$geoip2_city_name_en default=ShangHai city names en;
}
map $geoip2_country_code $allowed_country {
default yes;
CN no;
}
map $geoip2_city_name_en $allowed_city {
default yes;
Haikou no;
Meilan no;
}
...
}
```
##### nginx.conf的server配置块中添加以下配置
```
server {
...
# 限制allowed_country为no的国家或地区访问
if ($allowed_country = no) { return 503;}
# 限制allowed_city为no的城市访问
if ($allowed_city = no) {return 600;}
...
location ...
}
```
##### 重启nginx

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 474 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,17 @@
```
if ($request_method = 'OPTIONS') {
# 表示允许这个域跨域调用(客户端发送请求的域名和端口)
# $http_origin动态获取请求客户端请求的域 不用*的原因是带cookie的请求不支持*号
add_header Access-Control-Allow-Origin $http_origin;
# 表示请求头的字段 动态获取
add_header Access-Control-Allow-Headers $http_access_control_request_headers;
# 该字段可选。它的值是一个布尔值表示是否允许发送Cookie。默认情况下
# Cookie不包括在CORS请求之中。设为true即表示服务器明确许可
# Cookie可以包含在请求中一起发给服务器。这个值也只能设为true
# 如果服务器不要浏览器发送Cookie删除该字段即可
add_header Access-Control-Allow-Credentials true;
# 指定允许跨域的方法,*代表所有
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
return 200;
}
```

View file

@ -0,0 +1,13 @@
举例说明访问连接为http://192.168.1.107/server/api?wsdl
nginx配置
server {
listen 80;
server_name 192.168.1.107;
location /server/api {
if ($args ~ 'wsdl') {
return 301 http://$server_name:$server_port/server/api?wsdl;
}
}
}
}

View file

@ -0,0 +1,27 @@
```
groupadd -g 501 www
useradd -u 501 -g 501 www
yum -y install pcre pcre-devel gcc-c++ perl perl-devel perl-ExtUtils-Embed openssl-devel
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module --with-http_v2_module \
--with-http_realip_module --with-http_gunzip_module \
--with-http_gzip_static_module --with-http_stub_status_module \
--with-http_perl_module --with-stream \
--with-stream_ssl_module --with-stream_realip_module --with-pcre
make
make install
```
若make时失败提示
```
relocation R_X86_64_32 against `.rodata' can not be used when making a shared object;
```
在configure前加上CFLAGS="-O3 -fPIC",即
```
CFLAGS="-O3 -fPIC" ./configure --prefix=/usr/local/nginx \
--with-http_ssl_module --with-http_v2_module \
--with-http_realip_module --with-http_gunzip_module \
--with-http_gzip_static_module --with-http_stub_status_module \
--with-http_perl_module --with-stream \
--with-stream_ssl_module --with-stream_realip_module --with-pcre
```

Binary file not shown.

View file

@ -0,0 +1,327 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="tool" content="leanote-desktop-app">
<title>nginx服务器防sql注入/溢出攻击/spam及禁User-agents</title>
<style>
*{font-family:"lucida grande","lucida sans unicode",lucida,helvetica,"Hiragino Sans GB","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif;}
body {
margin: 0;
}
/*公用文字样式*/
h1{font-size:30px}h2{font-size:24px}h3{font-size:18px}h4{font-size:14px}
.note-container{
width:850px;
margin:auto;
padding: 10px 20px;
box-shadow: 1px 1px 10px #eee;
}
#title {
margin: 0;
}
table {
margin-bottom: 16px;
border-collapse: collapse;
}
table th, table td {
padding: 6px 13px;
border: 1px solid #ddd;
}
table th {
font-weight: bold;
}
table tr {
background-color: none;
border-top: 1px solid #ccc;
}
table tr:nth-child(2n) {
background-color: rgb(247, 247, 249);
}
.mce-item-table, .mce-item-table td, .mce-item-table th, .mce-item-table caption {
border: 1px solid #ddd;
border-collapse: collapse;
padding: 6px 13px;
}
blockquote {
border-left-width:10px;
background-color:rgba(128,128,128,0.05);
border-top-right-radius:5px;
border-bottom-right-radius:5px;
padding:15px 20px;
border-left:5px solid rgba(128,128,128,0.075);
}
blockquote p {
margin-bottom:1.1em;
font-size:1em;
line-height:1.45
}
blockquote ul:last-child,blockquote ol:last-child {
margin-bottom:0
}
pre {
padding: 18px;
background-color: #f7f7f9;
border: 1px solid #e1e1e8;
border-radius: 3px;
display: block;
}
code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
white-space: nowrap;
background-color: #f9f2f4;
border-radius: 4px;
}
.footnote {
vertical-align: top;
position: relative;
top: -0.5em;
font-size: .8em;
}
hr {
margin:2em 0
}
img {
max-width:100%
}
pre {
word-break:break-word
}
p,pre,pre.prettyprint,blockquote {
margin:0 0 1.1em
}
hr {
margin:2em 0
}
img {
max-width:100%
}
.sequence-diagram,.flow-chart {
text-align:center;
margin-bottom:1.1em
}
.sequence-diagram text,.flow-chart text {
font-size:15px !important;
font-family:"Source Sans Pro",sans-serif !important
}
.sequence-diagram [fill="#ffffff"],.flow-chart [fill="#ffffff"] {
fill:#f6f6f6
}
.sequence-diagram [stroke="#000000"],.flow-chart [stroke="#000000"] {
stroke:#3f3f3f
}
.sequence-diagram text[stroke="#000000"],.flow-chart text[stroke="#000000"] {
stroke:none
}
.sequence-diagram [fill="#000"],.flow-chart [fill="#000"],.sequence-diagram [fill="#000000"],.flow-chart [fill="#000000"],.sequence-diagram [fill="black"],.flow-chart [fill="black"] {
fill:#3f3f3f
}
ul,ol {
margin-bottom:1.1em
}
ul ul,ol ul,ul ol,ol ol {
margin-bottom:1.1em
}
kbd {
padding:.1em .6em;
border:1px solid rgba(63,63,63,0.25);
-webkit-box-shadow:0 1px 0 rgba(63,63,63,0.25);
box-shadow:0 1px 0 rgba(63,63,63,0.25);
font-size:.7em;
font-family:sans-serif;
background-color:#fff;
color:#333;
border-radius:3px;
display:inline-block;
margin:0 .1em;
white-space:nowrap
}
.toc ul {
list-style-type:none;
margin-bottom:15px
}
</style>
<!-- 该css供自定义样式 -->
<link href="../leanote-html.css" rel="stylesheet">
</head>
<body>
<div class="note-container">
<h1 class="title" id="leanote-title">nginx服务器防sql注入/溢出攻击/spam及禁User-agents</h1>
<div class="content-html" id="leanote-content"><pre id="leanote_ace_1508394055763_0" class="brush:sh ace-tomorrow">server {
## 禁SQL注入 Block SQL injections
set $block_sql_injections 0;
if ($query_string ~ "union.*select.*(") {
set $block_sql_injections 1;
}
if ($query_string ~ "union.*all.*select.*") {
set $block_sql_injections 1;
}
if ($query_string ~ "concat.*(") {
set $block_sql_injections 1;
}
if ($block_sql_injections = 1) {
return 444;
}
## 禁掉文件注入
set $block_file_injections 0;
if ($query_string ~ "[a-zA-Z0-9_]=http://") {
set $block_file_injections 1;
}
if ($query_string ~ "[a-zA-Z0-9_]=(..//?)+") {
set $block_file_injections 1;
}
if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") {
set $block_file_injections 1;
}
if ($block_file_injections = 1) {
return 444;
}
## 禁掉溢出攻击
set $block_common_exploits 0;
if ($query_string ~ "(&lt;|%3C).*script.*(&gt;|%3E)") {
set $block_common_exploits 1;
}
if ($query_string ~ "GLOBALS(=|[|%[0-9A-Z]{0,2})") {
set $block_common_exploits 1;
}
if ($query_string ~ "_REQUEST(=|[|%[0-9A-Z]{0,2})") {
set $block_common_exploits 1;
}
if ($query_string ~ "proc/self/environ") {
set $block_common_exploits 1;
}
if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|%3D)") {
set $block_common_exploits 1;
}
if ($query_string ~ "base64_(en|de)code(.*)") {
set $block_common_exploits 1;
}
if ($block_common_exploits = 1) {
return 444;
}
## 禁spam字段
set $block_spam 0;
if ($query_string ~ "b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)b") {
set $block_spam 1;
}
if ($query_string ~ "b(erections|hoodia|huronriveracres|impotence|levitra|libido)b") {
set $block_spam 1;
}
if ($query_string ~ "b(ambien|bluespill|cialis|cocaine|ejaculation|erectile)b") {
set $block_spam 1;
}
if ($query_string ~ "b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)b") {
set $block_spam 1;
}
if ($block_spam = 1) {
return 444;
}
## 禁掉user-agents
set $block_user_agents 0;
# Dont disable wget if you need it to run cron jobs!
#if ($http_user_agent ~ "Wget") {
# set $block_user_agents 1;
#}
# Disable Akeeba Remote Control 2.5 and earlier
if ($http_user_agent ~ "Indy Library") {
set $block_user_agents 1;
}
# Common bandwidth hoggers and hacking tools.
if ($http_user_agent ~ "libwww-perl") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GetRight") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GetWeb!") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Go!Zilla") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Download Demon") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Go-Ahead-Got-It") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "TurnitinBot") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GrabNet") {
set $block_user_agents 1;
}
if ($block_user_agents = 1) {
return 444;
}
}</pre><p><br data-mce-bogus="1"></p><pre id="leanote_ace_1508397464648_0" class="brush:sh ace-tomorrow"># block sql injections
set $block_sql_injections 0;
if ($request_uri ~* "(cost\()|(concat\()"){set $block_sql_injections 1;}
if ($request_uri ~* "[+|(%20)]union[+|(%20)]") { set $block_sql_injections 1;}
if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {set $block_sql_injections 1;}
if ($request_uri ~* "[+|(%20)]or[+|(%20)]") {set $block_sql_injections 1;}
if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {set $block_sql_injections 1;}
if ($query_string ~* ".*[;'&lt;&gt;].*" ){set $block_sql_injections 1;}
if ($uri ~* (.*)(insert|select|delete|update|count|master|truncate|declare|exec|\*|%|\')(.*)$ ) { set $block_sql_injections 1; }
if ($block_sql_injections = 1) {return 403;}
# block file injections
set $block_file_injections 0;
if ($query_string ~ “[a-zA-Z0-9_]=http://”) { set $block_file_injections 1;}
if ($query_string ~ “[a-zA-Z0-9_]=(..//?)+”) {set $block_file_injections 1;}
if ($query_string ~ “[a-zA-Z0-9_]=/([a-z0-9_.]//?)+”){set $block_file_injections 1;}
if ($block_file_injections = 1) {return 403;}
set $block_spam 0;
if ($query_string ~ "\b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)\b") {set $block_spam 1;}
if ($query_string ~ "\b(erections|hoodia|huronriveracres|impotence|levitra|libido)\b") {set $block_spam 1;}
if ($query_string ~ "\b(ambien|blue\spill|cialis|cocaine|ejaculation|erectile)\b") {set $block_spam 1;}
if ($query_string ~ "\b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)\b") {set $block_spam 1;}
if ($block_spam = 1) {return 403;}
set $block_user_agents 0;
# Don't disable wget if you need it to run cron jobs!
#if ($http_user_agent ~ "Wget") {set $block_user_agents 1;}
# Disable Akeeba Remote Control 2.5 and earlier
if ($http_user_agent ~ "Indy Library") {set $block_user_agents 1;}
# Common bandwidth hoggers and hacking tools.
if ($http_user_agent ~ "libwww-perl") {set $block_user_agents 1;}
if ($http_user_agent ~ "GetRight") {set $block_user_agents 1;}
if ($http_user_agent ~ "GetWeb!") {set $block_user_agents 1;}
if ($http_user_agent ~ "Go!Zilla") {set $block_user_agents 1;}
if ($http_user_agent ~ "Download Demon") {set $block_user_agents 1;}
if ($http_user_agent ~ "Go-Ahead-Got-It") {set $block_user_agents 1;}
if ($http_user_agent ~ "TurnitinBot") {set $block_user_agents 1;}
if ($http_user_agent ~ "GrabNet") {set $block_user_agents 1;}
if ($block_user_agents = 1) {return 403;}</pre><p><br data-mce-bogus="1"></p></div>
</div>
<!-- 该js供其它处理 -->
<script src="../leanote-html.js"></script>
</body>
</html>

View file

@ -0,0 +1,16 @@
```
http {
...
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
...
}
```

View file

@ -0,0 +1,333 @@
######Nginx配置文件nginx.conf中文详解#####
#定义Nginx运行的用户和用户组
user www www;
#nginx进程数建议设置为等于CPU总核心数。
worker_processes 8;
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /usr/local/nginx/logs/error.log info;
#进程pid文件
pid /usr/local/nginx/logs/nginx.pid;
#指定进程可以打开的最大描述符:数目
#工作模式与连接数上限
#这个指令是指当一个nginx进程打开的最多文件描述符数目理论值应该是最多打开文件数ulimit -n与nginx进程数相除但是nginx分配请求并不是那么均匀所以最好与ulimit -n 的值保持一致。
#现在在linux 2.6内核下开启文件打开数为65535worker_rlimit_nofile就相应应该填写65535。
#这是因为nginx调度时分配请求到进程并不是那么的均衡所以假如填写10240总并发量达到3-4万时就有进程可能超过10240了这时会返回502错误。
worker_rlimit_nofile 65535;
events
{
#参考事件模型use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
#是Linux 2.6以上版本内核中的高性能网络I/O模型linux建议epoll如果跑在FreeBSD上面就用kqueue模型。
#补充说明:
#与apache相类nginx针对不同的操作系统有不同的事件模型
#A标准事件模型
#Select、poll属于标准事件模型如果当前系统不存在更有效的方法nginx会选择select或poll
#B高效事件模型
#Kqueue使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
#Epoll使用于Linux内核2.6版本及以后的系统。
#/dev/poll使用于Solaris 7 11/99+HP/UX 11.22+ (eventport)IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
#Eventport使用于Solaris 10。 为了防止出现内核崩溃的问题, 有必要安装安全补丁。
use epoll;
#单个进程最大连接数(最大连接数=连接数*进程数)
#根据硬件调整和前面工作进程配合起来用尽量大但是别把cpu跑到100%就行。每个进程允许的最多连接数理论上每台nginx服务器的最大连接数为。
worker_connections 65535;
#keepalive超时时间。
keepalive_timeout 60;
#客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置一般一个请求头的大小不会超过1k不过由于一般系统分页都要大于1k所以这里设置为分页大小。
#分页大小可以用命令getconf PAGESIZE 取得。
## getconf PAGESIZE
#4096
#但也有client_header_buffer_size超过4k的情况但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。
client_header_buffer_size 4k;
#这个将为打开文件指定缓存默认是没有启用的max指定缓存数量建议和打开文件数一致inactive是指经过多长时间文件没被请求后删除缓存。
open_file_cache max=65535 inactive=60s;
#这个是指多长时间检查一次缓存的有效信息。
#语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.
open_file_cache_valid 80s;
#open_file_cache指令中的inactive参数时间内文件的最少使用次数如果超过这个数字文件描述符一直是在缓存中打开的如上例如果有一个文件在inactive时间内一次没被使用它将被移除。
#语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache中总是打开状态.
open_file_cache_min_uses 1;
#语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.
open_file_cache_errors on;
}
#设定http服务器利用它的反向代理功能提供负载均衡支持
http
{
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#默认编码
#charset utf-8;
#服务器名字的hash表大小
#保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小那么在查找键的时候最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址第二次是在存储单元中查找键 值。因此如果Nginx给出需要增大hash max size 或 hash bucket size的提示那么首要的是增大前一个参数的大小.
server_names_hash_bucket_size 128;
#客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置一般一个请求的头部大小不会超过1k不过由于一般系统分页都要大于1k所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。
client_header_buffer_size 32k;
#客户请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值如果header过大它会使用large_client_header_buffers来读取。
large_client_header_buffers 4 64k;
#设定通过nginx上传文件的大小
client_max_body_size 8m;
#开启高效文件传输模式sendfile指令指定nginx是否调用sendfile函数来输出文件对于普通应用设为 on如果用来进行下载等应用磁盘IO重负载应用可设置为off以平衡磁盘与网络I/O处理速度降低系统的负载。注意如果图片显示不正常把这个改成off。
#sendfile指令指定 nginx 是否调用sendfile 函数zero copy 方式来输出文件对于普通应用必须设为on。如果用来进行下载等应用磁盘IO重负载应用可设置为off以平衡磁盘与网络IO处理速度降低系统uptime。
sendfile on;
#开启目录列表访问,合适下载服务器,默认关闭。
autoindex on;
#此选项允许或禁止使用socke的TCP_CORK的选项此选项仅在使用sendfile的时候使用
tcp_nopush on;
tcp_nodelay on;
#长连接超时时间,单位是秒
keepalive_timeout 120;
#FastCGI相关参数是为了改善网站的性能减少资源占用提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本默认1.1前端如果是squid2.5请使用1.0
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; #压缩类型默认就已经包含textml所以下面就不用再写了写上去也不会有问题但是会有一个warn。
gzip_vary on; #是否在http header中添加Vary: Accept-Encoding建议开启
gzip_disable "MSIE [1-6]\."; # 禁用IE6 gzip
#开启限制IP连接数的时候需要使用
#limit_zone crawler $binary_remote_addr 10m;
#负载均衡配置
upstream piao.jd.com {
#upstream的负载均衡weight是权重可以根据机器配置定义权重。weigth参数表示权值权值越高被分配到的几率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
#nginx的upstream目前支持4种方式的分配
#1、轮询默认
#每个请求按时间顺序逐一分配到不同的后端服务器如果后端服务器down掉能自动剔除。
#2、weight
#指定轮询几率weight和访问比率成正比用于后端服务器性能不均的情况。
#例如:
#upstream bakend {
# server 192.168.0.14 weight=10;
# server 192.168.0.15 weight=10;
#}
#2、ip_hash
#每个请求按访问ip的hash结果分配这样每个访客固定访问一个后端服务器可以解决session的问题。
#例如:
#upstream bakend {
# ip_hash;
# server 192.168.0.14:88;
# server 192.168.0.15:80;
#}
#3、fair第三方
#按后端服务器的响应时间来分配请求,响应时间短的优先分配。
#upstream backend {
# server server1;
# server server2;
# fair;
#}
#4、url_hash第三方
#按访问url的hash结果来分配请求使每个url定向到同一个后端服务器后端服务器为缓存时比较有效。
#例在upstream中加入hash语句server语句中不能写入weight等其他的参数hash_method是使用的hash算法
#upstream backend {
# server squid1:3128;
# server squid2:3128;
# hash $request_uri;
# hash_method crc32;
#}
#tips:
#upstream bakend{#定义负载均衡设备的Ip及设备状态}{
# ip_hash;
# server 127.0.0.1:9090 down;
# server 127.0.0.1:8080 weight=2;
# server 127.0.0.1:6060;
# server 127.0.0.1:7070 backup;
#}
#在需要使用负载均衡的server中增加 proxy_pass http://bakend/;
#每个设备的状态设置为:
#1.down表示单前的server暂时不参与负载
#2.weight为weight越大负载的权重就越大。
#3.max_fails允许请求失败的次数默认为1.当超过最大次数时返回proxy_next_upstream模块定义的错误
#4.fail_timeout:max_fails次失败后暂停的时间。
#5.backup 其它所有的非backup机器down或者忙的时候请求backup机器。所以这台机器压力会最轻。
#nginx支持同时设置多组的负载均衡用来给不用的server来使用。
#client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
#client_body_temp_path设置记录文件的目录 可以设置最多3层目录
#location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
}
#虚拟主机的配置
server
{
#监听端口
listen 80;
#域名可以有多个,用空格隔开
server_name www.jd.com jd.com;
index index.html index.htm index.php;
root /data/www/jd;
#对******进行负载均衡
location ~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
#图片缓存时间设置
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;
}
#JS和CSS缓存时间设置
location ~ .*.(js|css)?$
{
expires 1h;
}
#日志格式设定
#$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址
#$remote_user用来记录客户端用户名称
#$time_local 用来记录访问时间与时区;
#$request 用来记录请求的url与http协议
#$status 用来记录请求状态成功是200
#$body_bytes_sent :记录发送给客户端文件主体内容大小;
#$http_referer用来记录从那个页面链接访问过来的
#$http_user_agent记录客户浏览器的相关信息
#通常web服务器放在反向代理的后面这样就不能获取到客户的IP地址了通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中可以增加x_forwarded_for信息用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#定义本虚拟主机的访问日志
access_log /usr/local/nginx/logs/host.access.log main;
access_log /usr/local/nginx/logs/host.access.404.log log404;
#对 "/" 启用反向代理
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向代理的配置,可选。
proxy_set_header Host $host;
#允许客户端请求的最大单文件字节数
client_max_body_size 10m;
#缓冲区代理缓冲用户端请求的最大字节数,
#如果把它设置为比较大的数值例如256k那么无论使用firefox还是IE浏览器来提交任意小于256k的图片都很正常。如果注释该指令使用默认的client_body_buffer_size设置也就是操作系统页面大小的两倍8k或者16k问题就出现了。
#无论使用firefox4.0还是IE8.0提交一个比较大200k左右的图片都返回500 Internal Server Error错误
client_body_buffer_size 128k;
#表示使nginx阻止HTTP应答代码为400或者更高的应答。
proxy_intercept_errors on;
#后端服务器连接的超时时间_发起握手等候响应超时时间
#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 90;
#后端服务器数据回传时间(代理发送超时)
#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
proxy_send_timeout 90;
#连接成功后,后端服务器响应时间(代理接收超时)
#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理也可以说是后端服务器处理请求的时间
proxy_read_timeout 90;
#设置代理服务器nginx保存用户头信息的缓冲区大小
#设置从被代理服务器读取的第一部分应答的缓冲区大小通常情况下这部分应答中包含一个小的应答头默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小不过可以将其设置为更小
proxy_buffer_size 4k;
#proxy_buffers缓冲区网页平均在32k以下的设置
#设置用于读取应答来自被代理服务器的缓冲区数目和大小默认情况也为分页大小根据操作系统的不同可能是4k或者8k
proxy_buffers 4 32k;
#高负荷下缓冲大小proxy_buffers*2
proxy_busy_buffers_size 64k;
#设置在写入proxy_temp_path时数据的大小预防一个工作进程在传递文件时阻塞太长
#设定缓存文件夹大小大于这个值将从upstream服务器传
proxy_temp_file_write_size 64k;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file confpasswd;
#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
}
#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
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 http://127.0.0.1:8080;
}
#所有静态文件由nginx直接读取不经过tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|
pdf|xls|mp3|wma)$
{
expires 15d;
}
location ~ .*.(js|css)?$
{
expires 1h;
}
}
}
######Nginx配置文件nginx.conf中文详解#####

View file

@ -0,0 +1,68 @@
#### 背景
```
1、kubernetes中某个nginx需要反向代理某个svc虽然svc对应的pod有扩缩容或更新或者svc重建时svc链接不变但是对应的ip已发生了变化。
2、proxy_pass配置的链接nginx会在请求dns后把对应的ip信息缓存起来后续请求就一直使用缓存ip直到nginx重启
```
#### 目的
```
实现nginx热更新即每次请求都使用新的ip地址
```
#### 方法
```
nginx使用resolver与set关键字。
resolver用于解析dnsset用于定义变量
```
#### 具体实现
nginx的server配置段需要配置如下内容
```
#
server {
listen 80;
server_name localhost;
# DNS
resolver kube-dns.kube-system.svc.cluster.local valid=1s;
# test
# test命名空间中的一个端口为8080的service
set $test test-svc.test.svc.cluster.local:8080;
location / {
proxy_pass http://$test; ## 后面不能跟/
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
```
*** 问题来了 ***
```
当需要反向代理多个后端或者访问的uri中需要加字符串标识即location不是/,反向代理需要加上/实际访问后端链接不带location配置的字符串实际访问的链接是非期望的。
```
*** 解决方法 ***
```
#
server {
listen 80;
server_name localhost;
# DNS
resolver kube-dns.kube-system.svc.cluster.local valid=1s;
# test
# test命名空间中的一个端口为8080的service
set $test test-svc.test.svc.cluster.local:8080;
location /test/ {
rewrite ^/test/(.*) /$1 break;
proxy_pass http://$test; ## 后面不能跟/
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
```

View file

@ -0,0 +1,73 @@
# 1、生成CA私钥
```
openssl genrsa -out ca.key 4096
```
# 2、生成CA证书请求
```
openssl req -new -key ca.key -out ca.csr
```
***ca的Common Name与其他证书不同其他相同***
> Country Name (2 letter code) [AU]: CN # 国家名称
State or Province Name (full name) [Some-State]: Hainan # 省
Locality Name (eg, city) []: Haikou # 市
Organization Name (eg, company) [Internet Widgits Pty Ltd]: Probe ( Hai Nan ) Investment Co., Ltd. # 公司名称
Organizational Unit Name (eg, section) []: Probe Institute # 组织单位名称
Common Name (e.g. server FQDN or YOUR name) []: probe.cc # ca与其他证书不同
Email Address []:
# 3、生成ca证书
*ca证书有效期10年*
```
openssl x509 -req -in ca.csr -out ca.crt -signkey ca.key -CAcreateserial -days 3650
```
# 4、生成server私钥
```
openssl genrsa -out server.key 4096
```
# 5、生成server证书请求文件
```
openssl req -new -key server.key -out server.csr
```
> Country Name (2 letter code) [AU]: CN # 国家名称
State or Province Name (full name) [Some-State]: Hainan # 省
Locality Name (eg, city) []: Haikou # 市
Organization Name (eg, company) [Internet Widgits Pty Ltd]: Probe ( Hai Nan ) Investment Co., Ltd. # 公司名称
Organizational Unit Name (eg, section) []: Probe Institute # 组织单位名称
Common Name (e.g. server FQDN or YOUR name) []: api.probe.cc # 与ca不同,双向认证接口域名
Email Address []:
# 6、生成server证书
*server证书有效期10年*
```
openssl x509 -req -in server.csr -out server.crt -signkey server.key -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650
```
# 7、生成p12格式证书
```
openssl pkcs12 export clcerts in server.crt inkey server.key out server.p12
```
# 8、nginx 配置
```
server {
listen 443;
server_name api.probe.cc;
ssl on;
ssl_certificate /etc/nginx/keys/server.crt;#配置证书位置
ssl_certificate_key /etc/nginx/keys/server.key;#配置秘钥位置
ssl_client_certificate /etc/nginx/keys/ca.crt;#双向认证
ssl_verify_client on; #双向认证
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照这个套件配置
ssl_prefer_server_ciphers on;
root html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
```
# 9、安装p12证书
导出server.p12文件并在浏览器安装。略

View file

@ -0,0 +1,21 @@
编译安装nginx时需要在编译参数中加上--with-stream
windows下的nginx直接配置nginx.conf即可
在nginx.conf的events的同级目录配置stream块如下示例
events {
worker_connections 1024;
}
stream {
upstream socket_server{
server 127.0.0.1:3802 weight=1;
server 127.0.0.1:3803 weight=1;
    }
    #监听socket端口
    server {
listen 3801;
proxy_pass socket_server;
proxy_connect_timeout 10s;
proxy_timeout 300s
    }
}

View file

@ -0,0 +1,20 @@
修改/usr/local/nginx/conf/nginx.conf
(如果需要转发的网站较多,为方便查询,可以在目录中添加对应的配置文件(建议新建目录放配置文件)
如vhost_test.iboxpay.com.conf
添加如下内容
server {
listen 80;
server_name test.iboxpay.com;
rewrite ^/(.*)$ http://$server_name/test/ last;
location /test/ {
proxy_pass http://192.168.100.100:8080/test/; ##nat地址:端口
access_log /usr/local/nginx/logs/insurex_access.log main;
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_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
client_max_body_size 10m;
}
}