Setting up Nginx as a Reverse Proxy on Windows

Setting up Nginx as a Reverse Proxy on Windows


Table of Contents


Setting up Nginx as a Reverse Proxy on Windows

Hi there, in this article, I want to show you how to set up Nginx as a reverse proxy on Windows. Nginx is a fast and reliable web server that can also act as a reverse proxy. It can be used to proxy requests to a backend server and provide load balancing, caching, and other features.

Prerequisites

Before starting, you will need to have the following:

  • A Windows machine to install and run Nginx.
  • Administrative access to the machine.
  • The latest stable version of Nginx, which you can download from the official website.

Download and install Nginx

To get started, you need to download and install Nginx on your Windows machine. You can download the latest version of Nginx from the official website, https://nginx.org/. Once the download is complete, run the installer and follow the installation wizard.

Configure Nginx

After installation, navigate to the installation directory of Nginx, which is usually located at C:\nginx. Open the nginx.conf file in a text editor.

Set up a backend server

Next, we need to set up a backend server to which Nginx will forward requests. In this example, let’s assume that our backend server is running on http://localhost:8080.

Set up a reverse proxy

To set up a reverse proxy, we need to create a new location block in the nginx.conf file that will forward requests to the backend server. Add the following code to the file:

location / {
    proxy_pass http://localhost:8080;
}

After you’ve set up the reverse proxy, It’s time to test it. Start the Nginx server and navigate to http://localhost in your web browser. If everything is set up correctly, you should see the same content that is being served by your backend server.

Set up HTTPS

To secure your connection, you can set up HTTPS using an SSL certificate. To do this, you’ll need to obtain an SSL certificate from a trusted certificate authority. Once you have the certificate, open the nginx.conf file and add the following code:

server {
    listen       443 ssl;
    server_name  localhost;

    ssl_certificate      path/to/your/certificate;
    ssl_certificate_key  path/to/your/private_key;

    location / {
        proxy_pass https://localhost:8080;
    }
}

Replace path/to/your/certificate and path/to/your/private_key with the actual paths to our certificate and private key files. Restart the Nginx server to apply the changes.

Set up gzip compression

To improve the performance of your website, you can enable gzip compression in Nginx. This will compress the response data before sending it to the client, resulting in faster page load times. To enable gzip compression, add the following code to the nginx.conf file:

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1000;
gzip_proxied any;

This code enables gzip compression and specifies the file types that should be compressed. It also sets the minimum file size that should be compressed and specifies that gzip compression should be used for all types of proxied requests.

Set up caching

Finally, we can improve the performance of our website even further by setting up caching in Nginx. Caching stores the response data from the backend server so that it can be served faster to clients who make the same request. To enable caching, add the following code to the nginx.conf file:

proxy_cache_path  C:/nginx/proxy_cache keys_zone=my_cache:10m;

proxy_cache_key "$scheme$request_method$host$request_uri";

proxy_cache_valid  200 302  60m;
proxy_cache_valid  404      1m;

These directives do the following:

  • proxy_cache_path: This sets the path to the directory where Nginx will store its cache. keys_zone=my_cache:10m sets up a shared memory zone for storing the cache keys, which can be accessed by all worker processes. The 10m value sets the size of the shared memory zone to 10 megabytes.
  • proxy_cache_key: This sets the cache key. In this case, we’re using a combination of the request method ($request_method), the scheme ($scheme), the host ($host), and the request URI ($request_uri) to generate the cache key.
  • proxy_cache_valid: This sets the cache expiration time for different response codes. In this example, we’re caching successful responses (200 and 302) for 60 minutes, and 404 responses for 1 minute.

Save the file and restart Nginx.

nginx -s reload

Your Nginx server is now caching responses to requests made to the backend server. This can help speed up your website by reducing the number of requests made to the backend server.

After configuring everything your nginx conf file should be looking like this:

Without SSL

http {
    include       mime.types;
    default_type  application/octet-stream;

    # Logging settings
    access_log  logs/access.log;
    error_log   logs/error.log;

    # Enable Gzip compression
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    # Server block
    server {
        listen       80;
        server_name  example.com; # Replace with your domain

        # Location block to handle all requests
        location / {
            # Reverse proxy configuration
            proxy_pass http://localhost:8080; # Replace with your backend server address
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        # Additional location blocks can be defined as needed
        # Example: location /images/ { ... }
    }
}

With SSL

http {
    include       mime.types;
    default_type  application/octet-stream;

    # Logging settings
    access_log  logs/access.log;
    error_log   logs/error.log;

    # Enable Gzip compression
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    # SSL settings
    ssl_certificate      /path/to/your/certificate.crt; # Update with your SSL certificate path
    ssl_certificate_key  /path/to/your/private.key;    # Update with your SSL key path
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;        # Adjust protocols as needed
    ssl_ciphers          HIGH:!aNULL:!MD5;

    # Server block for HTTPS
    server {
        listen       443 ssl;
        server_name  example.com; # Replace with your domain

        # SSL configuration
        ssl on;

        # Location block to handle all requests
        location / {
            # Reverse proxy configuration
            proxy_pass http://localhost:8080; # Replace with your backend server address
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }

    # Server block for HTTP to redirect to HTTPS
    server {
        listen      80;
        server_name example.com; # Replace with your domain
        return      301 https://$host$request_uri;
    }
}

Test the setup

To test the setup, open a web browser and enter the IP address of your Nginx server in the address bar. You should see the default Nginx page.

Next, enter the IP address of backend server followed by /app/index.html. You should see the web application.

Congratulations! You have successfully set up Nginx as a reverse proxy on Windows, and configured it to handle HTTPS, compression, and caching. We can now use Nginx to serve multiple web applications and scale our infrastructure as our needs grow.

Conclusion

In this tutorial, we went through the steps to set up Nginx as a reverse proxy on Windows. Nginx is a powerful tool that can help you improve the performance, security, and scalability of your web applications. With the help of this tutorial, you should be able to set up a reverse proxy for your web application and serve it through Nginx.

© 2024 Virendra Giri