# NGINX

## .NET Core

Wisej.NET applications targeting .NET Core can follow Microsoft's instructions for deploying an ASP.NET Core application to Linux with NGINX.

{% embed url="<https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-7.0&tabs=linux-ubuntu>" %}

Below you will find some useful information related to Wisej.NET-specific deployments.&#x20;

:point\_right: If you are deploying on a [Linux ](https://docs.wisej.com/deployment/targets/linux-macos)distribution or on [MacOS](https://docs.wisej.com/deployment/targets/linux-macos), make sure [libgdiplus ](https://www.mono-project.com/docs/gui/libgdiplus/)is installed.

{% hint style="success" %}
Starting with Wisej.NET 4, the use of `libgdiplus` is no longer necessary. This change is due to the development of a new managed System.Drawing library, which has been created in conjunction with [ImageSharp](https://sixlabors.com/products/imagesharp/).
{% endhint %}

### Basic Steps

1. After generating a build using the **Visual Studio Publishing Tool**, copy the **executable**, **resource files**, and **referenced DLLs** to the folder they will run in.
2. Test that the deployment works using the **dotnet start AppName** command.

{% hint style="warning" %}
If the application is deployed as an executable, ensure the executable has the **execute** permission and run **sudo ./AppName**.
{% endhint %}

3\. After verifying the app runs, you will need to [configure the app to run on startup](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-6.0#create-the-service-file). **systemd** can be used to create a service file to start and monitor the underlying web app.

{% hint style="info" %}
**systemd** is an init system that provides many powerful features for starting, stopping, and managing processes on Linux systems. [Click here to make a service file.](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-6.0#create-the-service-file)
{% endhint %}

All NGINX needs is an address and a port. In the [nginx.conf](https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/) file define the URL for your application and assign the **URL:port** of the actual Wisej application to the **proxy\_pass** setting.

```nginx
http {
    
    server {
        listen 80;
        
        location /myapp/ {
            # Location of the Wisej server. 
            proxy_pass http://10.1.10.112:43662; 
        }
    }
}
```

{% hint style="danger" %}
The configuration in this book are minimal examples, use the full NGINX guide to determine how to be configure your NGINX server.
{% endhint %}

{% embed url="<https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy>" %}
NGINX Reverse Proxy Setup
{% endembed %}

This is all you need for NGINX to dispatch the requests to the Wisej.NET application on .NET Core.

### Security

When deploying on .NET Core, you are unable to use the `web.config` file to configure the `System.Web.HttpForbiddenHandler`, which is typically used to block the downloading of potentially sensitive `.json` files. As a result, these `.json` files could become accessible for downloading, which might pose a security vulnerability.

Since ASP.NET Core relies on middleware modules instead of using traditional HttpHandlers and HttpModules, to secure specific files or directories, you need to modify the configuration in the `Startup.cs` file. Adjusting the middleware settings within `Startup.cs` allows you to implement security measures to protect sensitive files or directories effectively.

Below is an example of how to block access to specific files or directories in ASP.NET Core, based on your security requirements. You can customize the middleware configuration in the `Startup.cs` file to meet your particular security needs.

{% code title="Conditional UseFileServer" %}

```csharp
// app.UseFileServer();
app.UseWhen(
 cx => !cx.Request.Path.Value.EndsWith(".json", StringComparison.OrdinalIgnoreCase), 
 app => app.UseFileServer()
);
```

{% endcode %}

## .NET Framework

Deploying a Wisej application on [NGINX ](https://www.nginx.com/)is similar the the [IIS ](https://docs.wisej.com/deployment/targets/iis)deployment in regards to the files to copy to the production machine:

### Basic Steps

1. Create the application directory on the deployment server
2. Create the **/bin** directory in the application's directory on the deployment server
3. Copy all the assemblies from your local /bin to the server's **/bin** (no need to copy xml and pdb files)
4. Copy the **/Themes** folder if you have custom themes or mixins
5. Copy the **/Images** folder if you have images that need to be served as URLs
6. Copy the **/App\_Data** folder if your application uses it
7. Copy **Web.config**
8. Copy **Default.html** (and other html files you use sub-applications)
9. Copy **Default.json** (and other json configuration files if you use sub-applications)
10. Copy **favicon.ico**.

{% hint style="warning" %}
Notice that step 1 is different from IIS because **NGINX never loads the actual application**, instead it acts as a [Reverse Proxy](https://www.nginx.com/resources/glossary/reverse-proxy-server) dispatching requests to the web application listening on a another internal port.
{% endhint %}

The actual Wisej application must be running and listening for requests in a separate process on the local machine or a remote machine. It can be either another [IIS ](https://docs.wisej.com/deployment/iis#configuration-file)process, or a [Self Hosted](https://docs.wisej.com/deployment/self-hosting#service) process (may also run in a Docker container).

All NGINX needs is an address and a port. In the [nginx.conf](https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/) file define the URL for your application and assign the **URL:port** of the actual Wisej application to the **proxy\_pass** setting.

```nginx
http {
    
    server {
        listen 80;
        
        location /myapp/ {
            # Location of the Wisej server. 
            proxy_pass http://10.1.10.112:43662; 
        }
    }
}
```

{% hint style="danger" %}
The configuration in this book are minimal examples, use the full NGINX guide to determine how to be configure your NGINX server.
{% endhint %}

{% embed url="<https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy>" %}
NGINX Reverse Proxy Setup
{% endembed %}

This is all you need for NGINX to dispatch the requests to the Wisej.NET application on .NET Framework.

## Load Balancer

NGINX can also serve as a load balancer out of the box. Instead of setting the **proxy\_pass** property to the actual Wisej server, assign it to an **upstream** configuration group that defines the load balancing rotation.

```nginx
http {
    
    upstream myapp1 {
        ip_hash;
        server 10.1.10.1:52433;
        server 10.1.10.1:52434;
        server 10.1.10.2:52000;
        server 10.1.10.2:52001;
    }
    
    server {
        listen 80;
        
        location /myapp/ {
            # Location of the Wisej server. 
            proxy_pass http://myapp1; 
        }
    }
}
```

{% embed url="<https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer>" %}
NGING Load Balancing Setup
{% endembed %}

{% hint style="info" %}
Note that in the sample above we used "**ip\_hash**" to bind a specific server to a client in order to achieve the required [sicky session](https://docs.wisej.com/deployment/concepts/load-balancing#sticky-sessions) functionality. NGINX also supports "**sticky**" and other options.
{% endhint %}
