WikiJS/home/linux/nginx/configuration.md
2025-06-08 22:19:54 +00:00

114 lines
2.5 KiB
Markdown

---
title: NGINX Configuration
description: How to simply configure and NGINX http server
published: true
date: 2025-06-06T12:27:48.905Z
tags: server, nginx, http, linux
editor: markdown
dateCreated: 2025-06-06T12:27:47.287Z
---
# Configuring NGINX
#### Note:
Following "The NGINX Crash Course" by Laiture on Youtube.com
*Big BIG thanks to Laiture for this incredible how to video.*
##### - */etc/nginx/nginx.conf ==> main configuration file*
## Terminology:
Directives - key value pairs within blocks of code
Contexts - blocks of code that contain directives. http, events, etc... events context must be present.
To Start NGINX:
`nginx`
Simplest Static Site:
- Use http and events context:
```
#nginx.conf example
http {
server{
listen 8080;
root /var/www/html;
}
}
events {}
```
Reload NGINX after changes:
`nginx -s reload`
## Mime Types
- Mime types allow styles and other dynamic content to be served based on filetypes.
- Mime.types file contains a collection of filetypes supported by NGINX to serve.
- Including it in the conf file allows dynamic content to be served.
To include Mime Types simply add:
`include mime.types`
## Location Block
- Adding a location block can be done as the following:
```
server {
listen 8080;
root /var/www/html;
location /somepath { # http://localhost:8080/somepath
root /var/www/html; # Serves index.html within /somepath folder in /var/www/html
}
}
```
- Location can take directives such as:
1. *alias* - redirects elsewhere such as:
```
server {
listen 8080;
root /var/www/html;
location /somepath { # http://localhost:8080/somepath
root /var/www/html; # Serves index.html within /somepath folder in /var/www/html
}
location /otherpath {
alias /var/www/html/somepath; # Points back to "somepath".
}
}
```
2. *try_files* - Specifies files to try before index.html, followed by what to show if not found.
- Example: ` try_files /somepath/somesite.html /index.html =404; `
- Explanation:
- Try to find and serve *somesite.html* in *somepath* folder and if not found, redirect to *root/index.html*.
- If neither are found, produce 404 Error.
## Redirects & Rewrites
### Redirects:
```
location /somesite {
return 307 /someOtherSite;
}
```
### Rewrite:
```
rewrite /some/path/I/want /to/some/path/that/exists;
location { ... }