tanbushi commited on
Commit
09af5b8
·
1 Parent(s): fffb5c4
Files changed (5) hide show
  1. Dockerfile +12 -6
  2. README.md +23 -4
  3. nginx/conf.d/default.conf +27 -2
  4. nginx/html/index.html +32 -3
  5. nginx/nginx.conf +5 -2
Dockerfile CHANGED
@@ -1,14 +1,20 @@
1
- FROM nginx:latest
2
 
3
  # 复制自定义配置文件
4
- COPY nginx/nginx.conf /etc/nginx/nginx.conf
5
- COPY nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf
6
 
7
  # 复制静态文件
8
- COPY nginx/html/ /usr/share/nginx/html/
 
 
 
 
 
 
9
 
10
  # 暴露端口(Hugging Face Spaces 通常使用 7860)
11
  EXPOSE 7860
12
 
13
- # 启动 nginx,监听在 7860 端口
14
- CMD ["nginx", "-g", "daemon off;"]
 
1
+ FROM openresty/openresty:latest
2
 
3
  # 复制自定义配置文件
4
+ COPY nginx/nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
5
+ COPY nginx/conf.d/default.conf /usr/local/openresty/nginx/conf/conf.d/default.conf
6
 
7
  # 复制静态文件
8
+ COPY nginx/html/ /usr/local/openresty/nginx/html/
9
+
10
+ # 创建密码文件目录
11
+ RUN mkdir -p /usr/local/openresty/nginx/conf
12
+
13
+ # 创建简单的用户认证
14
+ RUN echo "admin:$(openssl passwd -crypt admin123)" > /usr/local/openresty/nginx/conf/.htpasswd
15
 
16
  # 暴露端口(Hugging Face Spaces 通常使用 7860)
17
  EXPOSE 7860
18
 
19
+ # 启动 OpenResty
20
+ CMD ["/usr/local/openresty/bin/openresty", "-g", "daemon off;"]
README.md CHANGED
@@ -7,17 +7,29 @@ sdk: docker
7
  pinned: false
8
  ---
9
 
10
- # Nginx 服务 for Hugging Face Space
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  ## 本地开发
13
 
14
- 使用 Docker Compose 运行 nginx 服务器:
15
 
16
  ```bash
17
  docker-compose up -d
18
  ```
19
 
20
- 访问 http://localhost:8080 查看服务
21
 
22
  停止服务:
23
  ```bash
@@ -26,7 +38,14 @@ docker-compose down
26
 
27
  ## Hugging Face Space 部署
28
 
29
- 此 Space 使用自定义 Dockerfile 配置 nginx 服务器,监听在 7860 端口(Hugging Face Spaces 默认端口)
 
 
 
 
 
 
 
30
 
31
  ## 配置文件说明
32
 
 
7
  pinned: false
8
  ---
9
 
10
+ # OpenResty 服务 with Authentication for Hugging Face Space
11
+
12
+ ## 功能特性
13
+
14
+ - 🔐 **基本认证**: HTTP Basic Authentication
15
+ - 🛡️ **Lua 安全**: 基于 Lua 脚本的访问控制
16
+ - 🚀 **OpenResty**: 高性能 Web 平台
17
+ - 📊 **健康检查**: 内置健康检查端点
18
+
19
+ ## 认证信息
20
+
21
+ - **Username**: `admin`
22
+ - **Password**: `admin123`
23
 
24
  ## 本地开发
25
 
26
+ 使用 Docker Compose 运行 OpenResty 服务器:
27
 
28
  ```bash
29
  docker-compose up -d
30
  ```
31
 
32
+ 访问 http://localhost:8080,使用上述凭据进行认证
33
 
34
  停止服务:
35
  ```bash
 
38
 
39
  ## Hugging Face Space 部署
40
 
41
+ 此 Space 使用 OpenResty Dockerfile,监听在 7860 端口,提供带认证的 Web 服务
42
+
43
+ ## 安全特性
44
+
45
+ 1. **Basic Authentication**: 保护所有页面
46
+ 2. **Lua 脚本过滤**: 阻止恶意 User-Agent
47
+ 3. **自定义响应头**: 识别服务类型
48
+ 4. **健康检查**: `/health` 端点无需认证
49
 
50
  ## 配置文件说明
51
 
nginx/conf.d/default.conf CHANGED
@@ -2,13 +2,38 @@ server {
2
  listen 7860;
3
  server_name localhost;
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  location / {
6
- root /usr/share/nginx/html;
7
  index index.html index.htm;
 
 
 
 
 
 
 
 
 
 
8
  }
9
 
10
  error_page 500 502 503 504 /50x.html;
11
  location = /50x.html {
12
- root /usr/share/nginx/html;
13
  }
14
  }
 
2
  listen 7860;
3
  server_name localhost;
4
 
5
+ # 基本认证
6
+ auth_basic "Restricted Area";
7
+ auth_basic_user_file /usr/local/openresty/nginx/conf/.htpasswd;
8
+
9
+ # 使用 Lua 进行更复杂的认证
10
+ access_by_lua_block {
11
+ local headers = ngx.req.get_headers()
12
+ local user_agent = headers["User-Agent"] or ""
13
+
14
+ -- 简单的用户代理检查
15
+ if string.find(user_agent, "bot") then
16
+ ngx.exit(403)
17
+ end
18
+ }
19
+
20
  location / {
21
+ root /usr/local/openresty/nginx/html;
22
  index index.html index.htm;
23
+
24
+ # 添加自定义响应头
25
+ add_header X-Powered-By "OpenResty";
26
+ add_header X-Auth-Type "Basic + Lua";
27
+ }
28
+
29
+ location /health {
30
+ access_log off;
31
+ return 200 "OK\n";
32
+ add_header Content-Type text/plain;
33
  }
34
 
35
  error_page 500 502 503 504 /50x.html;
36
  location = /50x.html {
37
+ root /usr/local/openresty/nginx/html;
38
  }
39
  }
nginx/html/index.html CHANGED
@@ -1,17 +1,46 @@
1
  <!DOCTYPE html>
2
  <html>
3
  <head>
4
- <title>Welcome to nginx!</title>
5
  <style>
6
  body {
7
  width: 35em;
8
  margin: 0 auto;
9
  font-family: Tahoma, Verdana, Arial, sans-serif;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  }
11
  </style>
12
  </head>
13
  <body>
14
- <h1>Welcome to nginx!</h1>
15
- <p>If you see this page, the nginx web server is successfully installed and working.</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  </body>
17
  </html>
 
1
  <!DOCTYPE html>
2
  <html>
3
  <head>
4
+ <title>OpenResty with Authentication</title>
5
  <style>
6
  body {
7
  width: 35em;
8
  margin: 0 auto;
9
  font-family: Tahoma, Verdana, Arial, sans-serif;
10
+ padding: 2em;
11
+ background-color: #f5f5f5;
12
+ }
13
+ .container {
14
+ background: white;
15
+ padding: 2em;
16
+ border-radius: 8px;
17
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
18
+ }
19
+ .auth-info {
20
+ background: #e7f3ff;
21
+ padding: 1em;
22
+ border-radius: 4px;
23
+ margin: 1em 0;
24
  }
25
  </style>
26
  </head>
27
  <body>
28
+ <div class="container">
29
+ <h1>🔐 OpenResty with Authentication</h1>
30
+ <p>Welcome to a secure OpenResty server!</p>
31
+
32
+ <div class="auth-info">
33
+ <h3>Authentication Details:</h3>
34
+ <ul>
35
+ <li><strong>Username:</strong> admin</li>
36
+ <li><strong>Password:</strong> admin123</li>
37
+ <li><strong>Auth Method:</strong> Basic HTTP + Lua Script</li>
38
+ </ul>
39
+ </div>
40
+
41
+ <p>This server is protected with both basic authentication and Lua-based access control.</p>
42
+
43
+ <p><a href="/health">Health Check</a></p>
44
+ </div>
45
  </body>
46
  </html>
nginx/nginx.conf CHANGED
@@ -3,11 +3,14 @@ events {
3
  }
4
 
5
  http {
6
- include /etc/nginx/mime.types;
7
  default_type application/octet-stream;
8
 
9
  sendfile on;
10
  keepalive_timeout 65;
11
 
12
- include /etc/nginx/conf.d/*.conf;
 
 
 
13
  }
 
3
  }
4
 
5
  http {
6
+ include /usr/local/openresty/nginx/conf/mime.types;
7
  default_type application/octet-stream;
8
 
9
  sendfile on;
10
  keepalive_timeout 65;
11
 
12
+ # Lua 模块配置
13
+ lua_package_path "/usr/local/openresty/nginx/conf/?.lua;;";
14
+
15
+ include /usr/local/openresty/nginx/conf/conf.d/*.conf;
16
  }