Build a proxy server with nginx on multiple remote servers using Ansible

Purpose

As the title suggests, use Ansible to build a proxy server with nginx on multiple remote servers. This time I'm using an AWS EC2 instance as the remote server.

Thing you want to do

--Build a server on multiple EC2 instances with one command. --When adding an instance, just add the target IP address.

things to do

What to do when executing the ansible command.

  1. Install nginx.
  2. Describe the nginx configuration file.
  3. Start nginx.
  4. Execute 1. to 3. with one command for multiple instances.

Installation

Installed in the local environment to run. Now you can use the command ʻansible-playbook`.

$ pip install ansible

Write a config file

Directory structure

.
|-- webapp.yml
|-- hosts
|-- nginx.conf
`-- myapp.j2

nginx config file

myapp.j2


server {
    listen       80;
    server_name  0.0.0.0;

    location / {
        proxy_pass {{ proxy_pass }};
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        
        proxy_cache zone1;
        proxy_cache_key $proxy_host$uri$args;
        proxy_cache_valid 600s;
    }
}

Prepare nginx.conf in the same directory.

ansible config file

webapp.yml



- hosts: web-servers
  user: ec2-user
  sudo: True

  vars:
    proxy_pass: '<Origin URL>'

  tasks: 
  #Install nginx.
  - name: Install nginx
    yum: name=nginx state=latest
  #Create a cache directory.
  - name: Create Cache
    action: file dest=/var/cache/nginx/cache state=directory owner=nginx group=nginx
  #Local nginx.Copy conf remotely.
  - name: Copy the default nginx config file
    action: copy src=./nginx.conf dest=/etc/nginx/nginx.conf
  #Local myapp.j2 to myapp.Compile to conf and copy remotely.
  - name: Write the original nginx config file
    action: template src=./myapp.j2 dest=/etc/nginx/conf.d/myapp.conf
    #Start nginx.
    notify:
    - Start nginx
  handlers:
    - name: Start nginx
      action: service name=nginx state=restarted

Describe the target host

[web-servers]
54.64.xxx.xxx
54.64.yyy.yyy
54.64.zzz.zzz

Run

$ ansible-playbook ./webapp.yml --private-key ~/.ssh/mykey.pem -i ./hosts

Try using Ansible

If the configuration is as simple as starting a web server, Ansible can be written simply, and when adding the target remote server, it is only added to the hosts file, so I was able to do exactly what I wanted to do :) It's a Python library, but it's also attractive that you only write yaml and ini file-like hosts files. I recommend it because I was able to do it in about 3 hours while watching the tutorial etc .: D

Recommended Posts

Build a proxy server with nginx on multiple remote servers using Ansible
Build a python environment with ansible on centos6
SSL-enable multiple sites on one server with nginx and Let's Encrypt
Build a Pypi cache server on QNAP
Easily build a DNS server using Twisted
Build a simple WebDAV server on Linux
Build a Samba server on Arch Linux
Build jupyter notebook on remote server (CentOS)
Build a web server on your Chromebook
Run Jupyter notebook on a remote server
Build a local server with a single command [Mac]
Enable Jupyter Notebook with conda on remote server
Mount a directory on another server with sshfs
Build a server on Linux and local network with Raspberry Pi NextCloud and desktop sharing
Build a Django development environment using pyenv-virtualenv on Mac
Set up a file server on Ubuntu 20.04 using Samba
[Part 2] Let's build a web server on EC2 Linux
Build a Python environment on your Mac using pyenv
Build a Python development environment using pyenv on MacOS
Proxy server with Docker
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
SSH connection to a private server using a bastion server on EC2
Build a Go development environment with VS Code's Remote Containers
Build a web API server at explosive speed using hug
Build a comfortable development environment with VSCode x Remote Development x Pipenv
Build a speed of light web API server with Falcon
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Build a python environment on CentOS 7.7 for your home server
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Using a printer with Debian 10
Build a Chainer environment using CUDA and cuDNN on a p2 instance
Control the motor with a motor driver using python on Raspberry Pi 3!
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a Python environment on your Mac with Anaconda and PyCharm
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
How to develop containers on remote servers with VS Code Remote-Containers
Build a Python execution environment using GPU with GCP Compute engine
I made a poker game server chat-holdem using websocket with python
What I stumbled upon when using CodeIgniter on a Linux server