|
1 | 1 | *Copyright (C) 2021, Axis Communications AB, Lund, Sweden. All Rights Reserved.* |
2 | 2 |
|
3 | | -# Serve HTTP requests through reverse proxy |
| 3 | +# Web server examples introduction |
4 | 4 |
|
5 | | -This example demonstrates how to setup the Axis device web server (Apache) in a |
6 | | -[Reverse Proxy](https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html) |
7 | | -configuration, where HTTP requests to the application are routed to a web server |
8 | | -[CivetWeb](https://github.com/civetweb/civetweb) running inside the ACAP application |
9 | | -and acting as a CGI. |
| 5 | +This folder contains ACAP Native SDK examples that show two ways to expose HTTP endpoints and web content on an Axis device. |
10 | 6 |
|
11 | | -> [!NOTE] |
12 | | -> The web server runs on the port specified in the application, |
13 | | -> however there is a possibility that the port already is in use. |
14 | | -> For more information on common ports used by Axis devices, see the |
15 | | -> [commonly used network ports](https://help.axis.com/en-us/axis-os-knowledge-base#commonly-used-network-ports) |
16 | | -> in the AXIS OS knowledge base. To change the port of the application, |
17 | | -> both the [`web_server_rev_proxy.c`](./app/web_server_rev_proxy.c) and [`manifest.json`](./app/manifest.json] file have to be updated before |
18 | | -> building the application. |
| 7 | +## Available examples |
19 | 8 |
|
20 | | -The advantage of a webserver proxy is that when porting existing code to your |
21 | | -ACAP application, its request handling can remain largely unmodified. This eases |
22 | | -the task of sharing code between platforms. The webserver proxy method enforces |
23 | | -a URL routing scheme as follows: |
| 9 | +1. **Reverse proxy using fixed port** |
| 10 | + The reverse proxy example using CivetWeb demonstrates how to setup the Axis device web server (Apache) in a Reverse Proxy configuration, where HTTP requests to the application are routed to a web server CivetWeb running inside the ACAP application and acting as a CGI. See [reverse-proxy-using-fixed-port](./reverse-proxy-using-fixed-port/). |
24 | 11 |
|
25 | | -`http://<AXIS_DEVICE_IP>/local/<appName>/<apiPath>` |
| 12 | +2. **HTTP requests using FastCGI** |
| 13 | + The HTTP request example using FastCGI explains how to build an ACAP application that can handle HTTP requests sent to the Axis device. The application uses FastCGI to handle the request and response, and uriparser to parse the received query parameters. See [http-requests-using-fastcgi](./http-requests-using-fastcgi/). |
26 | 14 |
|
27 | | -With `<appName>` and `<apiPath>` as defined in the manifest. |
| 15 | +## Which one to use |
28 | 16 |
|
29 | | -Note that this example shows the reverse proxy concept using CivetWeb, but you are |
30 | | -free to use any webserver of your choice. |
31 | | - |
32 | | -## Alternative approach |
33 | | - |
34 | | -Another example that serves HTTP requests is |
35 | | -[web-server-using-fastcgi](../web-server-using-fastcgi), where the Axis device |
36 | | -web server and the supported ACAP API |
37 | | -[FastCGI](https://developer.axis.com/acap/api/native-sdk-api/#fastcgi) |
38 | | -are used. |
39 | | - |
40 | | -## Reverse proxy configuration in Apache server |
41 | | - |
42 | | -A reverse proxy configuration provides a flexible way for an ACAP application |
43 | | -to expose an external API through the Apache Server in AXIS OS and internally |
44 | | -route the requests to a web server running in the ACAP application. |
45 | | - |
46 | | -The Apache server is configured using the `manifest.json` file in an ACAP |
47 | | -application. In `manifest.json` under `configuration`, it is possible to specify |
48 | | -a `settingPage` and a `reverseProxy` where the latter will connect the CivetWeb |
49 | | -server to the Apache server. |
50 | | - |
51 | | -Prior to manifest 1.5.0, reverse proxy was only supported through the |
52 | | -postinstall script. The manifest based method is more strict on URLs in order to |
53 | | -avoid name clashes that could occur in the old mechanism. When upgrading, your |
54 | | -URLs will change to the format shown in |
55 | | -[Serve HTTP requests through reverse proxy](#serve-http-requests-through-reverse-proxy). |
56 | | - |
57 | | -The web server running in the ACAP application can also be exposed directly to |
58 | | -the network by allowing external access to the port in the network |
59 | | -configuration for the device. There are disadvantages with exposing Web |
60 | | -Server directly to the network such as non standard ports and no reuse of |
61 | | -authentication, TLS and other features that comes with Apache Server. |
62 | | - |
63 | | -## CivetWeb web server |
64 | | - |
65 | | -CivetWeb is an embeddable C web server for Linux. It is a great solution |
66 | | -for running a web server on embedded Linux. Apart from being a |
67 | | -HTTP server, it has a C API which can be extended as desired. The CivetWeb Web |
68 | | -Server [documentation](https://github.com/civetweb/civetweb/) describes the |
69 | | -configuration in detail. CivetWeb is open source, and will contain different |
70 | | -licenses depending on the features you build it with. Please see |
71 | | -[CivetWeb's repository](https://github.com/civetweb/civetweb/) for more information. |
72 | | - |
73 | | -## Getting started |
74 | | - |
75 | | -These instructions will guide you on how to execute the code. Below is the |
76 | | -structure used in the example: |
77 | | - |
78 | | -```sh |
79 | | -web-server |
80 | | -├── app |
81 | | -│ ├── LICENSE |
82 | | -│ └── manifest.json |
83 | | -├── Dockerfile |
84 | | -└── README.md |
85 | | -``` |
86 | | - |
87 | | -- **app/LICENSE** - Lists open source licensed source code in the application. |
88 | | -- **app/manifest.json** - Defines the application and its configuration. |
89 | | -- **Dockerfile** - Builds an Axis container image and the specified example. |
90 | | -- **README.md** - Step by step instructions on how to run the example. |
91 | | - |
92 | | -## Limitations |
93 | | - |
94 | | -- Apache Reverse Proxy can not translate content with absolute addresses (i.e. |
95 | | - /image.png) in the HTML page. Use only relative content (i.e. image.png or |
96 | | -../image.png). See [how to handle relative URLs correctly with a reverse proxy](https://serverfault.com/questions/561892/how-to-handle-relative-urls-correctly-with-a-reverse-proxy) |
97 | | -for more information. |
98 | | - |
99 | | -### How to run the code |
100 | | - |
101 | | -Below is the step by step instructions on how to execute the program. So |
102 | | -basically starting with the generation of the .eap file to running it on a |
103 | | -device. |
104 | | - |
105 | | -#### Build the application |
106 | | - |
107 | | -Standing in your working directory run the following commands: |
108 | | - |
109 | | -> [!NOTE] |
110 | | -> |
111 | | -> Depending on the network your local build machine is connected to, |
112 | | -you may need to add proxy |
113 | | -> settings for Docker. See |
114 | | -> [Proxy in build time](https://developer.axis.com/acap/develop/proxy/#proxy-in-build-time). |
115 | | -
|
116 | | -```sh |
117 | | -docker build --platform=linux/amd64 --tag <APP_IMAGE> --build-arg ARCH=<ARCH> . |
118 | | -``` |
119 | | - |
120 | | -- `<APP_IMAGE>` is the name to tag the image with, e.g., `web-server:1.0` |
121 | | -- `<ARCH>` is the SDK architecture, `armv7hf` or `aarch64`. |
122 | | - |
123 | | -Copy the result from the container image to a local directory `build`: |
124 | | - |
125 | | -```sh |
126 | | -docker cp $(docker create --platform=linux/amd64 <APP_IMAGE>):/opt/app ./build |
127 | | -``` |
128 | | - |
129 | | -The `build` directory contains the build artifacts, where the ACAP application |
130 | | -is found with suffix `.eap`, depending on which SDK architecture that was |
131 | | -chosen, one of these files should be found: |
132 | | - |
133 | | -- `web_server_rev_proxy_1_0_0_aarch64.eap` |
134 | | -- `web_server_rev_proxy_1_0_0_armv7hf.eap` |
135 | | - |
136 | | -> [!NOTE] |
137 | | -> |
138 | | -> For detailed information on how to build, install, and run ACAP applications, refer to the official ACAP documentation: [Build, install, and run](https://developer.axis.com/acap/develop/build-install-run/). |
139 | | -
|
140 | | -#### Install and start the application |
141 | | - |
142 | | -Browse to the application page of the Axis device: |
143 | | - |
144 | | -```sh |
145 | | -http://<AXIS_DEVICE_IP>/index.html#apps |
146 | | -``` |
147 | | - |
148 | | -1. Click on the tab **Apps** in the device GUI |
149 | | -2. Enable **Allow unsigned apps** toggle |
150 | | -3. Click **(+ Add app)** button to upload the application file |
151 | | -4. Select the newly built application package, depending on architecture: |
152 | | - |
153 | | - - `web_server_rev_proxy_1_0_0_aarch64.eap` |
154 | | - - `web_server_rev_proxy_1_0_0_armv7hf.eap` |
155 | | - |
156 | | -5. Click **Install** |
157 | | -6. Run the application by enabling the **Start** switch |
158 | | - |
159 | | -#### The expected output |
160 | | - |
161 | | -A user can make a HTTP request to the application API using e.g. cURL |
162 | | - |
163 | | -```sh |
164 | | -curl -u <USER>:<PASSWORD> --anyauth http://<AXIS_DEVICE_IP>/local/web_server_rev_proxy/my_web_server |
165 | | -``` |
166 | | - |
167 | | -Where the expected output is |
168 | | - |
169 | | -```sh |
170 | | -<html> |
171 | | - <head><link rel="stylesheet" href="style.css"/></head> |
172 | | - <title> |
173 | | - ACAP Web Server Example |
174 | | - </title> |
175 | | - <body> |
176 | | - <h1>ACAP Web Server Example</h1> |
177 | | - Welcome to the web server example, this server is based on the |
178 | | - <a href="https://github.com/civetweb/civetweb">CivetWeb</a> C library. |
179 | | - </body> |
180 | | -</html> |
181 | | -``` |
182 | | - |
183 | | -To view the rendered web page, click on the `Open` button of the application |
184 | | -from the `Apps` tab of the device GUI. Alternatively, browse directly to |
185 | | -`http://<AXIS_DEVICE_IP>/local/web_server_rev_proxy/my_web_server`. |
186 | | - |
187 | | -The application log can be found by either |
188 | | - |
189 | | -- Browse to `http://<AXIS_DEVICE_IP>/axis-cgi/admin/systemlog.cgi?appname=web_server_rev_proxy`. |
190 | | -- Browse to the application page and click the `App log`. |
191 | | - |
192 | | -## License |
193 | | - |
194 | | -**[Apache License 2.0](../LICENSE)** |
| 17 | +- Choose the **reverse proxy** example when you want an embedded web server with flexible routing and easy reuse of existing server-side C code. |
| 18 | +- Choose the **FastCGI** example when you want tighter integration with Apache and a native ACAP web serving flow. |
0 commit comments