🤖 Ai回答: `nginx njs` refers to Nginx JavaScript (njs), which is a lightweight JavaScript engine developed by the Nginx team、It is designed to run JavaScript code directly within the Nginx web server, enabling powerful server-side scripting capabilities without requiring external processes or modules.
🔹 What is Nginx njs?
njs (Nginx JavaScript) is a JavaScript engine that allows you to write and execute JavaScript code inside Nginx、It is based on NJS, which is a subset of ECMAScript (JavaScript) and optimized for high performance and low memory usage.
It is used primarily for server-side logic in Nginx, such as:
Request/response manipulation
Dynamic content generation
API routing
Authentication and authorization
Load balancing decisions
Logging and monitoring
🔹 Key Features
| Feature | Description |
| ✅ Embedded JavaScript | Run JavaScript directly in Nginx configuration files、|
| ✅ High Performance | Lightweight and fast, suitable for real-time processing、|
| ✅ Event-driven | Works well with Nginx’s event-driven architecture、|
| ✅ No External Dependencies | Runs natively inside Nginx, no need for Node.js or other runtimes、|
| ✅ Built-in Modules | Includes modules for HTTP, TCP, UDP, and more、|
You need to compile Nginx with njs support or use a distribution that includes it.
bash
Example: Compile Nginx with njs
./configure add-module=/path/to/nginx-njs
make && make install
Or use a package manager (e.g., on Ubuntu/Debian):
bash
sudo apt install nginx-extras
Note: `nginx-extras` often includes njs support.
2、Basic Configuration Example
nginx
http {
js_import myscript.js;
server {
listen 8080;
location / {
js_content myscript.handler;
}
}
}
3、JavaScript File (`myscript.js`)
js
function handler(r) {
r.return(200, Hello from Nginx njs!);
}
export default { handler };
🔹 Available Objects and APIs
| Object | Description |
|
| `r` | Request object (contains `variables`, `headers`, `body`, etc.) |
| `req` | Alias for `r` (request object) |
| `res` | Response object |
| `variables` | Request variables (e.g., `remote_addr`, `uri`, `host`) |
| `headers` | Request and response headers |
| `return(code, body)` | Send response with status and body |
| `log` | Logging functions (`log`, `error`, `warn`, etc.) |
| `set` | Set variables or headers |
🔹 Example: Dynamic Response Based on User Agent
js
function handler(r) {
var ua = r.variables.http_user_agent;
if (ua.match(/Mobile/)) {
r.return(200, Mobile view);
} else {
r.return(200, Desktop view);
}
}
🔹 Limitations
Not full ECMAScript: njs supports a subset of JavaScript (ES5/ES6 features limited).
No DOM or browser APIs: It's server-side only.
Limited standard library: No `fetch`, `setTimeout`, etc., unless added via modules.
Debugging: Limited tools compared to Node.js.
| Embedded in Nginx | ✅ Yes | ❌ No | ✅ Yes (OpenResty) |
| Performance | ⚡ Fast | Fast | ⚡ Fast |
| Full JS Support | ❌ Limited | ✅ Full | ❌ Limited |
| Ease of Use | Moderate | Easy | Moderate |
| Use Case | Lightweight logic in Nginx | Full backend services | Nginx scripting |
🔹 Conclusion
Nginx njs is ideal when you need lightweight, high-performance server-side scripting directly within Nginx、It's perfect for:
API gateways
Dynamic routing
Simple authentication
Request/response transformation
It's especially useful in microservices, edge computing, and high-performance web servers where you want to avoid spawning external processes.
🔹 Resources
Official GitHub: [https://github.com/nginx/njs](https://github.com/nginx/njs)
Documentation: [https://nginx.org/en/docs/njs/](https://nginx.org/en/docs/njs/)
OpenResty (Lua-based alternative): [https://openresty.org](https://openresty.org)
Let me know if you'd like a step-by-step tutorial, real-world example, or help debugging a specific njs script!