Nginx Related Tips and Configurations

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.

Noteable Nginx Points

For nginx, On most systems, The config can be found in /etc/nginx & virtual hosts are present in:

  • /etc/nginx/conf.d - CentOS/RHEL/AlmaLinux/RockyLinux Based systems
  • /etc/nginx/sites-available - Debian/Ubuntu Based systems

Things to know:

  • File names must end in a .conf as by default, the nginx.conf (the parent file) only sees .conf files (*.conf - setting).
  • 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.

Basic Nginx Config

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.

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

    location / {

        proxy_pass http://localhost:2000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $remote_addr;
    }

}

Creating phpinfo() page

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:

<?php
phpinfo( );
?>

After this, Navigate to the URL and the phpinfo() page should open.

Serving Static files on a reverse proxy

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:

location /your-path {
   alias /path/to/files;
   index example.htm;
#  try_files $uri $uri/ @reverse_proxy; # Can be added/deleted - Info below

Points to note:

  • 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
  • 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.
  • 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;.
  • This config does not account for .php and is purely for static content. However, .php is possible with addiotional setup
  • 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