Nginx for deployment on AWS EC2

I have a few chat apps running on EC2 instances. The apps are built with Streamlit and have relatively low traffic at the moment.

I have a Web Application Firewall (WAF) that sits in front of an application load balancer (ALB) that sits in front of the EC2 instances that host these chatbots. Here’s the setup again for more clarity:

User => WAF => ALB => EC2 Streamlit App

For analytics I’m contemplating using the streamlit-analytics2 package.

To keep the streamlit apps alive, I use the systemd command.

For caching, I use the built-in st.cache_data decorator.

Given this setup, can you comment on the pros and cons of using Nginx as a webserver for deployment of a Streamlit app on AWS EC2? Do we really need Nginx?

Getting a robust discussion around this topic may be helpful to many other people in a similar situation.

And here’s what ChatGPT says:

Using Nginx as a web server for deploying Streamlit apps on AWS EC2 has both pros and cons. Here’s a brief overview to help you decide if you really need it:

Pros:

  1. Load Balancing: Although you have an ALB, Nginx can provide additional load balancing, helping distribute traffic more efficiently among your instances.
  2. SSL/TLS Termination: Nginx can handle SSL/TLS termination, reducing the load on your Streamlit apps and providing a layer of security.
  3. Reverse Proxy: It can act as a reverse proxy, handling requests and forwarding them to your Streamlit apps, which can help with performance and security.
  4. Caching: Nginx can cache static content, reducing the load on your Streamlit apps and improving response times.
  5. Security: Additional security features like rate limiting and access control can be implemented using Nginx.

Cons:

  1. Complexity: Adding Nginx introduces additional complexity to your setup, requiring configuration and maintenance.
  2. Overhead: There is a slight performance overhead due to the additional layer in your stack, though this is usually minimal.
  3. Redundancy: Since you already have a WAF and ALB, some of the benefits of Nginx might be redundant, depending on your specific use case.

Do You Really Need Nginx?

Given your current setup with WAF and ALB, adding Nginx might not be strictly necessary. However, if you require the specific benefits that Nginx provides (e.g., SSL termination, caching, additional security), it could still be valuable. For low-traffic apps, the added complexity may not be justified, and you might want to keep your setup simpler until traffic increases or specific needs arise.


But it would be great to get feedback from real humans :slight_smile:

@Vikram_N Hi there :slight_smile: I am the maintainer for streamlit-analytics2
I am glad to hear that you are contemplating using the package. I am here if you have any questions or suggestions for the project.

My two cents on this: NGINX is a great tool but what problem in your setup does it solve? With a WAF and an ALB(assumed to be an L7 ALB), your security and routing should be handled quite well.

I have run streamlit apps in containerized deployments before and it actually works incredibly well since the streamlit app can be used in a stateless manner by default. Throw in an API behind streamlit and now you have a use case for NGINX but tbh still overkill. Flask or even nowadays even just Golang is suitable for an API to connect your streamlit frontend to a backend and other data sources.
Maybe that level of system design is overkill but it is definitely scalable.
I can hardly think of a use case that requires NGINX these days with all the other tools that are afforded to us by cloud providers that can handle all the routing, SSL termination and scaling for us. It suits well if youre scaling VMs but since you mentioned there is low traffic right now, I would argue for a containerized version of streamlit as a FE with an API to connect to backends, etc

WAF → ALB → Streamlit stateless FE → Flask/golang API → stateful BE +
| → SQL server +
| → external resources +
| → internal cloud services

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.