How to Install and set up Nginx as a proxy OpenSUSE Leap 15.3

Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. It started out as a web server designed for maximum performance and stability. Nginx has grown in popularity since its release due to its light-weight resource utilization and its ability to scale easily on minimal hardware. Nginx excels at serving static content quickly and is designed to pass dynamic requests off to other software that is better suited for those purposes.

In this guide we are going to learn how to install Nginx in OpenSUSE Leap 15.3 and set it up as a proxy server. We will set up an Nginx virtual host that proxies a request from a domain to a service listening on a port.

# Table of Content

  1. Ensuring that the server is up to date
  2. Installing Nginx on OpenSUSE Leap 15.3
  3. Starting and enabling Nginx service in OpenSUSE
  4. Setting up the proxy service
  5. Creating Nginx Virtual host to proxy the request
  6. Testing the configuration

# 1. Ensuring that the server is up to date

Before proceeding, it is always a good practice to ensure that the server is up to date. Use these commands to achieve this

sudo zypper refresh
sudo zypper update -y

# 2. Installing Nginx on OpenSUSE Leap 15.3

Nginx, as a stable high-performance web server and with a very low consumption of resources. Nginx has an asynchronous architecture that is much more scalable, based on events.

Nginx is available in the default UpenSUSE repos. Install it using this command:

sudo zypper install -y nginx

Confirm the installed nginx

~> zypper info nginx
Loading repository data...
Reading installed packages...


Information for package nginx:
------------------------------
Repository     : Main Repository
Name           : nginx
Version        : 1.19.8-3.6.1
Arch           : x86_64
Vendor         : SUSE LLC <https://www.suse.com/>
Installed Size : 2.2 MiB
Installed      : Yes
Status         : up-to-date
Source package : nginx-1.19.8-3.6.1.src
Summary        : A HTTP server and IMAP/POP3 proxy server
Description    :
    nginx [engine x] is a HTTP server and IMAP/POP3 proxy server written by Igor Sysoev.
    It has been running on many heavily loaded Russian sites for more than two years.

# 3. Starting and enabling nginx service

The installed Nginx service is not started by default. To start the service, use this command:

sudo systemctl start nginx

Confirm that the service is up and running using this command:

~> sudo systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
     Active: active (running) since Mon 2021-12-20 18:56:15 UTC; 36s ago
    Process: 31740 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
   Main PID: 31741 (nginx)
      Tasks: 2 (limit: 4587)
     CGroup: /system.slice/nginx.service
             ├─31741 nginx: master process /usr/sbin/nginx -g daemon off;
             └─31742 nginx: worker process

Dec 20 18:56:15 ip-10-2-40-44 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Dec 20 18:56:15 ip-10-2-40-44 nginx[31740]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 20 18:56:15 ip-10-2-40-44 nginx[31740]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 20 18:56:15 ip-10-2-40-44 systemd[1]: Started The nginx HTTP and reverse proxy server.

To enable the service on boot, use this command:

sudo systemctl enable nginx

# 4. Setting up Service to proxy

In this example, we will use a simple service that I have already built. I have a golang service here but feel free to use your service.

➜ /tmp/gosimple
2021/12/20 23:38:34 service running, listening on 3000

Test the service

➜ curl -iL http://127.0.0.1:3000/
HTTP/1.1 200 OK
Date: Mon, 20 Dec 2021 20:39:01 GMT
Content-Length: 12
Content-Type: text/plain; charset=utf-8

Hello World!

# 5. Creating Nginx virtual host to proxy the request

Nginx allows you to serve multiple sites using virtual hosts. The concept of virtual hosts allows more than one Web site on one system or Web server. The servers are differentiated by their host name. Visitors to the Web site are routed by host name or IP address to the correct virtual host. Virtual hosting allows companies sharing one server to each have their own domain names.

An Nginx server blocks  can be used to encapsulate configuration details and host more than one domain on a single server. We are going to create a virtual host for our site (site1.citizix.com).

Nginx virtual hosts configurations are stored in the directory /etc/nginx/conf.d/. Lets create a config for our site:

sudo vim /etc/nginx/conf.d/site1.citizix.com.conf

Add the following content

server {
    listen 80;
    server_tokens off;
    client_max_body_size 100M;
    server_name site1.citizix.com;

    access_log /var/log/nginx/site1.citizix.com.log;
    error_log  /var/log/nginx/site1.citizix.com.log;

    root /opt/simpleapp;

    if ($host !~* ^(site1.citizix.com)$) {
        return 444;
    }

    location / {
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_pass http://127.0.0.1:3000;
    }
}

Let us restart the nginx for the configuration to apply:

sudo systemctl restart nginx

# 6. Testing the configuration

In this example, we will use the domain site1.citizix.com for the virtual host but be free to use the domain of your choice. Please note that the DNS for the domain to be used should already be configured to point to the IP address of the server. Confirm that with:

dig -t A site1.citizix.com

If you haven’t yet configured your DNS settings or simply do not want the site to go live yet, you can modify your computer’s hosts file. To do this, use the following command in your local computer’s CLI

sudo vim /etc/hosts

Then, add the IP address of your actual server followed by the domain name you are configuring, for example:

10.2.11.11 site1.citizix.com

With that set up we can make a request to our domain. For me it works fine!

➜ curl -iL http://site1.citizix.com
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 20 Dec 2021 20:51:42 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 12
Connection: keep-alive

Hello World!

# Wrapping up

We have managed to install and set up Nginx to act as a proxy in this guide!

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy