Forwarding requests from Apache to Payara Server 5 in Ubuntu
In this article series, we will try to give an overview of the basics of using Payara Server in production using Apache Web Server (httpd) and Ubuntu. Many of the concepts do not rely on the tools we use here, and can be applied to other scenarios.
Our goal is to configure the following:
Here we have an Apache web server that will be available to our users. Their requests will be sent to one of the two copies of the Payara server. Here we will look at using JSESSIONID cookie-based session hooking to ensure that each user interacts with the same Payara server, as well as session replication to ensure that if one Payara server goes down, another will be able to handle existing sessions. no problem.
We assume that the Apache web server is already installed and configured. Our next step is to set up request forwarding to send traffic to Payara Server.
What is forwarding requests?
Forwarding requests (or proxies) is when the server that receives web traffic sends it to another destination. They can be used to hide the user's real location, not to see internal systems or control performance. Like most web servers, the Apache web server can be supplemented with modules that give it the ability to forward requests to the server and send responses through a proxy. This means that you can use Apache as an intermediary, which eliminates the need to specify port numbers or exact IP addresses and allows the user to receive replies from the same server (instead of clicking on localhost and having the address bar labeled localhost : 8080 ).
Configure forward requests between the Apache web server and Payara server
In this post we will look at the package available in the Ubuntu repository. The Ubuntu package contains Apache binaries, but also has additional “helper tools” for managing configuration files. In Ubuntu 18, the two modules that we need to add to Apache are part of the apache2 package that we installed earlier. It can be installed from the repository using:
sudo apt install apache2
Please note that before Ubuntu 16.04 you will need to use “apt-get” instead of “apt”.
We will install two modules to enable basic transfer of http requests - mod_proxy and mod_proxy_http. In Ubuntu, helper tools control the installation of these modules — to make it even easier, tools will search for their dependencies and include them. It is useful that mod_proxy_http requires mod_proxy to work, which means that our team looks like this:
sudo a2enmod proxy_http
Please note that in these commands we can omit the mod_ prefix.
If you want to disable the module, for a2enmod there is an auxiliary tool called a2dismod, which is used in the same way. The tool found a dependency and also included mod_proxy. If you had several modules that you wanted to activate, you can add them after proxy_http, separated by spaces ...
We will restart the server later, as we also need to change the configuration.
The apache2 package in Ubuntu has an example site configuration called “000-default”. The configuration file for this site is located in the / etc / apache2 / sites-available folder and contains a symbolic link to the sites-enabled folder.
Apache configuration file structure
As a rule, you will see that online manuals refer to httpd.conf as a configuration file for editing. This is just the default parent configuration file. On Debian / Ubuntu systems (and their derivatives, such as Linux Mint), the search file is apache2.conf.
This file is read, and its directives are applied from top to bottom; therefore, if two different values are given for the same property, the second will be used. (More precisely, the first will be applied only to read the second parameter).
This file can also specifically "include" files and folders (note that wildcards also work). They will be read and combined with the main configuration in the place where it is written “include”. Thus, the most recent line in the main configuration file (unless it indicates another file) will be the last line of the configuration to be installed, no matter what.
The default page for a new installation for Apache in Ubuntu is in /var/www/index.html and gives a good overview of how this works in detail.
Creating a new site configuration
We will not use the default configuration because we need to add certain behavior. Copy the old configuration from the available sites and rename it to something memorable.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/payara-site.conf
Open a new file in your preferred text editor as root. You should see something like this:
The element contains a series of directives that tell the server how to respond to the listed incoming requests. In this example, <VirtualHost *: 80> handles all responses coming to the server through port 80. For a more detailed description of the VirtualHost tags, see the Apache documentation.
We need to add two lines in the tags to tell Apache how to handle incoming and outgoing traffic — one to tell Apache where to send requests, and another to tell Apache where the responses should come from. Thus, a user visiting localhost can be redirected to our application server at localhost : 8080 /, and responses sent to localhost : 8080 / are redirected instead, so the user sees them coming from localhost .
To manage incoming traffic, add the line:
ProxyPass / http://localhost:8080/
This directive tells Apache to send everything after the domain name of the server to localhost : 8080 /, as if the user went directly to this URL.
ProxyPassReverse / http://localhost:8080/
This directive tells Apache about the reverse URL of the proxy, so that, although the response is returned from the Payara Server instance on port 8080, the URL looks like it was received from Apache. These two directives, when used together, mean that we can avoid complex regular expressions with mod_rewrite.
Assuming that the domain name we want to use, for example, www.example.com , our configuration means that the request to www.example.com/myapp will actually be sent to the Payara server, as if the user had directly connected to localhost : 8080 // MyApp.
Your file should now look like this (I deleted most of the comments to clarify exactly what was needed):
Save your file.
To disable the default site and activate our new configuration, run the following commands:
sudo a2dissite 000-default.conf sudo a2ensite payaraSite.conf
Restart the Apache HTTP Server service (restart, which we missed earlier):
sudo service apache2 restart
To stop and start the Apache HTTP Server, this is:
stop the service
sudo service apache2 stop sudo service apache2 start
Do not forget to start Payara Server:
/opt/payara5/bin/asadmin start-domain domain1
Before making changes to the configuration, logging on to localhost will take you to the Apache welcome screen:
But now we have added to our proxy server and included new directives for our Apache user configuration when we enter localhost / index.html, which we now send to the Payara Server welcome screen:
Now you can add applications to the Payara server.
Thus, we now have an active Apache HTTP server, acting as our proxy (with the possibility of adding additional modules), and a working Payara server, ready to add applications to it.
We went through the installation of both servers, configuring Apache, examining the file system and very briefly reviewed the asadmin commands of the Payara Server.
Next time we will look at how to expand the functionality of Apache and Payara Server, and how we can make Payara Server provide scalability and additional improvements that we will add to Apache to handle several internal servers.