Familiarize yourself with the installation and configuration of Nginx

Tram Ho

The content of the article is completely theoretical so that you have an overview of what Nginx has and what to do. So today I’m going to learn how to configure Nginx for your project. Hopefully the article will be useful to you.

Setting

Installing Nginx from supplies including third-party modules makes Nginx much more powerful. It allows us to customize it to fit the needs of the application.

Use Debian to install.

The way you can check that the above command has successfully installed Nginx is to use the version checking command.

Result The webserver you just installed will have installation information at

These are the folders and files inside it

As you can see, there are quite a lot of folders and files inside. However, we only need to pay special attention to nginx.conf and the sites-available directory.

Configuration

By default, the nginx.conf file will look like this:

The file is structured into Contexts . The first is the events context , the next is the http context and the last is an example to configure mail. The above structure allows for some advanced configuration classes. Because in each context there may be other nested contexts that can inherit from their parents and it is also possible to override when needed.

Many things in this file can be customized based on your needs. However, with the simplicity of Nginx, you can completely use the default configuration without any modification. With the current reality as I did not edit this file ? . Some important parts of this configuration file are:

  • worker_processes: Determine the number of worker processes that Nginx will use. Because Nginx is a single thread, this number is usually equal to the number of cores of the CPU.
  • worker_connections: This is the maximum number of connections at a time per worker process and indicates how many users the worker process can be served simultaneously by Nginx. The larger this number, the higher the number of users served by Nginx.
  • access_log and error_log: These are the files that Nginx uses to record errors and access.
  • gzip: These are the settings for compressing the GZIP of the responses. Activating this setting along with the other sub-settings by default will increase performance.

Installing Nginx is possible for more than one site and server identification files. Your sites are located in /etc/nginx/sites-available .

The sites-available directory includes configurations for virtual hosts. This allows the web server to be configured for many specially configured websites. The sites in this directory don’t work and are only enabled if we create a sumbolic link to the /etc/nginx/sites-enabled .

We try with a configuration file defined in the Laravel document

Like nginx.conf , the file as above is defined in the nested context. The server context defines a specific virtual server to handle user requests. You can have multiple server blocks and Nginx will choose between them based on listen and server_name .

In the server block, we define multiple location contexts that are used to decide how to handle client requests. Whenever a request comes in, Nginx will try to compare its URI with one of the appropriate locations from which to provide appropriate handling.

Some important content is used in location context :

  • try_files will try to serve a static file in the directory pointing to root.
  • proxy_pass will send a request to a specified proxy server.
  • The rewrite will override the incoming URI based on the regex so a location block will be able to handle it.

Start Nginx

After a long series of steps to configure Nginx for your server as above, we can then start Nginx as follows:

And when you make any changes in Nginx configuration, you need to reboot (without down time) as follows:

To check the status of Nginx:

Stop Nginx service:

Stop and restart Nginx

Conclude

This article is a basic starting point for you to configure Nginx for your own applications. However, as you see the above configuration information is completely in the form of copy / paste and still do not understand each command line with the purpose, Let’s find out them in the following articles.

Share the news now

Source : Viblo