Nginx on docker

Install the docker community edition on your server.

once installed, as per your requirement, you can setup the NGINX container.

we will follow the below steps –

  1. make an image using Dockerfile
  2. starting the container
  3. updating the nginx.conf file as per our env requirement
  4. starting the new container with nginx.conf mapping and ports mapping

Dockerfile (the directories created here are as per our requirement, you can change as per yours)

FROM nginx

RUN mkdir /kst-ngx_certs

RUN mkdir /kst-ngx_logs

RUN mkdir /kst-ngx_htdocs

RUN mkdir /kst-ngx_misc

##COPY static-html-directory /usr/share/nginx/html

build the image with command like –

docker build -t kst-ngx

nginx.conf for mapping (make changes before you attempt to start the container) –

user  nginx;

worker_processes  auto;

error_log  /var/log/nginx/error.log notice;

pid        /var/run/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

server {

    listen 80 default_server;

    server_name _; # This is just an invalid value which will never trigger on a real hostname.

    ##access_log logs/default.access.log main;

    server_name_in_redirect off;

    root  /var/www/default/htdocs;

  }

server {

    listen 8880 default_server;

    listen [::]:8880 default_server ipv6only=on;

    root /usr/share/nginx/html;

    index index.html index.htm;

    server_name localhost;

    location / {

        try_files $uri $uri/ =404;

    }

   }

server {

# Redirect to HTTPS

listen      80;

server_name nagios.domain.com www.nagios.domain.com;

return      301 https://nagios.domain.com/$request_uri;

}

server {

    listen 4443;

    server_name cl-domain.in;

    ssl_certificate           /kst-ngx_certs/nagios.domain.com.crt;

    ssl_certificate_key       /kst-ngx_certs/nagios.domain.com.key;

    ssl on;

    ssl_session_cache  builtin:1000  shared:SSL:10m;

    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;

    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;

    ssl_prefer_server_ciphers on;

    access_log            /var/log/nginx/nagios.bl.access.log;

    location / {

      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_set_header        X-Forwarded-Proto $scheme;

      # Fix the “It appears that your reverse proxy set up is broken" error.

      proxy_pass          http://localhost:80;

      proxy_read_timeout  90;

      proxy_redirect      http://localhost:8080 https://jenkins.domain.com;

    }

  }

}

Once required directory structure is in place with nginx.conf file, you can use something like this to start your container –

docker run --name kst-ngx-monwiki -v $PWD/ngx_logs:/var/log/nginx -v $PWD/ngx_certs:/kst-ngx_certs -v $PWD/ngx_misc:/kst-ngx_misc -v $PWD/ngx_conf/nginx.conf:/etc/nginx/nginx.conf:ro -p 8999:8099 -p 443:4443 -p 80:8800 -p 8081:80 -d kst-ngx-monwiki

Leave a Reply

error: Content is protected !!