Path and hostname-based routing in Azure Container Apps with NGINX (2024)

Azure Container Apps is a fully managed serverless container service that enables you to deploy and run containerized applications without having to manage the infrastructure.

By default, HTTP apps in Azure Container Apps are accessible via a public URL that is unique to the app. However, you can create a container app to use a reverse proxy like NGINX to control how traffic is routed to multiple apps based on the path or hostname.

In this tutorial, you'll learn how to use Azure Container Apps to configure path and hostname-based routing for a set of containerized applications using NGINX as a reverse proxy. You'll deploy 4 applications: 1 NGINX container which will be publicly exposed and 3 container apps which will only be accessible from within the environment and that traffic will be routed to from the NGINX container.

Architecture Diagram

Path and hostname-based routing in Azure Container Apps with NGINX (1)

Prerequisites

  • An Azure account with an active subscription.
    • If you don't have one, youcan create one for free.
  • Install theAzure CLI.
  • Install the Azure Container Apps CLI.

Deploy Azure Container Apps resources

  1. Configure environment variables for the various resources you'll deploy:

    RESOURCE_GROUP_NAME=path-based-routing-rgLOCATION=northeuropeSTORAGE_ACCOUNT_NAME=pathbasedrouting$RANDOMENVIRONMENT_NAME=path-based-routing

    Note:$RANDOMis a bash variable that returns a random number and is used here to generate a storage account that is globally unique within Azure. If it's not available in your shell, use another unique value for theSTORAGE_ACCOUNT_NAMEvariable.

  2. Create a resource group:

    az group create --name $RESOURCE_GROUP_NAME --location $LOCATION
  3. Create an Azure Container Apps environment:

  4. Create two container apps in the environment:

    az containerapp create --name app1 --environment $ENVIRONMENT_NAME \ --resource-group $RESOURCE_GROUP_NAME \ --ingress internal --target-port 80az containerapp create --name app2 --environment $ENVIRONMENT_NAME \ --resource-group $RESOURCE_GROUP_NAME \ --ingress internal --target-port 80

    This will create two container apps,app1andapp2. Both apps are configured to not be publicly accessible and are only accessible within the environment. The only exposed public endpoint is from the NGINX app.

  5. Create a container app running NGINX:

    az containerapp create --name nginx --environment $ENVIRONMENT_NAME \ --resource-group $RESOURCE_GROUP_NAME \ --ingress external --target-port 80 --image nginx

    This will create a container app running NGINX. The app is publicly accessible and is accessible from the internet. The NGINX app will be used as a reverse proxy to route traffic to the other two apps.


    The command should print the public URL of the NGINX app. Navigate to it to verify that the app is running.

Now that the Container Apps resources are created, you can proceed to configure the path-based routing.

Configure path-based routing

To configure path-based routing, you'll create an NGINX configuration file that defines the routing rules and upload it to an Azure File Share. Then you'll mount the file share to the NGINX container app.

  1. Create a storage account to store the NGINX configuration file:

    az storage account create --name $STORAGE_ACCOUNT_NAME \ --resource-group $RESOURCE_GROUP_NAME --location $LOCATION \ --sku Standard_LRS
  2. Create a file share in the storage account:

    az storage share create --name nginx-config --account-name $STORAGE_ACCOUNT_NAME
  3. In the current directory, create a new file callednginx.confwith the following content:

    events {}http { server { listen 80; location /app1/ { proxy_http_version 1.1; proxy_pass http://app1/; } location /app2/ { proxy_http_version 1.1; proxy_pass http://app2/; } }}

    This NGINX configuration file defines two locations,/app1/and/app2/, and routes traffic to theapp1andapp2container apps respectively using their internal URLs,http://app1/andhttp://app2/.

  4. Upload the NGINX configuration file to the file share:

    az storage file upload --account-name $STORAGE_ACCOUNT_NAME --share-name nginx-config \ --source nginx.conf --path nginx.conf
  5. Get the access key for the storage account:

    STORAGE_ACCOUNT_KEY=$(az storage account keys list --account-name $STORAGE_ACCOUNT_NAME \ --resource-group $RESOURCE_GROUP_NAME --query "[0].value" --output tsv | tr -d '\r')
  6. Configure the file share in the Container Apps environment:

    az containerapp env storage set \ --name $ENVIRONMENT_NAME --resource-group $RESOURCE_GROUP_NAME \ --storage-name nginx-config \ --account-name $STORAGE_ACCOUNT_NAME \ --azure-file-account-key $STORAGE_ACCOUNT_KEY --azure-file-share-name nginx-config \ --access-mode ReadOnly
  7. Export the YAML from the NGINX container app:

    az containerapp show --name nginx --resource-group $RESOURCE_GROUP_NAME \ --output yaml > nginx.yaml
  8. Opennginx.yamlin a text editor. Add thevolumesarray to thetemplatesection to mount the Azure File Share to the NGINX container app. Then add thevolumeMountsarray to thecontainersarray to mount the volume to the NGINX container. The modified YAML should look like this snippet:

    // ...properties: // ... template: containers: - image: nginx name: nginx resources: cpu: 0.5 memory: 1Gi volumeMounts: - mountPath: /etc/nginx/nginx.conf subPath: nginx.conf volumeName: nginx-config scale: maxReplicas: 10 minReplicas: 0 serviceBinds: null terminationGracePeriodSeconds: null volumes: - name: nginx-config storageType: AzureFile storageName: nginx-config// ...
  9. Update the NGINX container app with the modified YAML:

    az containerapp update --name nginx --resource-group $RESOURCE_GROUP_NAME \ --yaml nginx.yaml
    This will update the NGINX container app to use the NGINX configuration file from the Azure File Share.
  10. Verify that the path-based routing is working. Navigate to the public application URL of the NGINX app.

    If you need to get the NGINX container's URL, run the following command. Your NGINX app's URL is https://[your nginx's fqdn]/.

    az containerapp ingress show --name nginx --resource-group $RESOURCE_GROUP_NAME

    Append/app1/and/app2/to the URL to verify that the traffic is being routed to theapp1andapp2container apps respectively (Ex: [your NGINX Application URL]/app1/).

    SelectShow more info about this appto see which container app is being used.
    Path and hostname-based routing in Azure Container Apps with NGINX (2)

Now that you've deployed your NGINX container and are routing based on paths to your container apps, you'll learn how to update the routing configuration in your NGINX container.

Update the NGINX configuration

In order to change how the NGINX container handles routing, you'll need to follow steps 3-4 inConfigure path-based routingto modify the nginx.confand reupload it to the file share.

You'llneed to restart the NGINX container app to apply the updated routing changes.

az containerapp revision restart --name nginx --resource-group $RESOURCE_GROUP_NAME \--revision $(az containerapp revision list -n nginx -g $RESOURCE_GROUP_NAME --query '[0].name' -o tsv | tr -d '\r')

In the next section, you'll learn how to use the same NGINX container to configure hostname-based routing for your containers, and we'll walk you through an example of updating yournginx.conf.

Configure hostname-based routing

In addition to routing traffic based on paths, you can also configure NGINX to route traffic based on the hostname. To do this, use multiple server blocks in the NGINX configuration file, each with a differentserver_namedirective. This example builds off the previousConfigure path-based routingsection.

  1. For the hostname-based routing, you'll create a third application which can be done using the following command:
    az containerapp create --name app3 --environment $ENVIRONMENT_NAME \ --resource-group $RESOURCE_GROUP_NAME \ --ingress internal --target-port 80​
    To configure your NGINX container for hostname-based routing, you'll need to update thenginx.confand upload it to your file share like you did in steps3-4 from the Configure path-based routingsectionby updating thenginx.confand uploading it to your file share. The steps are shown below.
  2. Modify thenginx.confto add additional domains to the nginx app. In the below example, we've added additional domains to the NGINX app which inform how traffic is routed to apps 1, 2, and 3. Traffic tonginx.proudgrass-abcdefgh.northeurope.azurecontainerapps.iois routed toapp1andapp2, while traffic topath-based-routing.anthonychu.devis routed toapp3.

    Note theserver_names_hash_bucket_size 128;directive. This is sometimes required when using a large number of server names, or in this case, when using a long domain name like the default one provided by Azure Container Apps.

    events {}http { server_names_hash_bucket_size 128; server { listen 80; server_name nginx.proudgrass-abcdefgh.northeurope.azurecontainerapps.io; location /app1/ { proxy_http_version 1.1; proxy_pass http://app1/; } location /app2/ { proxy_http_version 1.1; proxy_pass http://app2/; } } server { listen 80; server_name path-based-routing.anthonychu.dev; location /app3/ { proxy_http_version 1.1; proxy_pass http://app3/; } }}
  3. Then, run the following command to upload yournginx.confchanges to the file share.
    az storage file upload --account-name $STORAGE_ACCOUNT_NAME --share-name nginx-config \ --source nginx.conf --path nginx.conf​
  4. You'llneed to restart the NGINX container app to apply the updated routing changes.
    az containerapp revision restart --name nginx --resource-group $RESOURCE_GROUP_NAME \--revision $(az containerapp revision list -n nginx -g $RESOURCE_GROUP_NAME --query '[0].name' -o tsv | tr -d '\r')​
  5. Verify that the path-based routing is working. Navigate to the URL configured with the server_namefor app3 and provide the path /app3/.

    Select

    Show more info about this appto see which container app is being used with the hostname path-based-routing.anthonychu.devinstead of the default hostname for the NGINX app.
    Path and hostname-based routing in Azure Container Apps with NGINX (3)

Congratulations!

You have now successfully setup both path and hostname-based routing with an NGINX container for your container apps! Please comment below to let us know what you think of the experience.

Path and hostname-based routing in Azure Container Apps with NGINX (2024)

FAQs

What does Azure use for containers? ›

Which Container Service Option Should You Use?
Service NameDescription
Azure Kubernetes ServiceKubernetes clusters where Azure manages the control plan and you only pay for the nodes.
Azure Container InstancesRun containers in the cloud without having to manage any server.
4 more rows

Where to run containers in Azure? ›

On the Azure portal homepage, select Create a resource. Select Containers > Container Instances. On the Basics page, choose a subscription and enter the following values for Resource group, Container name, Image source, and Container image.

Which Azure service allows users to deploy and manage containers? ›

Azure Kubernetes Service is a robust and cost-effective container orchestration service that helps you to deploy and manage containerized applications in seconds where additional resources are assigned automatically without the headache of managing additional servers.

Which of these is the fastest way to run a container in Azure? ›

Azure Container Instances offers the fastest and simplest way to run a container in Azure, without having to manage any virtual machines and without having to adopt a higher-level service.

What is the difference between Docker container and Azure container? ›

In summary, Azure Container Instances provides a serverless and managed way to run containers without worrying about infrastructure management or orchestration, while Docker is a more flexible and standalone platform that requires additional tools and setup for container orchestration and infrastructure management.

What are the requirements for container names in Azure? ›

Container names can be between 3 and 63 characters long. Container names must start with a letter or number, and can contain only lowercase letters, numbers, and the dash (-) character. Two or more consecutive dash characters aren't permitted in container names.

Can you run Active Directory in a container? ›

Although Windows containers cannot be domain joined, they can still use Active Directory domain identities to support various authentication scenarios.

How to connect to an Azure container? ›

Navigate to the Azure portal. Select All resources on the left of the Azure portal. Enter Container Apps in the filter and select the name of the container app you want to use in the list. Select Service Connector from the left table of contents.

How do I run a container on a specific network? ›

Examples
  1. Connect a container to a network when it starts. ...
  2. Specify the IP address a container will use on a given network (--ip) ...
  3. Use the legacy --link option (--link) ...
  4. Create a network alias for a container (--alias) ...
  5. Set sysctls for a container's interface (--driver-opt)

How many containers can run in an Azure container instance? ›

Unchangeable (Hard) Limits
ResourceActual Limit
Number of containers per container group60
Number of volumes per container group20
Ports per IP5
Container instance log size - running instance4 MB
1 more row
Aug 29, 2024

How do I deploy multiple containers in Azure? ›

To deploy a multi-container group with the az container create command in the Azure CLI, you must specify the container group configuration in a YAML file. Then pass the YAML file as a parameter to the command.

Is Azure container a PaaS or IaaS? ›

Azure Container Service. Azure Container service is more of a Iaas offering compared to Azure Service Fabric which is offered as a Paas.

What is the difference between Azure container instance and Azure container registry? ›

Whereas az container create actually creates a running instance of your container or container group. So the container service will persist for as long as you expect it to run. The container registry is more a repository for your container images rather than a place where they are run and provided as a service.

What is the difference between Azure serverless and containers? ›

Serverless functions are typically stateless and offer limited control over the execution environment. Containers: Suitable for various applications, including long-running processes, stateful applications, and microservices architectures. They offer greater control and flexibility over the execution environment.

What is the maximum length of container name in Azure? ›

Container Names

All letters in a container name must be lowercase. Container names must be from 3 through 63 characters long.

Does Azure container apps use Kubernetes? ›

Azure Container Apps

Powered by Kubernetes and open-source technologies like Dapr, KEDA, and envoy. Supports Kubernetes-style apps and microservices with features like service discovery and traffic splitting.

Does Azure functions use containers? ›

Functions also supports containerized function app deployments. In a containerized deployment, you create your own function app instance in a local Docker container from a supported based image. You can then deploy this containerized function app to a hosting environment in Azure.

Does Azure have a container registry? ›

Azure Container Registry handles private Docker container images as well as related content formats, such as Helm charts, OCI artifacts, and images built to the OCI image format specification.

What container runtime does Azure use? ›

Containers in Azure Container Apps can use any runtime, programming language, or development stack of your choice. Azure Container Apps supports: Any Linux-based x86-64 ( linux/amd64 ) container image. Containers from any public or private container registry.

Top Articles
Automatically fill in one-time verification codes on iPhone
Lot Size Calculator - Best Tool w
Evil Dead Movies In Order & Timeline
Sdn Md 2023-2024
Is Paige Vanzant Related To Ronnie Van Zant
El Paso Pet Craigslist
Western Union Mexico Rate
Here are all the MTV VMA winners, even the awards they announced during the ads
Grange Display Calculator
Top Financial Advisors in the U.S.
Mylaheychart Login
Gameplay Clarkston
Campaign Homecoming Queen Posters
Slushy Beer Strain
General Info for Parents
Cnnfn.com Markets
Fredericksburg Free Lance Star Obituaries
Summer Rae Boyfriend Love Island – Just Speak News
Chile Crunch Original
SXSW Film & TV Alumni Releases – July & August 2024
V-Pay: Sicherheit, Kosten und Alternativen - BankingGeek
Craigslist Pinellas County Rentals
Ge-Tracker Bond
Barber Gym Quantico Hours
[PDF] PDF - Education Update - Free Download PDF
BJ 이름 찾는다 꼭 도와줘라 | 짤방 | 일베저장소
Jayme's Upscale Resale Abilene Photos
Dashboard Unt
Times Narcos Lied To You About What Really Happened - Grunge
Greyson Alexander Thorn
Motor Mounts
County Cricket Championship, day one - scores, radio commentary & live text
134 Paige St. Owego Ny
Wisconsin Volleyball Team Leaked Uncovered
Spy School Secrets - Canada's History
What Happened To Father Anthony Mary Ewtn
Ark Unlock All Skins Command
Car Crash On 5 Freeway Today
Cherry Spa Madison
Appraisalport Com Dashboard Orders
Best Restaurants Minocqua
Mitchell Kronish Obituary
Bekkenpijn: oorzaken en symptomen van pijn in het bekken
Eat Like A King Who's On A Budget Copypasta
Random Animal Hybrid Generator Wheel
Oakley Rae (Social Media Star) – Bio, Net Worth, Career, Age, Height, And More
Maplestar Kemono
Ratchet And Clank Tools Of Destruction Rpcs3 Freeze
Aznchikz
10 Bedroom Airbnb Kissimmee Fl
Factorio Green Circuit Setup
Coors Field Seats In The Shade
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 5768

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.