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

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