说明

web 服务器「静态代理」、反向代理服务器、负载均衡「轮训、ip哈希、加权轮训、随机、最少连接」、限流、缓存「浏览器缓存、代理层缓存」、黑白名单

下载

nginx: download

参考

Nginx 通关攻略

超实用的 Nginx 极简教程,覆盖了常用场景https://mp.weixin.qq.com/s/RfaPtO3VWDozIZvGxN2ASQ

2F60pt

基础入门:http://wiki.jikexueyuan.com/project/nginx/overwise.html

  1. Nginx 是一款轻量级的 Web 服务器、反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用
  2. 正向代理“代理”的是客户端,而且客户端是知道目标的,而目标是不知道客户端是通过VPN访问的
  3. 反向代理“代理”的是服务器端,而且这一个过程对于客户端而言是透明的

在线配置

以后配置Nginx 用这款神器! (qq.com)

$host、$http_host和$proxy_host区别

变量 是否显示端口
$host 不显示端口 浏览器请求的ip,不显示端口
$http_host 端口存在则显示 浏览器请求的ip和端口号
$proxy_host 默认80端口不显示,其它显示 被代理服务的ip和端口号

Docker

https://www.cnblogs.com/blueskyli/p/10700501.html

  1. 拉取 nginx 镜像,采用网易云的镜像

    1
    docker pull hub.c.163.com/library/nginx:latest
  2. 查看本地镜像

    1
    2
    3
    4
    docker images

    REPOSITORY TAG IMAGE ID CREATED SIZE
    hub.c.163.com/library/nginx latest 46102226f2fd 2 years ago 109MB
  3. 复制容器内的配置文件并修改

    1
    2
    //前提:先运行容器
    docker container cp nginx:/etc/nginx .
  4. 后台运行 nginx

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    docker run --name nginx -p 8000:80  -v /Users/mac126/study/docker/nginx/conf:/etc/nginx -v /Users/mac126/study/docker/nginx/html:/usr/share/nginx/html -v /Users/mac126/study/docker/nginx/logs:/var/log/nginx -d nginx

    > 命令说明:
    >
    > -d 让 Docker 在后台运行而不是直接把执行命令的结果输出在当前宿主机下
    >
    > -p 8080:80 将容器的 80 端口映射到主机的 8080 端口
    > --name nginx:将容器命名为 nginx
    > -v /mac126/docker/www:/www:将主机中当前目录下的 www 挂载到容器的 /www
    > -v /mac126/docker/conf/nginx.conf:/etc/nginx/nginx.conf:将主机中当前目录下的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf
    > -v /mac126/docker/logs:/wwwlogs:将主机中当前目录下的 logs 挂载到容器的 /wwwlogs
  5. 查看运行的容器

    1
    docker ps
  6. 进入容器

    1
    docker exec -it nginx /bin/bash

Windows

下载安装

  1. 下载

    1
    https://nginx.org/en/download.html

    UTOOLS1576647782397.jpg
    下载 nginx/Windows-1.17.2

  2. 启动

    1
    2
    3
    cd D:\nginx-1.17.2

    start nginx
  3. 查看进程

    1
    tasklist|findstr nginx
  4. 停止

    1
    nginx -s quit
  5. 启动状态下重载配置文件

    1
    nginx -s reload
  6. 开启状态监控

    https://www.aiops.com/docs/ci/services_example/nginx/

    1
    2
    3
    4
    location /nginx-status {
    stub_status on;
    access_log on;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    ### 服务

    #### Windows 转换服务

    1. 下载[Windows Service Wrapper](https://github.com/kohsuke/winsw/releases) 小工具

    2. 文件重命名为 `nginx-service.exe`

    3. 同目录下新建 `nginx-service.xml` 文件

    nginx nginx nginx C:\nginx-1.17.1 roll C:\nginx-1.17.1\nginx.exe C:\nginx-1.17.1\nginx.exe -s stop
    1
    2
    3
    4
    5

    #### 服务命令

    ##### 安装

nginx-service.exe install

1
2
3

##### 卸载


nginx-service.exe uninstall

1
2
3

##### 启动

nginx-service.exe start

1
2
3

##### 停止

nginx-service.exe stop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

### 问题

安装服务时,提示若要运行此应用程序,您必须首先安装.NET Framework 的以下版本之一

<img src="https://wangqinpei.oss-cn-qingdao.aliyuncs.com/uPic/image-20200702134439059.png" alt="image-20200702134439059" style="zoom:50%;" />

解决方法:根据提示安装.net FrameWork 即可 ,下载安装[Microsoft .NET Framework 4(独立安装程序)](https://www.microsoft.com/zh-cn/download/details.aspx?id=17718)

## Linux

### 下载

```shell
# centos8官方源已下线,建议切换centos-vault源
rm -f /etc/yum.repos.d/CentOS-AppStream.repo
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum clean all
yum makecache

# 安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

# 下载指定版本
wget http://nginx.org/download/nginx-1.18.0.tar.gz

安装

1
2
3
4
5
# 解压
tar -zxvf nginx-1.18.0.tar.gz

# 进入目录,编译、安装【默认安装在/usr/local/nginx】
cd nginx-1.18.0 && ./configure && make && make install

验证

1
2
3
nginx主配置文件:/usr/local/nginx/conf/nginx.conf
nginx日志文件:/usr/local/nginx/logs/access.log
启动Nginx:/usr/local/nginx/sbin/nginx

Mac

安装

  • 参考

    https://segmentfault.com/a/1190000016020328

  • 命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 更新 homebrew
    brew update

    # 查看 nginx
    brew search nginx

    # 安装 nginx
    brew install nginx
    # 安装目录
    /usr/local/Cellar/nginx/1.17.1/
    #bin 目录中 nginx 为可执行文件,/usr/local/bin/nginx 指向该文件
    #html 目录为软连接,指向 /usr/local/var/www

发布目录和配置文件

  • 发布目录

    1
    /usr/local/var/www
  • nginx 默认使用 8080 端口,配置文件

    1
    /usr/local/etc/nginx/nginx.conf

启动

1
nginx

停止

1
nginx -s quit

重新启动 nginx

1
nginx -s reload

最大请求实体大小限制

http节点下:将Nginx代理的所有请求实体的大小限制为20m

1
2
3
4
http {
# 将Nginx代理的所有请求实体的大小限制为20m
client_max_body_size 20m;
}

server节点下:将该服务下的所有请求实体的大小限制为20m

1
2
3
4
server {
# 将该服务下的所有请求实体的大小限制为20m
client_max_body_size 20m;
}

location节点下:将此路由请求的实体大小限制为20m

1
2
3
4
location /yanggb {
# 将此路由请求的实体大小限制为20m
client_max_body_size 20m;
}

保存之后要记得重启Nginx使修改后的配置生效

1
service nginx restart

开启目录浏览和索引

Nginx 默认是不允许列出整个目录

1
2
3
4
5
6
7
8
9
10
11
location / {
autoindex on;

autoindex_exact_size off;
#默认为 on,显示出文件的确切大小,单位是 bytes。
#改为 off 后,显示出文件的大概大小,单位是 kB 或者 MB 或者 GB

autoindex_localtime on;
# 默认为 off,显示的文件时间为 GMT 时间。
# 改为 on 后,显示的文件时间为文件的服务器时间
}

支持跨域访问

1
2
3
4
5
6
7
8
9
10
location / {
add_header Access-Control-Allow-Origin *;
# 允许跨域的请求方法
add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS';
add_header Access-Control-Allow-Headers 'X-Sign,Keep-Alive,User-Agent,X-TIMESTAMP,If-Modified-Since,Cache-Control,Content-Type,X-Access-Token';

if ($request_method = 'OPTIONS') {
return 204;
}
}

发布多个目录

方式一:root

  • root 则是最上层目录的定义
  • root 的处理结果是:root 路径+location URI 路径
  • Windows 中路径分隔符使用 /
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 协议://ip:port 访问:html/ 目录资源
location / {
root html;
index index.html index.htm;
# 开启允许跨域访问
add_header 'Access-Control-Allow-Origin' '*';
}

# 协议://ip:port/abc 访问:C:/nginx-1.17.1/files/abc 目录资源
location /abc {
root C:/nginx-1.17.1/files;
index index.html index.htm;
# 开启允许跨域访问
add_header 'Access-Control-Allow-Origin' '*';
}

方式二:alias

  • alias 是一个目录别名的定义
  • alias 的处理结果是:使用 alias 路径替换 location URI 路径
  • 注意!!!!:Windows 中路径分隔符使用 /
  • 注意!!!!:alias 后面必须要用 / 结束,否则会找不到文件,而 root 则可有可无
1
2
3
4
5
6
7
# 协议://ip:port/abc 访问:C:/nginx-1.17.1/files/ 目录资源
location /abc {
alias C:/nginx-1.17.1/files/;
index index.html index.htm;
# 开启允许跨域访问
add_header 'Access-Control-Allow-Origin' '*';
}

windows 中批量杀死 Nginx 进程

1
taskkill /fi "imagename eq nginx.exe" /f

Http 代理、负载

参考

https://blog.csdn.net/xyang81/article/details/51702900

跨域

为某类型请求指定代理服务器

/event/eventadd_App.jspx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
location ~ \.*_App.(jspx|do)?$ {
proxy_pass https://www.sina.cn;
}

location ~ /scm-web/(eventAdd|addEvent|caseAdd)/* {
proxy_pass https://www.sina.cn;
}

location /apis {
#当匹配到/apis 地址是时访问 http://172.88.88.88:31002/apis
proxy_pass http://172.88.88.88:31002/;
}

location ^~ /jeecg-boot/ {
proxy_pass http://127.0.0.1:8080/jeecg-boot/;
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;
}

# 通过 /jeecg 访问 http://192.168.0.147:8080/jeecg-boot/
location ^~ /jeecg/ {
proxy_pass http://192.168.0.147:8080/jeecg-boot/;
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;
}

# 访问 /*/login /*/post/conf/confGetByPage 等时转发到指定主机上
location ~* ^/(login|post/conf/confGetByPage|post/conf/GetConferenceBasic|/post/jsonrpc/callService)$ {
proxy_pass http://103.75.105.44;
}

代理百度地图接口

1
2
3
4
5
6
7
8
# 代理百度地图服务
location ^~ /api {
proxy_pass http://api.map.baidu.com/api;
}

location ^~ /library {
proxy_pass http://api.map.baidu.com/library;
}

重写 rewrite

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
server {
# 对外暴露 80 端口
listen 80;
server_name 192.168.10.2;

# 后端API地址暴露为:http://192.168.10.2/api 【推荐】
location /api {
proxy_pass http://127.0.0.1:9999/; # 实际访问后端地址
proxy_pass_request_headers on;
# 重写URL 去除apis
rewrite "^/api/(.*)$" /$1 break;
}

# 请求 http://192.168.0.99:8888/scmqx/ 时 ,重定向到 http://192.168.0.99:8888/scm/#/user/login?app=qx
# ${http_host} 表示 ip:port
location /scmqx {
rewrite "^/scm(.*)/$" http://${http_host}/scm/#/user/login?app=$1 break;
}


# 发布
location / {
root /home/ruoyi/projects/ruoyi-ui;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}

# 重写-推荐
location /prod-api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/;
}


# 重写: 外部访问 http://localhost:80/api 等价于内部访问 http://localhost:9999/gwyzl/
location /api {
proxy_pass http://localhost:9999/gwyzl/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

$1、$2参数解析

nginx rewrite 以及 $1、$2参数解析(附有生产配置实例) - 掘金 (juejin.cn)

资源跨域访问

在 nginx.conf 里找到 server 项,并在里面添加如下配置

1
2
3
4
location /{
# 开启允许跨域访问
add_header 'Access-Control-Allow-Origin' '*';
}

如果需要允许来自任何域的访问,可以这样配置

1
add_header Access-Control-Allow-Origin *;

注释如下

第一条指令:授权从 example.com 的请求 (必需)
第二条指令:当该标志为真时,响应于该请求是否可以被暴露 (可选)
第三条指令:允许脚本访问的返回头 (可选)
第四条指令:指定请求的方法,可以是 GET, POST, OPTIONS, PUT, DELETE 等 (可选)

TCP代理

nginx 9998 端口代理到 3306 端口

1
2
3
4
5
6
7
8
9
10
11
stream {
upstream mysql80{
server 127.0.0.1:3306;
}

server{
listen 9998;
proxy_connect_timeout 600s;
proxy_pass mysql80;
}
}

配置参考

文件 nginx_conf

worker_processes 为进程数,不要超过计算机 CPU 核心数,查看 CPU 核心:

image-20200727124752907
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

#user nobody;
worker_processes 32;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 10240;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
#gzip on;
upstream tomcat {
ip_hash;
server 10.82.28.225:8000;
server 10.82.28.223:8000;
}
server {
listen 8080;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;


location / {
proxy_pass http://tomcat;
proxy_redirect off;
#ÉèÖôúÀí
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}


location ^~ /arcgis_js_api {
root html;
}

location /files {
proxy_pass http://10.82.28.226:8080;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}


location /FileServer {
proxy_pass http://10.82.28.226:8000;
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_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

# 访问 /*/login /*/post/conf/confGetByPage 等时转发到指定主机上
location ~* ^/(login|post/conf/confGetByPage|post/conf/GetConferenceBasic|/post/jsonrpc/callService)$ {
proxy_pass http://103.75.105.44;
}
client_max_body_size 200m;

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

配置多个Server

一个 server 占 一个端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 录像文件回放
server {
listen 7707;
server_name localhost;

location / {
access_by_lua_file conf/lua/fileAccessTokenValidate.lua;
root /home/ftpfile;
index index.html index.htm;
}
location /apiV1/ {
proxy_pass http://127.0.0.1:7712/;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}

# offline map
server {
listen 7716;
server_name localhost;
location / {
root /usr/local/mcs8/offlineMapServer;
index index.html index.htm;
}
}

配置 https

参考:https://www.cnblogs.com/chasewade/p/7661290.html

安装 OpenSSL

  1. 安装 OpenSSL
  2. 配置环境变量

生成 https 证书

缺点是证书不受信任,浏览器会显示不安全

  1. 创建私钥

    1
    openssl genrsa -des3 -out server.key 1024 //server 自己取的名字  密码:bjtcrj
  2. 创建 csr 证书

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 有六项信息必须填写,其中“Common Name”就是证书的域名不要写错了
    # key 文件为刚才生成的文件,输入创建私钥时输入的密码
    openssl req -new -key server.key -out server.csr

    // 必填项
    Country Name (2 letter code) [XX]:CN
    State or Province Name (full name) []:Beijing
    Locality Name (eg, city) [Default City]:District east
    Organization Name (eg, company) [Default Company Ltd]:TONGFU.net
    Organizational Unit Name (eg, section) []:TONGFU.net
    Common Name (eg, your name or your server's hostname) []:tongfu.net
  3. 创建免密key文件

    1
    2
    3
    4
    5
    # 在重启web服务的时候不需要输入密码
    复制 `server.key` 文件,并重命名为 `server.key.org`

    # 输入创建私钥时输入的密码
    openssl rsa -in server.key.org -out server.key
  4. 生成 crt 证书

    1
    openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
  5. 查看证书信息

    1
    openssl x509 -in server.crt -noout -text

证书生成完毕。ssl 文件夹中一共生成了 4 个文件

修改 nginx.conf 配置文件

找到 HTTPS server 配置信息「已注释」,把前面的 #号去掉,修改其中的 ssl_certificate 和 ssl_certificate_key 配置项为刚才所生成的 server.crt 和 server.key 这两个文件的目录。并配置 server_name 为 localhost

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 443 ssl;
server_name tc.xxzhtc.cn; # 证书绑定的域名

ssl_certificate ../ssl/server.crt; # 这个是证书的crt文件所在目录
ssl_certificate_key ../ssl/server.key; # 这个是证书key文件所在目录

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root html; # 这个是指定一个项目所在目录
index index.html index.htm; # 这个是指定首页的文件名
}
}

正式证书配置

证书文件存放位置:conf/cert

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
isten 443 ssl;
server_name cs.bjtcrj.cn;
# ssl on;

ssl_certificate cert/9242785_cs.bjtcrj.cn.pem;
ssl_certificate_key cert/9242785_cs.bjtcrj.cn.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#表示使用的加密套件的类型。
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型,您需要自行评估是否配置TLSv1.1协议。
ssl_prefer_server_ciphers on;

location / {
root html; # 这个是指定一个项目所在目录
index index.html index.htm; # 这个是指定首页的文件名
}
}

防盗链

1
2
3
4
5
6
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}

支持 websocket

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
http {
#...... 其它配置
keepalive_timeout 65;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

server {
#.... 其它配置
location / {
#...其它配置
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}

压缩

1
2
3
4
5
6
7
#gzip  on;
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
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;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";

缓存

浏览器缓存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}

expires 30s; #缓存30秒
expires 30m; #缓存30分钟
expires 2h; #缓存2小时
expires 30d; #缓存30天

# 禁止缓存 *.html 文件
if ($request_filename ~* ^.*?.(html|htm)$) {
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
}

禁止访问某个目录

1
2
3
4
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}

redirect location 端口丢失的问题

原配置

1
proxy_set_header Host       $host;

修改为

1
proxy_set_header Host       $http_host;

完整

1
2
3
4
5
6
location / {
proxy_pass http://127.0.0.1:8080/;
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;
}

https服务兼容访问http资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#HTTPS server
server {
listen 7001 ssl;
server_name localhost;

ssl_certificate ../ssl/server.crt; # 这个是证书的crt文件所在目录
ssl_certificate_key ../ssl/server.key; # 这个是证书key文件所在目录

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

# https 服务兼容访问 http 资源配置
add_header Content-Security-Policy "upgrade-insecure-requests;connect-src *";

location / {
root html;
index index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html?s=$1 last;
break;
}
}

location ~* \.(jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
}

漏洞

Web服务器HTTP头信息公开

nginx标头漏洞 - 今年我十八岁 - 博客园 (cnblogs.com)

默认的nginx HTTP服务器设置

远程网络服务器包含默认设置,例如启用的服务器令牌和/或默认文件,例如默认索引或错误页面。这些项目可能会泄漏有关服务器安装的有用信息。

Web服务器错误页面信息泄露

远程Web服务器发送的默认错误页面公开了可以帮助攻击者的信息,例如Web服务器使用的服务器版本和语言

修复方案:

修改Nginx配置文件,http域中添加如下配置 【http { 内添加】:

1
2
3
4
server_tokens off;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options nosniff;

其它

热加载

如果已有请求正在处理,等当前请求处理完毕后关闭,如果没有请求正在处理,则直接关闭

1
nginx -s reload

原理:

  1. master主进程发送一个 HUP 信号
  2. master主进程检查配置文件语法的有效性
  3. master主进程应用新的配置,即打开新的日志文件和新的 socket 监听
    • 如果失败,将回滚配置更改并继续使用旧的配置
    • 如果成功了,开启新的工作进程 worker接收新的请求,并给旧的工作进程发关闭进程的消息
      • 如果旧worker工作进程正在处理请求,会等待请求处理完毕,然后关闭工作进程
      • 如果旧worker工作进程没有正在处理请求,则直接关闭工作进程

查看并发连接数

1
2
3
4
5
location /status {
stub_status on;
access_log logs/status.log;
auth_basic "NginxStatus";
}

后端服务器响应时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
location /connect-controller {
proxy_pass http://127.0.0.1:88; #请注意此处端口号不能与虚拟主机监听的端口号一样(也就是server监听的端口)
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;

#后端服务器连接的超时时间_发起握手等候响应超时时间
#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 90;

#后端服务器数据回传时间(代理发送超时)
#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
proxy_send_timeout 90;

#连接成功后,后端服务器响应时间(代理接收超时)
#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
proxy_read_timeout 90;
}