DevOps – Nginx Configuration

What is Nginx?

If you already know what Nginx is, you can skip to the configuration part.

Nginx is a web server. A web server is the program that gets requests from a client (the browser) and returns responses (like a web page) to the client through HTTP. It’s able to accept requests and return responses through the HTTP protocol.

Does PHP tie into Nginx?

PHP does not really tie into Nginx or vice versa. Nginx just decides what to do with a request and where to send it. You can try to make a request to a website, but nothing will happen if Nginx isn’t listening for that request. You are the one who decides the general rules for requests. You create these rules in a configuration file. 

Like many web technologies, it’s possible to do a lot with Nginx, but many things you can do in Nginx can also be done in PHP.

Ngint also lets you serve multiple websites from a single IP address. This is known as virtual hosting.

Sometimes, walking through the steps helps you to understand something better.

  • Enter URL into a browser. https://paircode.com/forums/forum/general
  • Hit enter.
  • The browser (the client/user agent) makes an HTTP request to the server at pair code. com. The DNS is responsible for pointing the domain to the server using an IP address.
  • The Nginx web server gets this request.
  • It then looks up rules that you set up to decide where to send the request. It can send the request to a PHP file.
  • The PHP file gets the requests and builds out the web page.
  • PHP gives the HTML page back to the web server (Nginx), and NginX sends the HTML page (the response) back to the browser through HTTP.

Instead of sending an HTML page, the web server can also send back other files, like images, videos, CSS, and JavaScript files.

Some languages have packages that are web servers. In C#, the ASP.NET framework has a built-in server called Kestrel.

NodeJS has a package called “http”. This package which is used in place of Nginx.

Becoming familiar with Nginx is important, especially if you want to become a back end web developer. In the end, it’s just one more tool to add to your belt.

The Configuration

Create a file called vhost.conf in the docker folder.

cd ~/Code/appone/docker
touch vhost.conf

Copy the following configuration code into the file.

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass phpapp:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Nginx uses this configuration file to serve your website. That’s why Nginx is called a web server.

listen 80;

Listen to any incoming requests to port 80 of the server.

index index.php index.html;

The index (home page) of the website will either be an index.php or index.html file.

root /var/www/public;

The root (directory) of our web app is in /var/www/public.

fastcgi_pass phpapp:9000;

Nginx has to use PHP-FPM as a separate process to handle requests for PHP files. Apache just uses the mod_php module. If we were using Apache, we wouldn’t even need a separate container for Nginx.

In this line, we are proxying requests for PHP files to the php-fpm process of our phpapp container. The php-fpm process is listening on port 9000. The phpapp part corresponds to the name of the phpapp container.