Nginx
Installing
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:
stopquit(better than stop)reloadreopen
Directories
/etc/nginx: default configuration root files/etc/nginx/nginx.conf: default conf. entrypoint. Includes info aboutconf.d/etc/nginx/conf.d: used in the top-level block directivehttpin thenginx.conffile./var/log/nginx/: default log locationaccess.log: entry for each request to nginx servererror.log: debug info.
Directives
includes
For keeping our conf. logically grouped into modular configuration sets.
- takes in path/file/mask that matches many files.
location
- 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_namedirective defines the hostname or the names of requests that should be directed to this server. If the configuration had not defined this context as thedefault_server, NGINX would direct requests to this server only if the HTTP host header matched the value provided to theserver_namedirective. With thedefault_servercontext set, you can omit theserver_namedirective 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
indexdirective 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;
}
}
streamoperates at layer 4.httpat 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
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 byNginxby 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.confunless required a global change. -
Steps to create a custom
confare:- Create a
confundersites-available - Link it inside
sites-enabled - Reload
nginx
- Create a
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
conffiles undersites-availablehave same servers i.e. same ports or server names, conflict will arise. -
Either change the server name or unlink the previous
confby:sudo unlink /etc/nginx/sites-enabled/namebefore linking the new one.
Sample urlShort conf
- There is an index.html under /var/www/testDir/show.