本文最后更新于 239 天前 ,文中信息可能已经过时。如有问题请在评论区留言。

Docker Compose

以下是 Docker Compose 示例:

yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
version: "3.9"

services:
  npm:
    image: 'jc21/nginx-proxy-manager:2.11.3'
    container_name: npm
    hostname: npm
    restart: unless-stopped
    network_mode: host
    volumes:
      - /docker/nginx-proxy-manager/data:/data
      - /docker/nginx-proxy-manager/letsencrypt:/etc/letsencrypt

注意事项:

  • 上述配置网络使用的 HOST 模式,如果端口 (80、81、443) 被占用,请修改为端口映射。

Login

前往 http://localhost:81,使用如下默认账号/密码登录:

Default Usernameadmin@example.com
Default Passwordchangeme
登录后请先修改个人信息。

FAQs

SSL Certificates

SSL Certificates 配置证书 (Let’s Encrypt) 时,提示缺少 zope。当使用 DNSPod 时一般会提示此错误。

解决方式:(安装 zope)

  1. 进入容器
shell
1
docker exec -it npm bash
  1. 执行如下代码安装 zope 模块
shell
1
pip install zope -i https://pypi.tuna.tsinghua.edu.cn/simple

Nginx Proxy Manager 配置说明

主要介绍 Nginx Proxy Manager 界面操作对应 Nginx 中的配置。

Nginx 路径

Nginx 存放在容器路径: /etc/nginx

Add Proxy Host

设置反向代理时,可以勾选一些常用配置,勾选后会在相应的配置文件中添加对应配置内容。

配置文件路径:/data/nginx/proxy_host,可通过界面查看对应的配置。

如下图: #22 对应 22.conf,则勾选对应配置时,修改 22.conf 的内容。

Details

配置含义
Cache Assets缓存
Block Common Exploits阻止常见漏洞
Websockets Support支持 WS

Cache Assets

设置缓存,启用该项后,会添加如下内容:

text
1
2
# Asset Caching
include conf.d/include/assets.conf;

assets.conf 完整内容如下:

容器内绝对路径:/etc/nginx/conf.d/include/assets.conf

conf
/etc/nginx/conf.d/include/assets.conf
 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
location ~* ^.*\.(css|js|jpe?g|gif|png|webp|woff|eot|ttf|svg|ico|css\.map|js\.map)$ {
        if_modified_since off;

        # use the public cache
        proxy_cache public-cache;
        proxy_cache_key $host$request_uri;

        # ignore these headers for media
        proxy_ignore_headers Set-Cookie Cache-Control Expires X-Accel-Expires;

        # cache 200s and also 404s (not ideal but there are a few 404 images for some reason)
        proxy_cache_valid any 30m;
        proxy_cache_valid 404 1m;

        # strip this header to avoid If-Modified-Since requests
        proxy_hide_header Last-Modified;
        proxy_hide_header Cache-Control;
        proxy_hide_header Vary;

        proxy_cache_bypass 0;
        proxy_no_cache 0;

        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504 http_404;
        proxy_connect_timeout 5s;
        proxy_read_timeout 45s;

        expires @30m;
        access_log  off;

        include conf.d/include/proxy.conf;
}

proxy.conf 完整内容如下:

容器内绝对路径:/etc/nginx/conf.d/include/proxy.conf

text
1
2
3
4
5
6
7
add_header       X-Served-By $host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-Proto  $scheme;
proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP          $remote_addr;
proxy_pass       $forward_scheme://$server:$port$request_uri;

Block Common Exploits

阻止常见的漏洞,启用该配置后,会添加如下内容:

text
1
2
# Block Exploits
include conf.d/include/block-exploits.conf;

block-exploits.conf 完整内容如下:

容器内绝对路径:/etc/nginx/conf.d/include/block-exploits.conf

conf
/etc/nginx/conf.d/include/block-exploits.conf
  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
## 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 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;
}

## Block common exploits
set $block_common_exploits 0;

if ($query_string ~ "(<|%3C).*script.*(>|%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 403;
}

## Block 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|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;
}

## Block user agents
set $block_user_agents 0;

# 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;
}

Websockets Support

支持 Websocket,启用该配置后,会添加如下内容:

text
1
2
3
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_http_version 1.1;

SSL

配置含义
Force SSL强制 SSL
HTTP/2 Support支持 HTTP/2
HSTS Enabled启用 HTTP 严格传输安全
HSTS Subdomains启用 HTTP 严格传输安全(包含子域)

Force SSL

强制 SSL,启用该配置后,会添加如下内容:

text
1
2
# Force SSL
include conf.d/include/force-ssl.conf;

force-ssl.conf 完整内容如下:

容器内绝对路径:/etc/nginx/conf.d/include/force-ssl.conf

text
1
2
3
if ($scheme = "http") {
        return 301 https://$host$request_uri;
}

HSTS Enabled & HSTS Subdomains

HSTS Enabled 启用 HTTP 严格传输安全,启用该配置,会添加如下内容:

text
1
2
# HSTS (ngx_http_headers_module is required) (63072000 seconds = 2 years)
add_header Strict-Transport-Security "max-age=63072000; preload" always;

如果启用了 HSTS Subdomains,则会额外添加 includeSubDomains;,最终内容如下:

text
1
2
# HSTS (ngx_http_headers_module is required) (63072000 seconds = 2 years)
add_header Strict-Transport-Security "max-age=63072000;includeSubDomains; preload" always;

以下内容摘自 Wikipedia

HTTP 严格传输安全(HTTP Strict Transport Security,HSTS)是一套由互联网工程任务组发布的互联网安全策略机制。网站可以选择使用 HSTS 策略,来让浏览器强制使用 HTTPS 与网站进行通信,以减少会话劫持风险。

其征求修正意见书文件编号是 RFC 6797,发布于2012年11月。

HSTS 的作用是强制客户端(如浏览器)使用 HTTPS 与服务器建立连接。服务器开启 HSTS 的方法是,当客户端通过 HTTPS 发出请求时,在服务器返回的超文本传输协议(HTTP)响应头中包含 Strict-Transport-Security 字段。非加密传输时设置的 HSTS 字段无效。

比如,https://example.com/ 的响应头含有 Strict-Transport-Security: max-age=31536000; includeSubDomains。这意味着两点:

  1. 在接下来的 31536000 秒(即一年)中,浏览器向example.com或其子域名发送HTTP请求时,必须采用HTTPS来发起连接。比如,用户点击超链接或在地址栏输入 http://www.example.com/ ,浏览器应当自动将 http 转写成 https,然后直接向 https://www.example.com/ 发送请求。
  2. 在接下来的一年中,如果 example.com 服务器发送的 TLS 证书无效,用户不能忽略浏览器警告继续访问网站。