Introduction
Nginx is open-source software that is popular worldwide for its web-serving features. Initially, it was designed as the fastest and most stable webserver. Later on, its features included caching, reverse proxy, load balancing, media streaming, and much more. What makes Nginx popular among the top web servers? Its high performance and quick stable loading make it a heart favorite of proxy users.
This blog will educate you about the exclusive features of Nginx. Further, how to use Nginx reverse proxy for two different apps using two subdomains. We will also discuss the procedure to set up the Nginx reverse proxy via local resolution. It will help you to create a mirror setup instantly. For better and quick understanding, we will use the HTTP tutorial. But the users can also use the same configuration setup for HTTPS.
For developing a static web server, SimpleHTTPServer- a product of Pathon will be used. Similarly, we will use Python to develop dynamic new apps.
Nginx –the fastest web-server
Before moving to the main topic, it is necessary to have a look at the Nginx features as a web server.
Nginx is the open-source web server that is popular and widely used among high-traffic sites. The most intensive websites like NASA, WordPress.com, and Netflix are also the users of Nginx.
Nginx uses an asynchronous mechanism that makes it more favorable. This mechanism uses a single thread to handle the requests. Thus, it utilizes low memory and is highly concurrent. As Nginx is asynchronous, it smoothly handles abundant requests without blocking or slowing down the sites.
Nginx has wider applications in the web serving world like:
- IPv6
- Load balancing
- Media Streaming
- Fast CGI support with caching
- Reverse proxy with caching
- Handles static files, index files, and creation of the auto-index
Nginx is a diverse web server that serves the IT world surprisingly. If you have not used it yet, try some of its features.
System requirements
For the users’ understanding, we have been using BitLaunch Ubuntu 20.04 LTS VPS for Nginx reverse proxy lessons. You can get connected to your VPS via Putty or any other SSH client servers. However, any new Unbuntu server can also be used using the given commands in the tutorial.
How to Prepare the servers?
Before the installation of Nginx, it is necessary to prepare the server for changes in the setup. Firstly, we need to update the local package index. If any outdated package exits, there is a need to upgrade it to move further.
Otherwise, Nginx reverse proxy may not get installed or work properly.
$ apt-get update Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease Hit:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease Hit:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease Reading package lists... Done
$ apt-get -y upgrade Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following packages were automatically installed and are no longer required: .... ....
Install Nginx Server
Nginx is among the default repositories of Unbuntu. So, the users need not add external repositories. All these repositories follow the mirror of the official versions. For the latest and true repository, it is better to fulfill the requirements of Nginx Installation and documents.
When the server is prepared, it is time to install the Nginx package via the apt package manager. For the Nginx installation, you can watch a detailed method on the Nginx installation tutorial.
$ apt install -y Nginx Reading package lists... Done Building dependency tree Reading state information... Done .... ....
Verify & configure Nginx Version
As the installation gets completed, the user needs to verify the Nginx version. Further, it also demands configuration with the -V flag.
$ nginx -V
nginx version: nginx/1.17.10 (Ubuntu) built with OpenSSL 1.1.1f 31 Mar 2020 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-Pmk9_C/nginx-1.17.10=. -fstack-protector-strong - .... ....
Access the external server by configuring a Firewall
At this point, if you want to allow external access to your server, allow port 80. If you are not interested, skip this process.
$ sudo ufw allow 80
Install Python and HTTPie
Finally, at this step, we are ready to create two apps. To develop the apps, we will use the Python platform. as described earlier, Python is the default repository. So, the process will continue smoothly.
We will use Python to create our two apps, so let’s install the required packages. Python is included in the default package repositories.
$ apt install -y python
Here, the system will ask for verification and testing. You can use HTTPie or Curl.
$ apt install -y httpie
Make Sure NGINX Starts After Boot
You need to enable Nginx to start after boot. Give a command to the system that after booting you need Nginx start.
$ sudo systemctl enable nginx
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx
Start NGINX Server
Here the system is ready to start the Nginx server.
$ systemctl start nginx
How to create two Apps using two domains?
Now, we are going to develop two apps using two domains. In this way, you can test your setup as well. For this purpose, we will use the Python platform and its module SimpleHTTPServer. Python’s SimpleHTTPServer is a static web server that cannot be used for development purposes. However, it is an excellent tool for testing the services.
Service A
Create the Content for App A
Follow the given commands to achieve the goal.
$ mkdir appA
$ cd appA
$ cat <<EOF >index.html
this is app A !
EOF
Start the Web Server for App A
In the command window, “&” is mentioned. It allows the whole process to run in the background and you can use the shell for other purposes. For instance, a shell can have prints of STDOUT logs.
For Python 2
$ python -m SimpleHTTPServer 3000 &
$ Serving HTTP on 0.0.0.0 port 3000 ...
For Python 3
$ python3 -m http.server 3000 &
Serving HTTP on 0.0.0.0 port 3000 (http://0.0.0.0:3000/) …
Test App A
The app content is ready to tets via HTTPie-http client.
$ http http://localhost:3000
127.0.0.1 - - [24/May/2020 13:30:59] "GET / HTTP/1.1" 200 - HTTP/1.0 200 OK Content-Length: 16 Content-type: text/html Date: Sun, 24 May 2020 13:30:59 GMT Last-Modified: Sun, 24 May 2020 13:28:42 GMT Server: SimpleHTTP/0.6 Python/2.7.18rc1
this is app A !
It works amazingly. You can observe all content of App A on port 3000.
Service B
Create the Content for App B
$ mkdir appB
$ cd appB
$ cat <<EOF >index.html
this is app B !
EOF
Start the Web Server for App B
Again, in the command window, “&” is mentioned. It allows the whole process to run in the background and you can use the shell for other purposes. For instance, the shell can have prints of STDOUT logs.
For Python 2
$ python -m SimpleHTTPServer 4000 &
$ Serving HTTP on 0.0.0.0 port 4000 ...
For Python 3
$ python3 -m http.server 4000 &
Serving HTTP on 0.0.0.0 port 4000 (http://0.0.0.0:4000/) …
Test App B
Now, you can test your app B via HTTPie http client.
$ http http://localhost:4000
127.0.0.1 – – [24/May/2020 13:39:18] “GET / HTTP/1.1” 200 –
HTTP/1.0 200 OK
Content-Length: 16
Content-type: text/html
Date: Sun, 24 May 2020 13:39:18 GMT
Last-Modified: Sun, 24 May 2020 13:38:23 GMT
Server: SimpleHTTP/0.6 Python/2.7.18rc1
this is app B !
Superb! You can observe all content of App B on port 4000.
How to setup Nginx Reverse Proxy using Local resolution
To differentiate two apps, it is necessary to use two different domains. You can use any fake or supposed domain like domain.com. It can be like the given below:
- For App A:domain.com
- For App B:domain.com
The system will ask for the two entries before the DNS resolution. Here, you need to develop in the /etc/hosts file.
$ sudo cat <<EOF >> /etc/hosts
127.0.0.1 appa.domain.com
127.0.0.1 appb.domain.com
EOF
How to configure NGINX as a Reverse Proxy Server
When you have customized the setup and install python and HTTPie, the system is ready to configure Nginx as a Reverse Proxy. The Host header regulates the type of request as per the webserver. In simpler words, the Nginx configuration determines that a certain type of request needs to pass to a certain web server. Thus, users can smoothly use the Nginx reverse proxies.
Here is the need to delete the default configuration symlink at the enables-sites.
$ rm /etc/nginx/sites-enabled/default
Then create the configuration file on the available under-sites:
$ touch /etc/nginx/sites-available/reverse-proxy
Create the new symlink on the enables-under sites:
$ ln -s /etc/nginx/sites-available/reverse-proxy /etc/nginx/sites-enabled/reverse-proxy
Choose the desired editor (nano or any other editor) and edit the configuration file:
$ nano /etc/nginx/sites-available/reverse-proxy
Here we have fulfilled all requirements to configure the reverse proxy. Nginx server uses and prefers the upstream module. The upstream modules are used for load balancing. But there are other methods also.
Finally, command the given statements into nano or the desired editor and exit using Ctrl-X.
upstream appA { server 127.0.0.1:3000; } upstream appB { server 127.0.0.1:4000; } server { listen 80; server_name appa.domain.com; location / { proxy_pass http://appA; } } server { listen 80; server_name appb.domain.com; location / { proxy_pass http://appB; } }
To sum up the procedure, both upstream features define app A and app B. One is shown on port 3000 and the other one in port 4000. Server directive and domain name are the main configuration titles. The Host header is used to determine the subdomains and upstream mapping.
Test the Nginx Reverse proxy Configuration
To complete the procedure flawlessly, it is better to assess the Nginx Reverse proxy configuration.
In case you do not test the configuration, it may cause issues in load balancing and other functioning of the server. In simpler words, if you have a non-valid configuration, the server may have more downtime after a restart. IT experts advise the Nginx users to tets the reverse proxy configuration to avoid delay time.
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload NGINX Server
First, reload the nginx web server. It will help to show all new configurations.
$ sudo systemctl reload nginx
Check the status of the NGINX Server
You can check the status of the Nginx server using the given commands.
$ sudo systemctl status nginx
-
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2020-05-24 13:16:41 UTC; 3min 16s ago Docs: man:nginx(8) Process: 3525 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS) Main PID: 3331 (nginx) Tasks: 2 (limit: 614) Memory: 4.2M CGroup: /system.slice/nginx.service ├─3331 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─3526 nginx: worker process May 24 13:16:41 5eca7132aa8cc30001a9eb8b systemd[1]: Starting A high performance web server and a reverse proxy server... May 24 13:16:41 5eca7132aa8cc30001a9eb8b systemd[1]: Started A high performance web server and a reverse proxy server. May 24 13:19:38 5eca7132aa8cc30001a9eb8b systemd[1]: Reloading A high performance web server and a reverse proxy server. May 24 13:19:38 5eca7132aa8cc30001a9eb8b systemd[1]: Reloaded A high performance web server and a reverse proxy server.
Verify the setup
It is time to see the results of your struggle. Test the two apps and the validity of the Nginx configuration. For this purpose, the requests will proceed from HTTP client-HTTPie. HTTPie will forward a request to appa.domain.com and appb.domain.com. If the correct web server shows the desired app content, it reveals your success.
App A
Send an HTTP request to host appa.domain.com
You can see the app A content via appa.domain.com.
App B
Send an HTTP request to host appb.domain.com
You can view the app B content via appb.domain.com.
Conclusion
The article has described the Nginx configuration simply. Further, set up of two different apps using two domains as well as how to reload the Nginx proxy server. While describing the promised procedures, we have skipped the performance features and several other settings. You can toggle them to settle well.