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. Make sure the port number is not occupied by any other program. For how to verify if a port number is occupied, follow the instructions in Choosing an appropriate port number.


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: https://www.nuget.org (for installing PowerServer NuGet packages) and the Appeon license server (for validating the PowerServer license).

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. For details, refer to Choosing an appropriate port number.

Step 3: Check if any syntax errors 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 the 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 the Web API URL

You must modify the Web API URL to point to the Nginx reverse proxy server.

Step 1: Go to the [appname] folder (for example, "salesdemo_cloud") on the web server and open the apprun.json file in a text editor.

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.

Step 2: Set the value of Web API URL to 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 PowerServer Web APIs will be first made to the reverse proxy server (in this example, https://172.16.100.39:8080) and then redirected by the reverse proxy server to the PowerServer Web APIs running on Kestrel server (in this example, https://172.16.100.35:6000).

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 detailed log information, let's directly start the PowerServer Web APIs as a console application in the local development environment, 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/salesdemo_cloud 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 publish and start the PowerServer Web APIs in a production environment, as described in Running Web APIs with Kestrel.