Proxy Everything into a Linux Container with nginx

| linux | devops |

I previously wrote about setting up Node.js + Ghost in an Ubuntu LXC container and using Apache to proxy all web requests into that container. This works pretty well for the most part, but it seems like nginx is much better tool for this since it was pretty much designed to be a proxy server. We have a server that we are using for all Bit-Monkeys projects and I recently set up gitlab, along with a development site for openfaqs inside of LXC containers. The main benefit of this approach is that you can isolate the environments, manage upgrades and updates of various pieces separately, and fix issues in one environment without bringing down your entire infrastructure. Setting this up to work with nginx is super easy. First you will need to grab the IP address of your container which you can easily get by running as the root user

lxc-ls --fancy
Once you have the IP address of the container, you will need to install nginx. We are running Ubuntu 14.04 so it is as simple as apt-get install nginx. The last step is to create a virtual host config file for your container.
vim /etc/nginx/sites-available/yoursite
The contents of this file should look something like this:
server {  

listen 80;
server_name dev.openfaqs.com www.dev.openfaqs.com;
location / {
proxy_pass http://10.0.3.194:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
port_in_redirect off;
proxy_redirect http://10.0.3.194:5000 /;
proxy_connect_timeout 300;
}
}

First you should replace the server_name directive with the name of your site. Next you will want to replace the IP address in the proxy_pass and proxy_redirect arguments to the IP address of your container. We are running Flask which is why it is routing to port 5000, you should replace the port with whatever port your application is running on. After this has been completed you should make a symbolic link to the /sites-enabled directory and restart nginx.
ln -s /etc/nginx/sites-available/yoursite /etc/nginx/sites-enabled/yoursite  

service nginx restart

If all goes well, you will now be able to enter the name of your site in the browser and be served with whatever content or application is running inside of your container. This is a really great use case for container in my opinion, and nginx makes it easier than ever to get started. UPDATE: You can just as easily add a server block for 443 to proxy all HTTPS requests into the container as well. (Thanks tostmiller via reddit for the question.) Sweet, now that you have mastered nginx proxies with LXC, check out the the complete guide to nginx high performance.

Thank you for reading! Share your thoughts with me on bluesky, mastodon, or via email.

Check out some more stuff to read down below.

Most popular posts this month

Recent Favorite Blog Posts

This is a collection of the last 8 posts that I bookmarked.

Articles from blogs I follow around the net

Pluralistic: Battery rationality (06 Dec 2024)

Today's links Battery rationality: Ringing down the curtain on 9/11's security theater. Hey look at this: Delights to delectate. This day in history: 2009, 2014, 2019, 2023 Upcoming appearances: Where to find me. Recent appearances: Where I've…

via Pluralistic: Daily links from Cory Doctorow December 6, 2024

P&B: Erica Fustero

This is the 67th edition of People and Blogs, the series where I ask interesting people to talk about themselves and their blogs. Today we have Erica Fustero and her blog, ericafustero.com To follow this series subscribe to the newsletter. …

via All the interviews for the People and Blogs series. December 6, 2024

P&B: Erica Fustero

This is the 67th edition of People and Blogs, the series where I ask interesting people to talk about themselves and their blogs. Today we have Erica Fustero and her blog, ericafustero.com To follow this series subscribe to the newsletter. …

via Manuel Moreale RSS Feed December 6, 2024

Generated by openring