Configuring Nginx reverse proxy server (Windows)

Preparations

In this tutorial, we will learn how to set up Nginx on Windows and use it as the reverse proxy server to redirect requests to the PowerServer Web APIs running on the Kestrel server.

The Nginx reverse proxy server can be set up on the same or different server from the PowerServer Web APIs and Kestrel. In this tutorial, the same server will be used.

In this tutorial, we will configure and use the following server environment and URLs. Be careful to use the correct port number and make sure the port is not occupied by any other program.


Step 1: Set up the server with the following OS and software (install the software in the order listed).

  • Windows Server 2019 (64-bit)

  • Nginx 1.19.10

    The section Installing Nginx has detailed installation instructions.

Step 2: Make sure the server can connect to the NuGet site (for installing PowerServer NuGet packages) and the following Appeon sites (through port number 80): https://apips.appeon.com and https://apipsoa.appeon.com (or https://apips.appeon.net and https://apipsoa.appeon.net) (for validating the PowerServer license).

Note

If the server connects to Internet through a proxy server, make sure to configure the proxy server settings in the PowerServer Web API as well (the ServerAPIs project > Server.json file > "ProxyOptions" block). The password for the proxy server (if any) must be an encrypted value (encrypted by the CustomizeDeploy.dll tool).

Step 3: Configure Windows Defender Firewall on the server to allow the port number (80 and 8080 in this tutorial or any port number you choose). The section "Configuring Windows Defender Firewall" has detailed instructions.

Configuring Nginx

This section is to configure Nginx as a reverse proxy server in a Windows machine.

Step 1: Go to the ..\nginx-1.19.10\conf folder and open the nginx.conf file in a text editor.

Step 2: Locate the "server" block and add another "server" block as shown below.

This is to configure Nginx as a reverse proxy server which will redirect requests made to the URL: https://172.16.100.39:8080/ to the PowerServer Web APIs running on Kestrel at https://172.16.100.35:6000/.

    server {
        listen  8080;
        location / {
           proxy_set_header Host $http_host;
           proxy_pass  https://172.16.100.35:6000;
        }
    }


Tip: In Windows, you can execute the command "netstat -ano | findstr 8080" to check if the port number is occupied by any other program.

Step 3: Check if any syntax error in the Nginx configuration file, and then restart Nginx for the changes to take effect.

nginx -t
nginx -s reload

Step 4: Verify that the Nginx processes are running.

tasklist /fi "imagename eq nginx.exe"

Step 5: If you have set up a firewall on the server, configure the firewall to allow port 8080 (by following instructions in "Configuring Windows Defender Firewall").

Note

If the firewall blocks the port number, you will have the following error when running the application.


Modifying and re-deploying the PowerServer project

The following modifications are made to the PowerServer project created in the Quick Start guide. If you have not created a PowerServer project yet, please follow the instructions in the Quick Start guide to create one.

Step 1: Modify the Web API URL to point to the Nginx reverse proxy server.

On the Web APIs tab of the PowerServer project painter, specify the URL of the Nginx reverse proxy server, for example, https://172.16.100.39:8080 in this tutorial. It is highly recommended that you specify an HTTPS URL for the production environment.

All requests for the PowerServer Web APIs will be first made to https://172.16.100.39:8080 and then redirected by the Nginx reverse proxy server to the PowerServer Web APIs running on Kestrel server (for example, https://172.16.100.35:6000).


Step 2: Select a Web server for deploying the app files.

On the Client Deployment tab of the PowerServer project painter, select a local or remote Web server (IIS, Apache, Nginx, etc.) you have configured properly.

The Web server and the Nginx reverse proxy server can reside in the same or different machine. If the Web server is an Nginx HTTP server, it can be the same or different server instance with the Nginx reverse proxy server.

In this tutorial, we use the same Nginx server instance as the Nginx HTTP server and the reverse proxy server.

  • If you choose the "Directly deploy to the server" option, make sure you have configured the FTP settings properly for the server. See Setting up Nginx on Windows > Installing FTP server for detailed instructions.

  • If you choose the "Package the compiled app and manually deploy later" option, follow the instructions in Packaging and copying the client app.


Step 3: Save the PowerServer project settings and then build and deploy the PowerServer project for the changes to take effect.

Verifying the configuration

Now let's run the PowerServer Web APIs and verify that the requests are forwarded successfully from the reverse proxy server to the PowerServer Web APIs.

In order to view the logs easily, let's directly start the PowerServer Web APIs as a console application on its internal Kestrel web server, using either of the following methods:

  • Execute the "dotnet run --project PowerServer19\ServerAPIs\ServerAPIs.csproj" command, or

  • Open the PowerServer C# solution in the SnapDevelop IDE and then click the Run button.

Make sure the PowerServer Web APIs is running on the correct IP address and port number. For example, https://172.16.100.35:6000/ in this tutorial. You may modify the port number in the launchSettings.json file of the ServerAPIs project of the PowerServer C# solution when running in the development environment.


Run the application (https://172.16.100.39:80/pssales in this tutorial). You should be able to see from the console that the requests are going through successfully and the requests are originally made to the Nginx proxy server (https://172.16.100.39/8080 in this tutorial).


Once you have verified that the reverse proxy server works properly, you can run the PowerServer Web APIs as a service, as described in Running Web APIs on Kestrel.