---
title: "@push.rocks/smartnginx"
---
# @push.rocks/smartnginx
control nginx from node, TypeScript ready

## Install
To install `@push.rocks/smartnginx`, you can use npm (Node Package Manager). Open your terminal and run:
```bash
npm install @push.rocks/smartnginx --save
```
This will download and install `@push.rocks/smartnginx` and its dependencies into your project's `node_modules` folder and save it as a dependency in your project's `package.json` file.

## Usage
`@push.rocks/smartnginx` is a powerful library for interacting with Nginx programmatically using Node.js and TypeScript. It simplifies tasks such as configuring hosts, deploying configurations, and managing SSL certificates. Below is a comprehensive guide to using the library effectively in your TypeScript projects.

### Getting Started
First, ensure you have imported the library into your TypeScript file. Use ESM syntax as shown:
```typescript
import { SmartNginx, NginxHost } from '@push.rocks/smartnginx';
```

### Initialize SmartNginx
Before you interact with Nginx, you need to create an instance of `SmartNginx`. This object acts as the main interface to your Nginx server. You can specify a default proxy URL that requests will be redirected to if no matching host is found.
```typescript
const smartNginx = new SmartNginx({
  defaultProxyUrl: 'https://your-default-url.com'
});
```

### Add Host Candidates
To serve content or applications via Nginx, you will define hosts. Each host corresponds to a domain or subdomain and can be configured with specific rules. Here's how to add host candidates:
```typescript
const myHost = smartNginx.addHostCandidate({
  hostName: 'example.com',
  destination: 'localhost',
  destinationPort: 8080,
  privateKey: '<Your SSL Private Key>',
  publicKey: '<Your SSL Public Key>'
});
```
Replace `'example.com'`, `'localhost'`, `8080`, `'<Your SSL Private Key>'`, and `'<Your SSL Public Key>'` with your actual host name, destination IP or hostname, port number, and SSL keys respectively.

### Deploying Configuration
After adding all your host candidates, you will need to apply these configurations for Nginx to recognize and use them. Deploy the configuration as follows:
```typescript
await smartNginx.deploy();
```
This method checks for any changes in your host configurations compared to what's currently deployed and updates the Nginx configuration accordingly.

### Managing SSL Certificates
When setting up SSL for your hosts, you will provide the paths to the private key and public certificate. It's essential to ensure these files are securely stored and accessible by the library during deployment.

### Handling Multiple Hosts
You can add multiple host candidates using `addHostCandidate` method for different domains or subdomains, each with unique configurations. Here's an example of adding another host:
```typescript
const anotherHost = smartNginx.addHostCandidate({
  hostName: 'sub.example.com',
  destination: 'localhost',
  destinationPort: 9090,
  privateKey: '<Another SSL Private Key>',
  publicKey: '<Another SSL Public Key>'
});
```

### Reloading Configurations
If at any time you make changes to your host configurations and need to apply these changes, simply call the `deploy` method again. `@push.rocks/smartnginx` efficiently detects changes and reloads Nginx with the new configurations.

### Stopping SmartNginx
To stop the Nginx process managed by `@push.rocks/smartnginx`, use:
```typescript
await smartNginx.stop();
```
Bear in mind that this might affect your web services if they rely on the Nginx instance you are stopping.

### Conclusion
`@push.rocks/smartnginx` abstracts away much of the complexity involved in managing Nginx configurations, offering a TypeScript-ready solution for Node.js projects. With simple method calls, you can automate and manage your Nginx server programmatically, making it an excellent tool for developers seeking to integrate Nginx management into their applications or deployment workflows.

## License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. 

**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

### Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

### Company Information

Task Venture Capital GmbH  
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
