NGINX Commands (Ubuntu)

A short guide of commands I've found myself using on Ubuntu for NGINX, I'll update this post as I find more.

Installing NGINX

sudo apt-get update

sudo apt-get install nginx

Restarting NGINX

To restart NGINX

sudo systemctl restart nginx

To just reload the NGINX

nginx -s reload

This will reload the NGINX config and any changes you make to it.

NGINX Config

A Sample Config for one of my static sites.

server {
  listen 80;

  listen [::]:80;

  root /var/www/jameskenny.co;

  index index.html;

  server_name jameskenny.io www.jameskenny.io;

  location / {
    try_files $uri $uri/ =404;
  }
}

A sample config of a reverse proxy for a docker container.

server {
  listen 80;

  listen [::]:80;

  server_name touroperator.io www.touroperator.io;

   set $upstream 127.0.0.1:8080;

 location / {
 
 proxy_pass_header Authorization;
 proxy_pass http://$upstream;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_http_version 1.1;
 proxy_set_header Connection "";
 proxy_buffering off;
 client_max_body_size 0;
 proxy_read_timeout 36000s;
 proxy_redirect off;

 }
}

A short guide of commands I've found myself using on Ubuntu for NGINX, I'll update this post as I find more. Installing NGINX sudo apt-get update sudo apt-get install nginx Restarting NGINX To restart NGINX sudo systemctl restart nginx To just reload the NGINX nginx -s reload This will reload…

Read More

NGINX Reverse Proxy for Docker

This post is a guide on setting the NGINX Config to work as a reverse proxy to our web application inside a docker container. When you use docker to host your web applications and services. It's handy to be able to use a reverse proxy to pass the traffic on port 80 and route it to the right container.

One cool thing about NGINX is we can running docker and static sites on the same linux box. It's just a case of having different NGINX config files.

I've setup a web application built in .net core and running inside a container. On my linux server I create a docker instance and set it to run on port 8080.

docker run --name some-app1 -d -p 8080:80 some-app1

Once the docker container is running we can go ahead and create a NGINX config for the reverse proxy.

Reverse Proxy Config

First in the folder we need to create our config.

/etc/nginx/site-available

The following is our reverse proxy config for a website I have called 'touroperator.io'

server {
  listen 80;

  listen [::]:80;

  server_name touroperator.io www.touroperator.io;

   set $upstream 127.0.0.1:8080;

 location / {
 
 proxy_pass_header Authorization;
 proxy_pass http://$upstream;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_http_version 1.1;
 proxy_set_header Connection "";
 proxy_buffering off;
 client_max_body_size 0;
 proxy_read_timeout 36000s;
 proxy_redirect off;
 }
}

The config is doing a number of things.

  • Listen for traffic on Port 80
  • Only deal with requests for the touroperator.io or www.touroperator.io domain name
  • Upstream we set it to 127.0.0.1 and port 8080 so it's sending traffic to it's own network card on port 8080. This is the same port our docker image is running on.
  • Next in Location we pass a number of bits of information up the pipe to the website.

Note: this type of config can be used for load balancing and other things. As I explore this deeper more posts will follow.

Next we need to create a short cut in the 'site-enabled' folder.

ln -s /etc/nginx/sites-available/touroperator.io /etc/nginx/sites-enabled/touroperator.io

Remember to change the file name to your own.

Lastly reload the NGINX server

nginx -s reload

And point your DNS and you are running. The traffic will be routed to the docker container.

This post is a guide on setting the NGINX Config to work as a reverse proxy to our web application inside a docker container. When you use docker to host your web applications and services. It's handy to be able to use a reverse proxy to pass the traffic on…

Read More

Setting up Docker on Linode

A quick guide to setting up Docker on a Linode Ubuntu server. As I go deeper into exploring docker and building applications and services in new ways, I wanted a post that acts as a starting point.

Installing docker

$ sudo apt-get update

First, add the GPG key for the official Docker repository to the system:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add the Docker repository to APT sources:

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Then Install Docker

sudo apt-get install -y docker-ce

Check the Docker Status

sudo systemctl status docker

That's it docker is now running on our Linux server. I will be adding more posts to the blog as I explore docker more.

A quick guide to setting up Docker on a Linode Ubuntu server. As I go deeper into exploring docker and building applications and services in new ways, I wanted a post that acts as a starting point. Installing docker $ sudo apt-get update First, add the GPG key for the official…

Read More

NGINX hosting a site on Linode

I wanted to host my own personal site https://jameskenny.io on a Linode Linux server and run NGINX to host the static site.

I've used Linode before and really recommend them. Also will be doing some work with hosting docker containers. Over the next few blog posts I'll go into the details of how to do it.

Linode

Linode are a hosting company that offer Linux hosts with SSD hard drives. Linode offers some great management systems. It's quick and easy to start a server.

I create a new Linode a Linode 1GB in Frankfurt, Germany. This server will cost $5 a month. Not bad for my own site and a few other test ideas. I deployed with Ubuntu Linux.

Once it's deployed we go ahead and ssh onto the server.

Install NGINX

So on our Ubuntu Linux, we first run

sudo apt-get update

sudo apt-get install nginx

NGINX is now installed.

Setting up our site

My site is just a simple HTML page wiht some css behind it.

By default, NGINX expects your static files to be in a specific directory. You can override this in the configuration. But for what I'm doing the defaults are ok.

/var/www/

So I go ahead and create a new folder jameskenny.io and upload my files.

/var/www/jameskenny.io

Configure NGINX to serve the website

Next we need to setup the config on the NGINX to serve the sites.

/etc/nginx/

We're interested in two folders here.

  • sites-available this contains individual configuration files for all of your possible static websites.
  • sites-enabled this contains links to the configuration files that NGINX will actually read and run.

So in our 'sites-available' folder we create a new config file called

jameskenny.io

server {
  listen 80;
  
  listen [::]:80;
  
  root /var/www/jameskenny.io;
  
  index index.html;
  
  server_name jameskenny.io www.jameskenny.io;
  
  location / {
    try_files $uri $uri/ =404;
  }
}

Make sure to replace the jameskenny.io with your domain and folder path.

This file tells NGINX a few things:

  • Listen to any traffic on port 80
  • Deliver files from the folder /var/www/jameskenny.io
  • The main index page is called index.html.
  • Requests that are requesting jameskenny.io should be served by this server block.
  • Note the www is also listed separately. This tells nginx to also route requests starting with www to the site. There’s actually nothing special about the www — it’s treated like any other subdomain but you can see how you can use this for other subdomains very quickly.

Next we need to create a short cut in our sites-enabled folder. In our SSH shell we run the command.

ln -s /etc/nginx/sites-available/jameskenny.io /etc/nginx/sites-enabled/jameskenny.io

This will create a shortcut pointing to our config to tell NGINX to server our site.

Now we restart NGINX to load it all up ok.

sudo systemctl restart nginx

Just point your A record to your new server and go check out your site.

I wanted to host my own personal site https://jameskenny.io on a Linode Linux server and run NGINX to host the static site. I've used Linode before and really recommend them. Also will be doing some work with hosting docker containers. Over the next few blog posts I'll go…

Read More

Azure Functions with Continuous Deployment

A quick guide to setting up continuous deployment for Azure functions.

Continuous Deployment or Continuous Delivery allows us to deploy our application / code into the production or test based on each check in to our source control, this allows us to focus our craft and not on deployments.

Setting up CD for Azure functions is really a simple process.

Source code can be found here

Create a function

First we need some functions. I've created a simple GET function. I'm assuming you have setup functions and seen them in action already. If not check out my blog posts on Azure functions

Create a folder for your azure function.

Add a host.json file - Leave this empty for now.

{}

By default the host.json needs to have {} this leaves it empty, leaving it blank will stop the function from starting.

Create a folder for your first function. Each function should have a folder.

In this folder create 3 files.

  • project.json
{
    "frameworks": 
    {  
     "net46":
     { 
      "dependencies":
      {
        "Newtonsoft.Json": "10.0.3"
      }
     }
   }
}
  • function.json
{
    "disabled": false,
    "bindings": [
      {
        "authLevel": "anonymous",
        "name": "req",
        "type": "httpTrigger",
        "direction": "in"
      },
      {
        "name": "$return",
        "type": "http",
        "direction": "out"
      }
    ]
}

Note: I've created this function as a GET with anonymous authentication so we can call it with no keys.

  • run.csx
using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");
    
    return req.CreateResponse(HttpStatusCode.OK, "Hello there");
}

Go ahead and commit the function to your source control.

Create a function

Back onto the Azure portal. Create a new function app.

Once created head to the "Platform Features" Tab

Azure-Function-platform

Select Deployment options and select "Setup"

azure-deployment-setup-2

Configure sources

Azure functions allows you to deploy from different sources.

azure-function-sources

Follow the steps to select the source you want. I am going with Github.

Once setup follow the steps to add your functions source. Then click sync or make a change and push to your source control.

Azure-Functions-Sync

That's it go ahead and test the function.

Azure-Functions-Test

When I run the test I get the hello response.

Azure makes it really easy to deploy from source control. There is the ability to add tests aswell, I'll cover that in another post.

A quick guide to setting up continuous deployment for Azure functions. Continuous Deployment or Continuous Delivery allows us to deploy our application / code into the production or test based on each check in to our source control, this allows us to focus our craft and not on deployments. Setting up…

Read More