DEV Community

shiva kumar
shiva kumar

Posted on • Updated on

Welcome to Nginx! (Part-II)

This post is the continuation of Part-I. If you haven't read that article, I recommend to read that.

By default nginx has some folders and files as shown below.

nginx_folder

We are interested into two main folders

1) /etc/nginx/conf - Where our most of the configuration goes
2) /etc/nginx/logs - Server logs files resides there. They are access.log and error.log files

Lets dive into conf folder first. Conf Folder has many .conf files as mentioned below, which are for different protocols

i) fastcgi.conf - This configuration is used for fastcgi protocol application
ii) nginx.conf - This is configuration we are interested in, we will break down this file and go in details regarding the configuration
iii) scgi_params - This file is used for passing request to an SCGI server
iv) uwsgi_params - This file is used for processing request to the uwsgi application e.g used with python application
v) mime.types - Multi Internet Mail Extension which has list of formats to support email and http

Log folder has error.log and access.log. Each request gets logged in access log and whenever a request throws a error that gets logged in error.log

Nginx configuration file consist of two main things, Directives and Contexts. Directives are simple single line configuration which ends with semi-colon and Contexts are top level directives that are grouped together with multiple directives.

Example of Directives are

user www-data;
include conf.d/first_nginx.conf;
Enter fullscreen mode Exit fullscreen mode

There are four main context

1) events - connection processing
2) http - for serving http/https traffic
3) mail - for serving mail traffic
4) stream - for steaming TCP and UDP packets

Let's create a simple static page and let's configure it with nginx to access across the internet. First, let's put our project inside a folder and create a homepage.html

mkdir apps && cd apps && mkdir welcome
cd welcome && touch homepage.html
Enter fullscreen mode Exit fullscreen mode

Now that we have created our welcome page now let's configure this to nginx.Instead of writing inside the default nginx.conf. We are going to create a separate configuration file called
first_nginx.conf inside /etc/nginx/conf/ . If you remember from the first post this is where we installed the nginx. Let's add below configuration and save

server {
  listen 80;
  # on which port nginx should listen for request


  server_name  server_ip;
  # domain name or ip address of the server

  root         /home/ubuntu/apps/welcome/;
  # path where the project resides

  index homepage.html;
  # default page to load

}
Enter fullscreen mode Exit fullscreen mode

Now let go to default nginx.conf file and include our first nginx configuration file inside http context. I have tried to explain each directive and context in single line comment

#user  nginx;
# user and group credentials used by worker_process if a group is not specified it will be equal to the user

worker_processes  1;
# Define the number of worker_processes, an optimal value would be the number of CPU cores in the server or set to auto

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
    #Number of maximum connection can be opened by one worker process concurrently

}


http {
    include       mime.types;
    default_type  application/octet-stream;
    # This means the response is a binary file, if it is not specified the response is set to text/plain


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    # Formatting the request data log we can configure the parameter we need in     the log.

    #access_log  logs/access.log  main;

    client_max_body_size 20M;
    # configuration to set max client body size, if the request body is greater than 20M 
    # (413 status code) Request Entity Too Large is return to client


    keepalive_timeout  65;
    # for how many seconds the connection to be alive if it takes more the 65s to process it will connection time out

    include /etc/nginx/conf/first_nginx.conf;
    # Rather than writing the all the configuration, we can write the configuration in different and include in # the default conf file to make it easier to maintain

    #gzip  on;
    # compresses the response using gzip be default it is off
}
Enter fullscreen mode Exit fullscreen mode

Now we are going to restart the server

sudo service nginx restart

Enter the ip address of the server in the browser .... ta-da. You did it NNN(Novice Nginx Ninja 👨🏻‍💻 )

home_page

Top comments (0)