Nginx basics
What is Nginx and how to add your own configurations?
What is Nginx
Nginx is an open source webserver. Next to webserver Nginx is also used as reverse proxy, HTTP cache and load balancer.
The main goal of Nginx is to deliver high performance with low memory usage.
Hipex uses Nginx because tests have shown it achieves better performance results compared to Apache. A few years ago the difference in performance was significant, nowadays Apache has managed to narrow the gap.
How do I add custom Nginx configurations?
Every website and webshop is different. That's why it's often required to add custom Nginx configurations for your web applications on top of the default configuration that already lives on your server.
In this article we explain how you can do that.
The directory structure
Below reflects the underlying directory structure of the aforementioned location.
var/etc/
├── <domainname>
├── <domainname>-443
├── <domainname>-80
├── <domainname>-8080
├── port-443
├── port-80
├── port-8080
└── scope-http
Be aware that for every domain and/or domain reference which is added to the hosting panel the next four maps are automatically created.
- <domainname>
- <domainname>-443
- <domainname>-80
- <domainname>-8080
Next to the domain specific directories the following directories are present:
- port-443
- port-80
- port-8080
- scope-http
Now we will explain these directories in detail.
Domainspecific directories
We start by explaining the first four directories mentioned above. These are all directories belonging to a specific domainname.
Configurations which are placed in these directories are loaded in to the server block of nginx.
Directory <domainname>
The configurations in this directory are solely serving this domainname, regardless of the port on which the request enters. The configurations in this directory overrule the configurations placed in the other 3 maps explained below.
Directory <domainname>-443
The configurations in this directory have to meet the next two criteria:
- The configuration solely serves the specific domainname
- The configuration solely serves traffic entering port 443 (https)
For example a rewrite rule in this configuration will only affect the specific domainname.
Directory <domainname>-80
The configurations in this directory have to meet the next two criteria:
- The configuration solely serves the specific domainname
- The configuration solely serves traffic entering port 80 (http)
For example a rewrite rule in this configuration which will relay all http traffic to http only affects the specific domainname.
Directory <domainname>-8080
The configurations in this directory have to meet the next two criteria:
- The configuration solely serves the specific domainname
- The configuration solely serves traffic entering port 8080
Generic directories
Next to the domain specific directories, the 4 generic directories will be explained.
port-443
Different from the configurations located in the domain directories, the configurations added to the 'port-443' directory are related to all domains.
The sole criterium is the configurations located here are only valid for incoming traffic on port 443.
port-80
This directory serves the same purpose as the 'port-443' directory, the configurations added to the 'port-80' directory are related to all domains.
The sole criterium is the configurations located here are only valid for incoming traffic on port 80.
port-8080
This directory also serves the same purpose as the 'port-443' directory, the configurations added to the 'port-8080' directory are related to all domains.
The sole criterium is the configurations located here are only valid for incoming traffic on port 8080.
scope-http
The configurations placed in the scope-http directory are loaded in the HTTP location block. This renders it possible to add Nginx configurations at the HTTP level. Which, for example offers the option to use a 'map'variable. Using the map variable it is, for example, easy to configure storecodes belonging to a domain. How to this is explained in this article.
Adding Nginx configurations
Once you have a basic understanding of the directory structure, we will add our nginx configuration. In our example we will make sure all the HTTP traffic is relayed to HTTPS
To do this we first create a new configuration https.nginx.conf
at location /home/<username>/domains/<domainname>/var/etc/port-80
IMPORTANT: It is mandatory to name all your nginx configurations, (irrespective in what (sub)directory) to end with the extension .nginx.conf
.
By following this convention, your custom configuration will automatically be included.
In the newly created file we add the following rule:
return 301 https://$host$request_uri;
Reload Nginx
Now save your file and reload the nginx configuration using this command:
nginx-reload
This command renders output to your screen showing if the reload was succesful: If so, the following notification is shown:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Nginx configuration reload successful
In case of a failed configuration this notification, or a similar one, is shown:
nginx: [emerg] unknown "request_hipex_uri" variable
nginx: configuration file /etc/nginx/nginx.conf test failed
The variable request_hipex_uri
used in this example should have been request_uri
.
Mind you every adjustment needs the nginx-reload
command to be executed afterwards.