You are on page 1of 6
How To Setup an Nginx Reverse Proxy using Amazon Lightsail Containers In this guide, youl lear how to configure» Flask web server behind an Nine reverse prony using Lightsall containers. The Noinx ‘reverse proxy accepts web requests on por 80 and forward them to the Flask web serve on port 5000, The Flask web server fli the requests andl retuen the response to Ngin« A ightsail container service wil be created ta host both the Nginx and the Fask containers A public endpoint wil be crested to allow external access tothe Ni sever. Yo get start, youl need an AWS account and must install Docker, Docker compas, the AWS Command Line Interface (CL tool and ‘the Lightsail Cantal gist. plugin on your system, Follow the provided inks ifyou don't have some of those 1. Get the source code ‘The sourcecode to accompany this uide can be found in this eepesitory, To get started, clone the Github respository locally. [iit das RR eee oT Change to the project directory ec diprasait-conainere-raina 2. The Flask application ‘The Flask apalication contains a single helle_sortd() function that is triggered when the route / is requested. When run this aplication binds to all IPs on the system (0.0007 and listens on port $000 (his is the default Flask port). The source forthe Flask application, app.2y, is shown below. pp = Flask(_nane_) hano-rouse("/") ef hello nord() return “ello, Worla!® Te Dockerfile forthe Flask application uses a Python alpine image to minimize container image size. The commang te run when the container starts is the same asf un from the command line: aythen 3p0.0y Bet base Inage (host 05) Rom pytons3-8-alpine by aotauit, 1ssten on port S008 POSE 5000/ ep Set the working directory in the container REOIR /3pp Copy the dependencies 16 to the working directory Install any dependencies Fun pip install =r renvirenents.tat copy te contert of the Local sre directory to the working directory Specify the command 9 run on containes start fo Cpythen", *./a90.0y") 3. Build the Flask container Complete the following steps to build the Flask appliaiton container on your local system, 16 uid the container using Docker. Execute the following cammanc fram the project crectony [eer tad 3 iniccortainer fine ‘This command builds 9 container using the Dackefle in the curent directory and tags the container élask-container. ‘once the container builds done, test the Flask application locally by running the container: (iacker ron 9 severseee ase ‘The Flask apo wil un in the container and will be exposed to your local system on port $000, Browse to htpy/localhostS000 or use cunt from the command ine and you will See welie, Korte! To stop the container type cTaLAC 4, The Nginx reverse proxy The Nginx reverse proxy forwards all requests tothe Flask application on port S000, Configuring Nginxto forward reqestsreugiras 2 simple configuration fle, nginx. cont vente server $(FLASK HOST} :$(FLASK PORT): server { This configuration forwards ll requests to the upstream Flask server, The hastname and part ofthe Flask server ate provided as environmental variables when the containers ae run. ‘The bockerfile for the Nginsraverseprony uses the master Nginx alpine mage ane simply cops the ngiox.con® configuration fle to the appropriate directory. FOR ng 18-apine 5. Build the Nginx container Complete the following steps to build the Ngine reverse prony container on your loca systems, Bull the container using Docker, Execute the following commane! rom the project rectory: Eiectar boii painccortainen Traine This command builds a container using the Dackerfle in the curentcirectory an tags the container ngina-cartainer. After the container build is Gone, test the None proxy and Flask application locally by unaing the container: fen 26 Both the Flask application and Nginx reverse proxy containers willbe run. The Nejnx server listens for requests on port 80 and forwards ‘them tothe Flask application. Browse to htp://localhost or use cur from the command ine and you willsee ella, Hori. To stop the conaners, type cree 6. Create a container service Complete the following steps to create the Lightsll container service ane then push your local container images to the new container reate a Lightsal container service with the ceate-cortainer-seuvice command, sewer saa \ ‘The power and seale parameters specify the capacty ofthe container service. Fr the purposes of ths guide, litle capacity is required, ‘The output ofthe create-containersenvice command indiates the state ofthe new service is PENDING, containerservice": { *cantainerserviceNone": “sanple-service* Use the getscontainar-cevices command te monitor the state ofthe container as itis being created [Eines Tightsail get-container-services —-servica-nane suele-senice 1 Wit unl the container service tate changes to ACTIVE before continuing tothe next step. Your container sevice should become active ater 9 few minutes, Push the Flask application container to Lights withthe push-contanet-image command. Ges Lightsail push-covtainesinage ~-service-tore Semple-senvice ¥ Label Alask-cortainer \ Note: the xin sanple-service, +2esk-container.X willbe a numeric valu. Hf his isthe ist time you've pushed an image te your container service, this numer will be 1. You will need this number inthe nest step. Push the Ngins reverse prony container to Lights ith the qush-con'sina-image command Label ngine-cortainer \ this snage a5 “:sanple-service.nginx-contatner.¥” in deploynents Note: the vin ssanple-service.ngtnx-consainer.¥ willbe 8 numeric valu. If his is the fist time you've pushed an image to your container service, this number will be 1, You wll need this number inthe next step. 7. Deploy the containers Complete the fllowing steps to create deployment and public endpoint configuration JSON ies, and then depley your container Images to your container service. a6 Create anew file, containers. sen. Et the fil and ade the following. Replace the Xin :sanple-service.flast-container.x and the Yin sanple-se-vice.relnx-contatner. with the numeric values frm the previous step. Save the fle, age" “:sanple-service.ngine-cortainer.Y", cormand": (1, environment: { sAGiNK_EWYSAST_OLTPUT_DIR": “/etc/nginx” sruask post": “Tecathost™ *songle-flask image": "isanple-cervice.lask-container.x°, > The containers Jeon file desenbes the settings ofthe containers that willbe launched onthe container sevice. In ths instance, the containers json fle descrites bath the Nin and Flask containers, the images the cantiners will use and the ports the container will ‘expose In adcition, environmental variables that specify the Fask host and port are provided. At runtime Nain wll replace the placeholder values inthe ngin.con file with the actual values provided her, Create ane fle, pubtic-endpoins. json. Eli the file and add the following, Save the file The puslic-endpoint. json fle describes the sectings of the public endpoint forthe container service In this instance the publi cendpoint}son fle indicates the Nginx containe” will expose port 0. Public endpoint settings are only requied for senvces that require public access. Deploy the containers to the container service with the AWS Cll using the create-container-senvice-deplayment command oy ndpotne son ewes ‘The cutput of the cveate-container-service-deployrent command indicates that the state ofthe container service is now BEPLOV:NG seontainerservices”: (( contatnerSenvicehone”: “sample-service”, Use the get-container-senices command te mentor the state ofthe container untlit changes to RUNNING before continuing tothe next step, [Floss Sightvail get. contatrer-service The get-container-service command also returns the endpoint URL for container service. “eontatnerservices™: (( Sura": “heeps://sample- service... 406 [ter the container service state changes to RLAWING, navigate to ths URL in your browser to verify your container service running propetly Your browser output should show "Hello, Worl” as betor. Congratulations. You have successfully deployed a containerized Noli reverse proxy and Flask application using Amazon Lights containers. Cleanup Complete the folowing steps tothe igh container sic tht you eented 3s pat ofthis tori To cleanup and delete Lightsll resources, use the delete. cantainer-semvce command. The celete-container-service removes the container service, any associated container deployments, and container images. Additional Resources The source code for this guide and tis documentation is located inthis Gibb repository You Might Also Enjoy: How to Serve a Flask App with Amazon Lightsail Containers Read more -+ How to Use Amazon EFS with Amazon Lightsail Read more ‘Amazon ight uy AP temny AS Moder-too-Saien AWS=SUMIMIt sn Analytic sna nm nee ATChITECEUE thie a Ao non ne 3G-Data Blokhina ns gi roe COMPURE cng ts COMLAINETS names ssn tne nie Database nu swe ses yw Dr i Bya EC2 CS EF AS an heh rn as Hons ISETUCUTE» temas Keynote rmommn KubeCon Kubernetes univni Lambda wnsuon tights as ws vw Machine-Learning say apts wes rion Mon myn wn Ne NERWOFKING oe NO no. tng pte etry roo msn Public Sectr nn AP CADE cn te 25 tomes Mee de Mey SP ne a Sager SECUTIAY Servers sw sane mks Sol Sonne pa ae en tne a iy ey TSCA ns Te Timesteam wis won YPC WE mnt abecteneot Wena Vogels Windows wu wa reinvent cept heehee ale conten on aes ented ude ass Conan Aen See 40 petona ees oe

You might also like