Blame

05ed40 Arpan S. 2024-03-29 10:59:21 1
# Nginx Related Tips and Configurations
2
324fb8 Arpan S. 2024-03-29 11:25:18 3
This page contains all the information you'll ever need regarding nginx and how to work with it in both a production and development/static enviroment.
4
5
### Noteable Nginx Points
6
For nginx, On most systems, The config can be found in `/etc/nginx` & virtual hosts are present in:
7
- `/etc/nginx/conf.d` - CentOS/RHEL/AlmaLinux/RockyLinux Based systems
8
- `/etc/nginx/sites-available` - Debian/Ubuntu Based systems
9
10
Things to know:
11
- File names must end in a `.conf` as by default, the `nginx.conf` _(the parent file)_ only sees .conf files _(*.conf - setting)_.
12
- Files placed inside `sites-available` must be system linked using `ln` command to `sites-enabled` as by default, the `nginx.conf` _(the parent file)_ only sees the `.conf` files present inside `siltes-enabled` and not `sites-available`.
05ed40 Arpan S. 2024-03-29 10:59:21 13
14
### Basic Nginx Config
15
16
This is the most basic nginx config where `proxy_pass` & `server_name` values need to be modified and it should work in most applications. This config already passes the _real IP_ in the `proxy_set_header` value and it can be commented out by putting a `#` in the beginning of the line to _**disable**_.
17
18
```
19
server {
20
listen 80;
21
server_name www.example.com example.com;
22
23
location / {
24
25
proxy_pass http://localhost:2000;
26
proxy_http_version 1.1;
27
proxy_set_header Upgrade $http_upgrade;
28
proxy_set_header Connection 'upgrade';
29
proxy_set_header Host $host;
30
proxy_cache_bypass $http_upgrade;
31
proxy_set_header X-Forwarded-For $remote_addr;
32
}
33
34
}
35
```
022d06 Arpan S. 2024-04-27 04:24:24 36
37
### Creating phpinfo() page
38
The `phpinfo()` function in php outputs a number of important but compromizing details about the php enviroment that the script is running on. It can be used to troubleshoot or fetch info about the server. This script can be simply made by creating a file with any name with the `.php` extension & putting then following into it:
39
```php
40
<?php
41
phpinfo( );
42
?>
43
```
44
After this, Navigate to the URL and the `phpinfo()` page should open.
97d773 Arpan S. 2024-09-05 22:00:31 45
46
### Serving Static files on a reverse proxy
47
You might have a reverse-proxy site where you need to also serve some static files. Maybe the proxy is some docker app or some external app that you have no control of but want to serve some images on /img or a .html under some random path. This can be easily done by adding the following config under the `server` directive:
48
49
```
50
location /your-path {
51
alias /path/to/files;
52
index example.htm;
53
# try_files $uri $uri/ @reverse_proxy; # Can be added/deleted - Info below
54
```
55
Points to note:
56
- If the provided path conflicts with a path being served by the reverse proxy then the directive set on the `nginx config` always takes priority
57
- Make sure `nginx` has `rw` (read/write) permissions to the directory set in `alias`. You can either group it under `www-data` or `nginx` user depending on your OS or set the permission to _777_.
58
- You can index any file you want (eg. .png/.jpg/.txt/.html/.sh) or turn on `autoindex` so that it lists all files inside of the directory. This can be done by adding or replacing index line with `autoindex on;`.
59
- This `config` does not account for .php and is purely for static content. However, .php is possible with addiotional setup
60
- `try_files` directive is for conflicts. You can setup the reverse-proxy under `@reverseproxy` or any other name of your choosing. So if you have `/img/logos` that is handled by the proxy site and you need to push `/img/vectors` & `/img/background` as static, then use this function to achieve this. _**Note:** If your reverse proxy handles invalid URIs and shows 404/403 pages then your static config will not work with this_.
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9