Serve Static Content in Azure Files With Nginx

| ,

Published on | Updated on February 4, 2021

4 Min. Read

NGINX, Inc., a company that is now part of F5 Networks, shares its name with its leading product, NGINX. NGINX has two versions: an Open Source Software Solutions (OSS), and a commercial solution NGINX Plus. These two versions dominate the world of application delivery controllers, both on-premises and in the cloud.

Both NGINX OSS and NGINX Plus can fit into your web application landscape as a load balancer for TCP and UDP, but they also can fill the need for a more advanced HTTP(S) application delivery controller.

In this article, I will share with you how to serve static content in Azure file share(s) with NGINX running in an Azure Container Instance (ACI), so you can provide secure access to your users over static content in Azure files.

Introduction

NGINX is one of the most popular web servers in the world. Not only is NGINX a fast and reliable static web server, but it is also used by a ton of developers as a reverse-proxy that sits in front of their APIs.

I came recently across an interesting scenario where I want to provide public access to my files that reside in Azure file share, those files will be consumed as static content (read-only). If you are familiar with static website hosting in Azure blob storage, where you can serve static content like (HTML, CSS, JavaScript, and image files) directly from a storage container named $web. However, this capability requires a blob storage container to work and not Azure file share.

What if you want a similar capability but with Azure files?

After looking for an alternative and cost-effective solution, I found out that I can use an Azure container instance running Nginx as a lightweight container to provide secure access to my users over static content, then you can share those files over (HTTP/HTTPS) so your users will be able to access them.

In this article, I will share with you how to deploy NGINX in an Azure Container Instance (ACI) and mount an existing Azure file share, so you can provide secure access to your users over static content.

Prerequisites

To follow this article, you need to have the following:

  1. Azure subscription – If you don’t have an Azure subscription, you can create a free one here.
  2. Azure Resource Group obviously.
  3. Azure storage v2 account – To create a general-purpose v2 storage account, you can follow the instructions described here.
  4. You also need to create at least two Azure file shares in your storage account. One file share will be used to upload the Nginx.conf file so we can configure Nginx at runtime, and the second file share will be used to serve the users with static content.
  5. Lastly, you need to upload some files to your second Azure file share so your users can consume them statically.

Deploy Nginx in a container

As mentioned previously, NGINX is a very powerful web server. You can do a ton of things with it, such as setting up reverse proxies or load balancing. It can also be used to host your static website and content which is the scope of this article.

When running web services in docker containers, it can be useful to run a reverse proxy in front of the containers so you can have more control over who can access your files and serve static content using Nginx. A reverse proxy server is a server that typically sits in front of other web servers in order to provide additional functionality that the web servers may not provide themselves.

The entire process to use Nginx in front of your Azure file share(s) is the following:

  1. Update the following Nginx.conf file with the list of allowed ‘public IP addresses‘ that you want to have your users access the static files via HTTP/HTTPS. In this configuration, I have one port open so we can connect natively via HTTP to the public IP / FQDN using TCP port 80.
    user  nginx;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
    
        include /etc/nginx/conf.d/*.conf;
    
        server {
            # Allow User Public IP addresses
            allow add_public_ip1_here;
            allow add_public_ip2_here;
            allow add_public_ip3_here;
            # Need this private IP range for the ACI monitoring infrastructure
            allow 10.92.0.0/16;
            deny all;
        listen 80;
            root /mnt/azfileshare;
    
               location / {
           autoindex on;        
               }
    
        }
    }
  2. Upload the updated Nginx.conf file to an existing Azure file share, assuming you already have a storage account deployed in Microsoft Azure. In this way, Nginx will be configured at runtime automatically.
  3. Deploy the Azure Container Instance using the ARM template (more on this in a bit).
  4. Finally, access your static content using the public IP address or the container DNS (FQDN) from your favorite browser and enjoy!

Click on the “Deploy to Azure” button and follow the steps as shown in the video below. The template will create an Azure Container Instance (ACI) with one container running Nginx, assuming that you have already uploaded the Nginx.conf file to an existing Azure storage account and file share.
Deploy To Azure

That’s it there you have it!

Summary

In this article, I showed you how to deploy Nginx on Azure Container Instance (ACI) using Azure File Shares in the backend, so you can provide secure access to your users over (HTTP). Running Nginx in a container will be cheaper than running it in an IaaS virtual machine.

For a production environment, I highly recommend enabling HTTPS, you need to have an SSL certificate and then you need to update the Nginx config file (server section) as follows:

server {
    listen  80;
    return  301 https://\$host\$request_uri;
}

server {
    listen              443 ssl;
    ssl_certificate     [PATH_TO_SSL_CERT];
    ssl_certificate_key [PATH_TO_SSL_CERT_KEY];

    location / {
    autoindex on; 
    }
}

Learn more on how to deploy Secure FTP (SFTP) Service on Microsoft Azure with Nginx.

__
Thank you for reading my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-

Related Posts

Previous

Replace DFS Replication With Azure File Sync

Replace Off-Site Backup With Azure Backup

Next

Leave a comment below...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Charbel Nemnom’s Blog

Get the latest posts delivered right to your inbox

The content of this website is copyrighted from being plagiarized! However, you can copy from the 'Code Blocks'.

Please send your feedback to the author using this form for any 'Code' you like.

Thank you for visiting!