Guide to Porting and Configuring Nginx on OKMX6ULx Embedded Linux (Kernel 4.1.15)

Nginx (engine x) is an open-source, high-performance Web server, reverse proxy server, load balancer, and HTTP cache server. It is characterized by low memory usage and strong concurrency ability. In fact, Nginx performs well in terms of concurrency among web servers of the same type. It is developed using the C programming language.

Nginx is specifically developed for performance optimization. Performance is an important consideration in its design. It focuses highly on efficiency in implementation and can withstand high-load tests, supporting up to 50,000 concurrent connections.

1. Compilation and Porting

1.1 Compile nginx-1.8 in Yocto

Execute the following commands for compilation:

$ DISTRO=fsl-imx-x11 MACHINE=imx6ull14x14evk source fsl-setup-release.sh -b build_x11
$ bitbake nginx

During the compilation process, the following errors appeared:

Yocto compilation error for Nginx package

Solution:

Modify the build_x11/conf/bblayers.conf file and add the source code path of Nginx to the file. build_x11 is the installation and compilation path defined by yourself.

Modify the content as follows:

Modifying bblayers.conf to include Nginx source path

After adding, execute bitbake nginx again for compilation.

1.2 Package the Image

After compilation, go to the

build_x11/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/nginx/1.8.1-r0/image
path to package the image.


$ cd tmp/work/cortexa7hf-neon-poky-linux-gnueabi/nginx/1.8.1-r0/image
$ tar -cjvf nginx-1.8.1.tar.bz2 *

1.3 Transplant

Extract the image packaged in the previous step to the root path of the system.

tar -xvf nginx-1.8.1.tar.bz2 -C /

2. Test

2.1 Reverse Proxy

2.1.1 Tomcat Installation

① Install the JDK environment

Download address: Oracle JDK 8 Downloads

Download the following two files:

jdk-8u151-linux-arm32-vfp-hflt.tar.gz
and
jdk-8u151-linux-arm32-vfp-hflt-demos.tar.gz


Extract them to the development board:

Extract the above two compressed packages to /home/root/jdk1.8.0_151.

Modify the environment variables:

Add the following content at the end of the /etc/profile file:

JAVA_HOME=/home/root/jdk1.8.0_151
CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH

Execute the following command to make the environment variables take effect immediately:

$ source /etc/profile

Verify whether the installation is successful:

Enter the following command to check the Java version:

$ java -version
Java version verification output on ARM board

② Install Tomcat

Download the source code: Tomcat 9.0 Downloads. Here, download version 9.0.108.

Extract the downloaded

apache-tomcat-9.0.108.tar.gz
to the
/home/root
path on the development board.


tar -xvf apache-tomcat-9.0.108.tar.gz -C /home/root

Start the Tomcat service:

cd /home/root/apache-tomcat-9.0.108/bin
./startup.sh

After starting, enter 192.168.1.13:8080 in the browser, and the Tomcat interface will be displayed.

Tomcat default welcome page

2.1.2 Modify Nginx Configuration

Modify the /etc/nginx/nginx.conf file:

server {
    listen 80;
    server_name www.123.com;
    
    location / {
        proxy_pass http://192.168.1.13:8080/;
        index index.html index.htm;
    }
}
  • listen: It indicates that the port 80 is monitored.

  • server_name: The access domain name is defined here.

  • proxy_pass: It is a proxy forwarding module. Its main function is to forward www.123.com requests to http://192.168.1.13:8080/.

Start the Nginx service:

mkdir /run/nginx
nginx -c /etc/nginx/nginx.conf

Modify the hosts file in Windows:

Press Win + S, run Notepad as an administrator, open the hosts file under C:\Windows\System32\drivers\etc, and add the mapping.

Test:

Enter www.123.com in the browser, and you can access the Tomcat interface via Nginx.

Accessing Tomcat via Nginx Reverse Proxy

2.2 Load Balancing

1. Setup Service 8080:

Rename the above-mentioned apache-tomcat to apache-tomcat8080, and create a test file:

mv apache-tomcat-9.0.108/ apache-tomcat8080/
cd apache-tomcat8080/webapps
mkdir test
vi test.html
<!--apache8080-->
<html>
<body>welcome to service 8080</body>
</html>

2. Setup Service 8081:

Extract another Tomcat package and rename it as apache-tomcat8081. Modify its server.xml port settings:

mv apache-tomcat-9.0.108/ apache-tomcat8081/
cd apache-tomcat8081/conf
vi server.xml
Configuring server.xml for second Tomcat instance Setting port 8081 in Tomcat configuration

Create the test page for 8081:

<!--apache8081-->
<html>
<body>welcome to service 8081</body>
</html>

3. Modify Nginx for Load Balancing:

Add the upstream block to nginx.conf:

upstream myserver {
    server 192.168.1.13:8080;
    server 192.168.1.13:8081;
}
server {
    listen 80;
    server_name www.123.com;
    location / {
        proxy_pass http://myserver;
        index index.html index.htm;
    }
}

Result:

Restart the nginx service. When accessing www.123.com/test/test.html and opening multiple web pages, you will find that the page content alternates between the services on ports 8080 and 8081. This realizes load balancing.

Load balancing result showing service 8080 Load balancing result showing service 8081


Dear friends, we have created an exclusive embedded technical exchange group on Facebook, where our experts share the latest technological trends and practical skills. Join us and grow together!