Basic web application configuration with Nginx on CentOS

After you have installed Nginx, you can deploy your application to be able to access it using the browser. Access by browser here means we only use domain or IP address with the default port of HTTP is 80 or HTTPS 443 without having to add the port! By default, Ngnix supports us to run applications written in Go, Node.js, Perl, PHP, Python, and Ruby. Of course, applications that only use HTML, CSS, and Javascript are also supported. In this tutorial, I will show you how to configure a basic web application to be able to run with Nginx on CentOS!

To configure a basic web application with Nginx, there are 2 steps you must do:

  • Copy the entire source code of your web application into any directory on the server where Nginx is installed.
  • Create a new .conf file in the /etc/nginx/conf.d directory that defines the information for your web application.

For the first step, let me tell you something first. You can see that after installing Nginx, a default page is displayed when we access the address http://<domain_or_ip_server>:

Basic web application configuration with Nginx on CentOS

The content of this default page is stored in /usr/share/nginx/html directory:

Basic web application configuration with Nginx on CentOS

You can also copy the source code into a new directory in /usr/share/nginx/html directory as well.

For example, I have a website with simple content as follows:

hen I will create a new directory named huongdanjava in /usr/share/nginx/html directory and create a new index.html file with the content as above:

Basic web application configuration with Nginx on CentOS

Now, we are going to configure our source code directory with Nginx to gain access to our web application.

To do this, as I said, we need to create a new .conf file in the /etc/nginx/conf.d directory to define the information to your web application.

The configuration of the default website that when you install Nginx, you see, is in the nginx.conf file in that /etc/nginx folder! This is the main configuration file of Nginx, the contents of this file are as follows:

There is a lot of information in this configuration file, right? To understand them, you need to know that these configuration information are called directives in Nginx. These directives are grouped into blocks corresponding to different contexts for different purposes. You can find details about each of Nginx’s directives here.

In the nginx.conf file above, we have the outermost directives, they are considered to be in the main context:

To configure your website, you need to know the http directive!

The Http directive contains other directives to handle requests from users to your site. The main directive to do this inside the http directive is the server directive.

In this http directive, as you can see, we have a directive named include, which will load all the configurations of the websites we want in the /etc/nginx/config.d/ directory. Every time you want to add a new website with Nginx, just create a new file with the file name in the format <domain_or_ip> .conf in /etc/nginx/config.d/ folder and then configure the information for it. The content we need to declare in the configuration file of each website is the content of that server directive. This information basically consists of the following:

Inside:

  • The listen directive configures the port number that Nginx will listen for users to request to your web application
  • The server_name directive defines the domain name information that is pointed to your server’s IP address. If you use IP to access your application, you can skip this directive.
  • The root directive defines the path to the source code of your application
  • The location directive defines the directory and file that will be processed from the user’s request.

Imagine, root directive defining mapping from domain or server’s ip to the root of your source code. The location directive defines the requests your application will handle. Corresponding to each location we can define additional root directives to specify which file directory to handle for a given request.

In the server directive definition above, we are configuring the request with the “/” context path in the location directive to the application’s root directory.

With the current configuration, I can access my web application with the following URL request: http://localhost/huongdanjava

Basic web application configuration with Nginx on CentOS

That’s because our application’s code is located in /usr/share/nginx/html directory and the location in the above configuration is mapping to this directory. Another note for you is that, by default, if you do not specify the file of the web application to access, by default Ngnix will display the contents of the index.* file. “*” here means there are many other extensions that Nginx supports.

To configure the example web application above, you can access it directly from http://localhost/, first I will open the file /etc/nginx/nginx.conf and comment server directive in http directive.

Then, I will create a new file named huongdanjava.com.conf in /etc/nginx/config.d/ directory with the following content:

Restart Nginx:

Then access to http: // localhost, you will see the following results:

Basic web application configuration with Nginx on CentOS

So we have successfully configured our web application with Nginx!

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment