Skip to content

Nginx

Installing

Nginx Installation
apt update
apt install -y curl gnupg2 ca-certificates lsb-release debian-archive-keyring`
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
  | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null`
OS=$(lsb_release -is | tr '[:upper:]' '[:lower:]')
RELEASE=$(lsb_release -cs)
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
    http://nginx.org/packages/${OS} ${RELEASE} nginx" \
    | tee /etc/apt/sources.list.d/nginx.list

apt update
apt install -y nginx
systemctl enable nginx

Starting nginx

Do nginx in the terminal.

Check whether it is running

curl localhost

Tip
  • By default the url is: localhost:80
  • or do ps -ef | grep nginx

Testing terminally

nginx -t or nginx -T to verify whether all the confs are in good shape.

Signals

  • nginx -s <signal>
Info
  • Signals:
    • stop
    • quit (better than stop)
    • reload
    • reopen

Directories

  • /etc/nginx: default configuration root files
  • /etc/nginx/nginx.conf: default conf. entrypoint. Includes info about conf.d
  • /etc/nginx/conf.d: used in the top-level block directive http in the nginx.conf file.
  • /var/log/nginx/: default log location
    • access.log: entry for each request to nginx server
    • error.log: debug info.

Directives

includes

For keeping our conf. logically grouped into modular configuration sets.

1
2
3
4
http {
    include conf.d/compression.conf;
    include ssl_config/*.conf
}
  • takes in path/file/mask that matches many files.

location

server {
  listen 80 default_server; 
  server_name www.example.com; 

  location / {
    root /usr/share/nginx/html;
    # alias /usr/share/nginx/html;
    index index.html index.htm;
  }
}
  • This configuration serves static files over HTTP on port 80 from the directory /usr/share/nginx/html/.
  • The first line in this configuration defines a new server block. This defines a new context that specifies what NGINX listens for. Line two instructs NGINX to listen on port 80, and the default_server parameter instructs NGINX to use this server as the default context for port 80.
  • The listen directive can also take a range of ports. The server_name directive defines the hostname or the names of requests that should be directed to this server. If the configuration had not defined this context as the default_server, NGINX would direct requests to this server only if the HTTP host header matched the value provided to the server_name directive. With the default_server context set, you can omit the server_name directive if you do not yet have a domain name to use.

  • The location block defines a configuration based on the path in the URL. The path, or portion of the URL after the domain, is referred to as the uniform resource identifier (URI).

  • NGINX will best match the URI requested to a location block. The example uses / to match all requests. The root directive shows NGINX where to look for static files when serving content for the given context. The URI of the request is appended to the root directive’s value when looking for the requested file. If we had provided a URI prefix to the location directive, this would be included in the appended path, unless we used the alias directive rather than root.
  • Finally, the index directive provides NGINX with a default file, or list of files to check, in the event that no further path is provided in the URI.

Load Balancing

http load balancing

http + upstream module.

upstream backend {
  server 10.10.12.45:80 weight=1;
  server app.example.com:80 weight=2;
  server spare.example.com:80 backup;
}
server {
  location / {
    proxy_pass http://backend;
  }
}

tcp load balancing

stream + upstream module.

stream {
  upstream mysql_read {
    server read1.example.com:3306 weight=5;
    server read2.example.com:3306;
    server 10.10.12.34:3306 backup;
  }

  server {
    listen 3306;
    proxy_pass mysql_read;
  }
}
  • stream operates at layer 4.
  • http at layer 7.

Load Balancing Methods

There are various load balancing methods include in nginx free as well as nginx plus.

  • Round Robin
  • Least Connections (least_conn)
  • Least Time (plus only) (lowest avg. response time)
  • Random (random)
Example
1
2
3
4
5
upstream backend {
    least_conn;
    server backend.example.com;
    server backend1.example.com;
  }
Important

Difference between nginx.conf and the sites-available/* and the sites-enabled/*

  • nginx.conf: the main global configuration file. A change here affects everything.
  • sites-available/*: stores all virtual host configurations/websites (active or inactive).
  • sites-enabled/*: controls which configurations are actually used by Nginx by containing symlinks to active configurations in sites-available.

Creating a custom .conf for a server

There can be multiple servers hosted on nginx.

  • Don't mess with the global nginx.conf unless required a global change.

  • Steps to create a custom conf are:

    • Create a conf under sites-available
    • Link it inside sites-enabled
    • Reload nginx
Example
- Created `urlShort` under `sites-available`.
- Linked: `sudo ln -s /etc/nginx/sites-available/urlShort /etc/nginx/sites-enabled/`
- `sudo nginx -s reload`
Note
  • If the conf files under sites-available have same servers i.e. same ports or server names, conflict will arise.

  • Either change the server name or unlink the previous conf by: sudo unlink /etc/nginx/sites-enabled/name before linking the new one.

Sample urlShort conf

1
2
3
4
5
6
7
8
server {
    listen 80;
    listen [::]:80;

    location /show {
        root /var/www/testDir;
    }
}
- There is an index.html under /var/www/testDir/show.