{"id":15014225,"url":"https://github.com/houbb/nginx4j","last_synced_at":"2025-08-20T18:32:23.810Z","repository":{"id":241366366,"uuid":"804966149","full_name":"houbb/nginx4j","owner":"houbb","description":"nginx4j 是 nginx 的java 版本。","archived":false,"fork":false,"pushed_at":"2024-07-27T09:58:30.000Z","size":421,"stargazers_count":44,"open_issues_count":3,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-12-09T12:42:43.552Z","etag":null,"topics":["apache","http","jboss","jetty","netty","nginx","proxy","reverse-proxy","tomcat","web","web-server"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/houbb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGE_LOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-23T16:06:36.000Z","updated_at":"2024-11-28T08:17:47.000Z","dependencies_parsed_at":"2024-06-08T15:40:02.681Z","dependency_job_id":"c3d692c7-450f-44ca-b2ec-d3d437c0c8ae","html_url":"https://github.com/houbb/nginx4j","commit_stats":{"total_commits":61,"total_committers":3,"mean_commits":"20.333333333333332","dds":"0.21311475409836067","last_synced_commit":"539d561afdcfe7b6d01530a4db41222d1c9a309c"},"previous_names":["houbb/nginx4j"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houbb%2Fnginx4j","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houbb%2Fnginx4j/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houbb%2Fnginx4j/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houbb%2Fnginx4j/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/houbb","download_url":"https://codeload.github.com/houbb/nginx4j/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230445926,"owners_count":18227060,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apache","http","jboss","jetty","netty","nginx","proxy","reverse-proxy","tomcat","web","web-server"],"created_at":"2024-09-24T19:45:20.840Z","updated_at":"2024-12-19T14:06:53.124Z","avatar_url":"https://github.com/houbb.png","language":"Java","funding_links":[],"categories":["Web服务器"],"sub_categories":["微服务框架"],"readme":"# 项目简介\n\nnginx4j 是基于 netty 实现的 nginx 的java 版本。\n\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.houbb/nginx4j/badge.svg)](http://mvnrepository.com/artifact/com.github.houbb/nginx4j)\n[![Build Status](https://www.travis-ci.org/houbb/nginx4j.svg?branch=master)](https://www.travis-ci.org/houbb/nginx4j?branch=master)\n[![Coverage Status](https://coveralls.io/repos/github/houbb/nginx4j/badge.svg?branch=master)](https://coveralls.io/github/houbb/nginx4j?branch=master)\n\n如果你想知道 servlet 如何处理的，可以参考我的另一个项目：\n\n\u003e  [简易版 tomcat](https://github.com/houbb/minicat)\n\n# 特性\n\n- 完全兼容 nginx.conf 的配置文件格式\n\n- 基于 netty 的 nio 高性能处理\n\n- 静态网资源支持\n\n- 默认 index.html\n\n- 404\n\n- 文件夹的自动索引\n\n- 大文件的下载支持\n\n- gzip 压缩\n\n- sendfile 零拷贝特性\n\n- http keep-alive 特性\n\n- 支持多 server \n\n- 请求头的修改+响应头的修改\n\n- 常见占位符 `$` 的内置支持\n\n- cookie 的操作处理 proxy_cookie_domain/proxy_cookie_flags/proxy_cookie_path 内置支持 \n\n- proxy_pass 反向代理实现\n\n# 变更日志\n\n\u003e [变更日志](CHANGE_LOG.md)\n\n# 快速开始\n\n## maven 依赖\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.houbb\u003c/groupId\u003e\n    \u003cartifactId\u003enginx4j\u003c/artifactId\u003e\n    \u003cversion\u003e0.27.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## 启动测试\n\n### 配置文件\n\n```conf\n# nginx.conf\n\n# 定义运行Nginx的用户和组\nuser nginx;\n\n# 主进程的PID文件存放位置\npid /var/run/nginx.pid;\n\n# 事件模块配置\nevents {\n    worker_connections 1024;  # 每个工作进程的最大连接数\n}\n\n# HTTP模块配置\nhttp {\n    include /etc/nginx/mime.types;  # MIME类型配置文件\n    default_type application/octet-stream;  # 默认的MIME类型\n\n    # 文件传输设置\n    sendfile on;  # 开启高效文件传输\n    # Keepalive超时设置\n    keepalive_timeout 65;\n\n    # 定义服务器块\n    server {\n        listen 8080;\n        server_name 192.168.1.12:8080;  # 服务器域名\n\n        # 单独为这个 server 启用 sendfile\n        sendfile on;\n\n        # 静态文件的根目录\n        root D:\\data\\nginx4j;  # 静态文件存放的根目录\n        index index.html index.htm;  # 默认首页\n\n        # 如果需要为这个 server 单独配置 gzip，可以覆盖全局配置\n        gzip on;\n        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;\n\n        # 默认匹配\n        location / {\n            proxy_set_header X-DEFINE-PARAM myDefineParam;\n            proxy_set_header X-DEFINE-HOST 127.0.0.1;\n\n            # 增加或修改响应头 这里就提现了一些占位符的强大之处。下一次可以考虑支持\n            add_header X-Response-Time 2024-06-08;\n\n            # 删除响应头\n            proxy_hide_header X-Unwanted-Header;\n\n            # 占位符测试 v0.17.0\n            set $myVal 1000;\n            add_header X-MY-VAL $myVal;\n            add_header X-MY-HOST $host;\n        }\n    }\n\n    # 定义服务器块2\n    server {\n        listen 8081;\n        server_name 192.168.1.12:8081;  # 服务器域名\n\n        # 单独为这个 server 启用 sendfile\n        sendfile on;\n\n        # 静态文件的根目录\n        root D:\\data\\nginx4j;  # 静态文件存放的根目录\n        index index.txt; # 默认首页\n\n        # 默认匹配\n        location / {\n            proxy_set_header X-DEFINE-PARAM myDefineParam;\n            proxy_set_header X-DEFINE-HOST 127.0.0.2;\n\n            # 增加或修改响应头 这里就提现了一些占位符的强大之处。下一次可以考虑支持\n            add_header X-Response-Time 2024-06-09;\n\n            # 删除响应头\n            proxy_hide_header X-Unwanted-Header;\n        }\n    }\n\n}\n```\n\n\n### 启动代码\n\n```java\n// 指定配置文件的位置\nNginxUserConfig nginxUserConfig = NginxUserConfigLoaders.configFile(\"D:\\\\github\\\\nginx4j\\\\src\\\\main\\\\resources\\\\nginx.conf\").load();\n\nNginx4jBs.newInstance()\n.nginxUserConfig(nginxUserConfig)\n.init()\n.start();\n```\n\n启动日志：\n\n```\n[DEBUG] [2024-05-24 23:40:37.573] [main] [c.g.h.l.i.c.LogFactory.setImplementation] - Logging initialized using 'class com.github.houbb.log.integration.adaptors.stdout.StdOutExImpl' adapter.\n[INFO] [2024-05-24 23:40:37.576] [main] [c.g.h.n.s.s.NginxServerSocket.start] - [Nginx4j] listen on port=8080\n```\n\n页面访问：[http://127.0.0.1:8080](http://127.0.0.1:8080)\n\n响应：\n\n```\nWelcome to nginx4j!\n```\n\n## 其他页面\n\n访问 [http://localhost:8080/1.txt](http://localhost:8080/1.txt)\n\n将返回对应的文件内容：\n\n```\nhello nginx4j!\n```\n\n## 不存在\n\nhttp://localhost:8080/asdfasdf\n\n返回：\n\n```\n404 Not Found: The requested resource was not found on this server.\n```\n\n## gzip\n\n```java\nNginxUserConfig nginxUserConfig = NginxUserConfigLoaders.configFile(\"D:\\\\github\\\\nginx4j\\\\src\\\\main\\\\resources\\\\nginx.conf\").load();\n\n Nginx4jBs.newInstance()\n .nginxUserConfig(nginxUserConfig)\n .init()\n .start();\n```\n\n可以开启 gzip 的处理。\n\n## 拓展阅读\n\n[从零手写实现 nginx-01-为什么不能有 java 版本的 nginx?](https://houbb.github.io/2018/11/22/nginx-write-01-how-to)\n\n[从零手写实现 nginx-02-nginx 的核心能力](https://houbb.github.io/2018/11/22/nginx-write-02-basic-http)\n\n[从零手写实现 nginx-03-nginx 基于 Netty 实现](https://houbb.github.io/2018/11/22/nginx-write-03-basic-http-netty)\n\n[从零手写实现 nginx-04-基于 netty http 出入参优化处理](https://houbb.github.io/2018/11/22/nginx-write-04-netty-http-optimize)\n\n[从零手写实现 nginx-05-MIME类型（Multipurpose Internet Mail Extensions，多用途互联网邮件扩展类型）](https://houbb.github.io/2018/11/22/nginx-write-05-mime-type)\n\n[从零手写实现 nginx-06-文件夹自动索引](https://houbb.github.io/2018/11/22/nginx-write-06-dir-list)\n\n[从零手写实现 nginx-07-大文件下载](https://houbb.github.io/2018/11/22/nginx-write-07-big-file)\n\n[从零手写实现 nginx-08-范围查询](https://houbb.github.io/2018/11/22/nginx-write-08-range)\n\n[从零手写实现 nginx-09-文件压缩](https://houbb.github.io/2018/11/22/nginx-write-09-comparess)\n\n[从零手写实现 nginx-10-sendfile 零拷贝](https://houbb.github.io/2018/11/22/nginx-write-10-sendfile)\n\n[从零手写实现 nginx-11-file+range 合并](https://houbb.github.io/2018/11/22/nginx-write-11-file-and-range-merge)\n\n[从零手写实现 nginx-12-keep-alive 连接复用](https://houbb.github.io/2018/11/22/nginx-write-12-keepalive)\n\n[从零手写实现 nginx-13-nginx.conf 配置文件介绍](https://houbb.github.io/2018/11/22/nginx-write-13-nginx-conf-intro)\n\n[从零手写实现 nginx-14-nginx.conf 和 hocon 格式有关系吗？](https://houbb.github.io/2018/11/22/nginx-write-14-nginx-conf-hocon)\n\n[从零手写实现 nginx-15-nginx.conf 如何通过 java 解析处理？](https://houbb.github.io/2018/11/22/nginx-write-15-nginx-conf-parser)\n\n[从零手写实现 nginx-16-nginx 支持配置多个 server](https://houbb.github.io/2018/11/22/nginx-write-16-nginx-conf-multi-server)\n\n[从零手写实现 nginx-17-nginx 默认配置优化](https://houbb.github.io/2018/11/22/nginx-write-17-nginx-conf-global-default)\n\n[从零手写实现 nginx-18-nginx 请求头+响应头操作](https://houbb.github.io/2018/11/22/nginx-write-18-nginx-conf-header-oper)\n\n[从零手写实现 nginx-19-nginx cors](https://houbb.github.io/2018/11/22/nginx-write-19-cors)\n\n[从零手写实现 nginx-20-nginx 占位符 placeholder](https://houbb.github.io/2018/11/22/nginx-write-20-placeholder)\n\n[从零手写实现 nginx-21-nginx modules 模块信息概览](https://houbb.github.io/2018/11/22/nginx-write-21-modules-overview)\n\n[从零手写实现 nginx-22-nginx modules 分模块加载优化](https://houbb.github.io/2018/11/22/nginx-write-22-modules-load)\n\n[从零手写实现 nginx-23-nginx cookie 的操作处理](https://houbb.github.io/2018/11/22/nginx-write-23-cookie-oper)\n\n[从零手写实现 nginx-24-nginx IF 指令](https://houbb.github.io/2018/11/22/nginx-write-24-directives-if)\n\n[从零手写实现 nginx-25-nginx map 指令](https://houbb.github.io/2018/11/22/nginx-write-25-directives-map)\n\n[从零手写实现 nginx-26-nginx rewrite 指令](https://houbb.github.io/2018/11/22/nginx-write-26-directives-rewrite)\n\n[从零手写实现 nginx-27-nginx return 指令](https://houbb.github.io/2018/11/22/nginx-write-27-directives-return)\n\n[从零手写实现 nginx-28-nginx error_pages 指令](https://houbb.github.io/2018/11/22/nginx-write-28-directives-error-pages)\n\n[从零手写实现 nginx-29-nginx try_files 指令](https://houbb.github.io/2018/11/22/nginx-write-29-directives-try_files)\n\n[从零手写实现 nginx-30-nginx proxy_pass upstream 指令](https://houbb.github.io/2018/11/22/nginx-write-30-proxy-pass)\n\n[从零手写实现 nginx-31-nginx load-balance 负载均衡](https://houbb.github.io/2018/11/22/nginx-write-31-load-balance)\n\n[从零手写实现 nginx-32-nginx load-balance 算法 java 实现](https://houbb.github.io/2018/11/22/nginx-write-32-load-balance-java-impl)\n\n[从零手写实现 nginx-33-nginx http proxy_pass 测试验证](https://houbb.github.io/2018/11/22/nginx-write-33-http-proxy-pass-test)\n\n[从零手写实现 nginx-34-proxy_pass 配置加载处理](https://houbb.github.io/2018/11/22/nginx-write-34-http-proxy-pass-config-load)\n\n[从零手写实现 nginx-35-proxy_pass netty 如何实现？](https://houbb.github.io/2018/11/22/nginx-write-35-http-proxy-pass-netty)\n\n# ROAD-MAP\n\n## static\n\n- [x] 基于 netty 实现\n- [x] index.html\n- [x] 404 403 等常见页面\n- [x] 基于 netty 的请求/响应封装\n- [x] 各种文件类型的请求头处理\n- [x] 文件夹的自动索引\n- [x] 大文件的分段传输？chunk\n- [x] range 范围请求\n- [x] 请求的压缩 gzip 等常见压缩算法\n- [x] sendFile 特性支持\n- [x] range 的代码合并到 file\n- [x] http keep-alive\n- [x] 配置的标准 POJO\n- [x] nginx.conf 的解析=》POJO\n- [x] http 全局的默认配置属性\n- [x] 请求头信息重写\n- [x] CORS 这个还是让用户处理，不过可以单独写一篇文章\n- [x] $ 占位符的实现\n- [x] 常见 cookie 的处理\n- [x] if 指令的支持\n- [x] map 变量修改指令\n- [x] rewrite 指令，重写 URL\n- [x] return 返回指令\n- [x] error_page 自定义错误页面\n- [x] try_files 文件处理指令\n- [ ] 更多 directive 指令实现\n- [ ] 更多文件格式的内置支持？\n- [ ] ETag 和 Last-Modified + cache 相关\n- [ ] 压缩更好的实现方式？ zlib 算法 + 实现优化？\n- [ ] http2\n- [ ] http3\n- [ ] ssl/https\n- [ ] 安全 访问限制\n\n## 反向代理\n\n- [x] reverse-proxy\n- [x] load-balance\n\n## system\n\n- [ ] cache\n- [ ] rateLimit 限流\n- [ ] filter 过滤器\n- [ ] listener 监听器\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoubb%2Fnginx4j","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhoubb%2Fnginx4j","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoubb%2Fnginx4j/lists"}