Httpd Proxypreservehost

Posted on  by 



  1. Under Apache 2.2, no - the ProxyPreserveHost directive is only valid in the server config or virtual host contexts; you'd need the different ProxyPass statements to be in different virtual hosts. In Apache 2.4, yes - the directory context has been added for the directive, so you can now do something such as.
  2. These users could then edit httpd.conf and manage log files and server restarts without needing root privileges. You may also choose to run the back-end hosts in a chroot environment, in a FreeBSD jail, or under other restricted permissions (using SELinux, for example).
  3. ProxyPass will map a remote server location to a local path. In this case, every request done to the root directory will be redirected to localhost at port 8080. Apache HTTP server will be simply mirroring the requests and responses from the client to the remote server.

In our local situation running Apache 2.2, and the primary app requires proxypreservehost (CQ/AEM author) to login, but a partner we proxy to requires their host in the host header. We don't need to do this with regex though, a regular works fine.

Skip to end of metadataGo to start of metadata

One frequently requested feature is to run different virtual hosts under different userids. Unfortunately, due to the basic nature of unix permission handling, this is impossible. (Although it is possible to run CGI scripts under different userids using suexec or cgiwrap.) You can, however, get the same effect by running multiple instances of Apache httpd and using a reverse proxy to bring them all into the same name space.

This same technique can also be useful if you want certain virtual hosts to be run under a non-threaded server (prefork) while others run with threading (worker or event).

Main host

This instance does not actually serve requests, but rather proxies them to other servers running on other ports.

Back-end hosts

These hosts do the real work. They must each be started independently. If they share the same httpd binary (and hence the same mpm), you can start them as follows:

Host 1

Host 2

Combining into one config file

If the virtual (back-end) hosts share many common config elements, it may be easier if all the hosts share a config file. This can be accomplished by wrapping the parts above in <IfDefine hostx> sections, and then using

to start each host.

Running unprivileged back-end hosts

If you use unprivileged ports for the back-end hosts (for example, replacing 81 and 82 above with 8001 and 8002) then you may choose to start these hosts directly under the less-privileged userids (host1user and host2user) in place of root. This will allow you to give complete control of these back-end servers to host1user and host2user. These users could then edit httpd.conf and manage log files and server restarts without needing root privileges. You may also choose to run the back-end hosts in a chroot environment, in a FreeBSD jail, or under other restricted permissions (using SELinux, for example).

When starting httpd under a less-privileged userid, you'll need to adjust certain directives such as PIDFile and CustomLog to point to locations writable by the less-privileged user.

Alternative Proxy Configuration

If back-end hosts might be added and removed frequently, it could be helpful to be able to do this without modifying the configuration of the front-end server. Here is an example config that allows back-end hosts to be added and deleted by editing a simple text database:

To add or delete a user server, simply edit the host_to_ip file, which might look like this:

Because the ProxyPreserveHost directive is used in place of ProxyPassReverse in this configuration, you need to ensure that the back-end hosts use the following config in order to get the correct hostname and port on server-generated redirects:

Apache is a very popular HTTP server and can be configured as a proxy to redirect HTTP traffic similar to nginx. In this guide, we will learn how to set up Apache on CentOS 7 and use it as a reverse-proxy to welcome incoming connections and redirect them to the ASP.NET Core application running on Kestrel. For this purpose, we will use the mod_proxy extension and other related Apache modules.

Prerequisites

  1. A server running CentOS 7, with a standard user account with
    sudo privilege.
  2. An existing ASP.NET Core application.

Publish your application

Run dotnet publish -c Release from your development environment to package your
application into a self-contained directory that can run on your server. The published application must then be copied to the server using SCP, FTP, etc.

Under a production deployment scenario, a continuous integration workflow does the work of publishing the application and copying the assets to the server.

Configure a proxy server

A reverse proxy is a common setup for serving dynamic web applications. The reverse proxy terminates the HTTP request and forwards it to the ASP.NET application.

A proxy server is one which forwards client requests to another server instead of fulfilling them itself. A reverse proxy forwards to a fixed destination, typically on behalf of arbitrary clients. In this guide, Apache is being configured as the reverse-proxy running on the same server that Kestrel is serving the ASP.NET Core application.

These instances could exist on separate physical machines, Docker containers, or a combination of configurations depending on your architectural needs or restrictions.

Install Apache

Installing the Apache web server on CentOS is a single command, but first let's update our packages.

This ensures that all of the installed packages are updated to their latest version. Install Apache using yum

The output should reflect something similar to the following.

In this example the output reflects httpd.86_64 since the CentOS 7 version is 64 bit. The output may be different for your server. To verify where Apache is installed, run whereis httpd from the command line.

Configure Apache for reverse-proxy

Configuration files for Apache are located within the /etc/httpd/conf.d/ directory. Update mac from 10.12 to 10.13. Any file with the .conf extension will be processed in alphabetical order in addition to the module configuration files in /etc/httpd/conf.modules.d/, which contains any configuration files necessary to load modules.

Create a configuration file for your app, for this example we'll call it hellomvc.conf

The VirtualHost node, of which there can be multiple in a file or on a server in many files, is set to listen on any IP address using port 80. ProxyRequests allows or prevents Apache httpd from functioning as a forward proxy server. In a typical reverse proxy or gateway configuration, this option should be set to Off. The next two lines are set to pass all requests received at the root to the machine 127.0.0.1 port 5000 and in reverse. For there to be bi-directional communication, both settings ProxyPass and ProxyPassReverse* are required.

Logging can be configured per VirtualHost using ErrorLog and CustomLog directives. ErrorLog is the location where the server will log errors and CustomLog sets the filename and format of log file. In our case this is where request information will be logged. There will be one line for each request.

Save the file, and test the configuration. If everything passes, the response should be Syntax [OK].

Restart Apache.

Monitoring our application

Apache is now setup to forward requests made to http://localhost:80 on to the ASP.NET Core application running on Kestrel at http://127.0.0.1:5000. However, Apache is not setup to manage the Kestrel process. We will use systemd and create a service file to start and monitor the underlying web app. systemd is an init system that provides many powerful features for starting, stopping and managing processes.

Create the service file

Apache

Create the service definition file

An example service file for our application.

User If apache is not used by your configuration, the user defined here must be created first and given proper ownership for files

Save the file and enable the service.

Httpd.conf proxypreservehost

Start the service and verify that it is running.

With the reverse-proxy configured and Kestrel managed through systemd, the web application is fully configured and can be accessed from a browser on the local machine at http://localhost. Inspecting the response headers, the Server still shows the ASP.NET Core application being served by Kestrel.

Viewing logs

Since the web application using Kestrel is managed using systemd, all events and processes are logged to a centralized journal. However, this journal includes all entries for all services and processes managed by systemd. To view the kestrel-hellomvc.service specific items, use the following command.

For further filtering, time options such as --since today, --until 1 hour ago or a combination of these can reduce the amount of entries returned.

Securing our application

Configure firewall

Firewalld is a dynamic daemon to manage firewall with support for network zones, although you can still use iptables to manage ports and packet filtering; it is not recommended to use them both at the same time. Firewalld should be installed by default, if not use yum to install it.

Using firewalld you can open only the ports needed for the application. In this case, port 80 and 443 are used. The following command permanently sets these to open. Apple software update catalina.

Httpd.conf Proxypreservehost

Reload the firewall settings, and check the available services, ports on the default zone. Options are available by inspecting firewall-cmd -h

SSL configuration

Apache Httpd Proxypreservehost

Httpd Proxypreservehost

To configure Apache for SSL, the mod_ssl module is used. This was installed initially when we installed the httpd module. If it was missed or not installed, use yum to add it to your configuration.

Proxypreservehost

To enforce SSL, install mod_rewrite

The hellomvc.conf file that was created for this example needs to be modified to enable the rewrite as well as adding the new VirtualHost section for HTTPS.

This example is using a locally generated certificate. SSLCertificateFile should be your primary certificate file for your domain name. SSLCertificateKeyFile should be the key file generated when you created the CSR. SSLCertificateChainFile should be the intermediate certificate file (if any) that was supplied by your certificate authority

Save the file, and test the configuration.

Restart Apache.

Additional Apache suggestions

Additional Headers

In order to secure against malicious attacks there are a few headers that should either be modified or added. Ensure that the mod_headers module is installed.

Secure Apache from clickjacking

Clickjacking is a malicious technique to collect an infected user's clicks. Clickjacking tricks the victim (visitor) into clicking on an infected site. Use X-FRAME-OPTIONS to secure your site.

Edit the httpd.conf file.

Add the the line Header append X-FRAME-OPTIONS 'SAMEORIGIN' and save the file, then restart Apache.

MIME-type sniffing

This header prevents Internet Explorer from MIME-sniffing a response away from the declared content-type as the header instructs the browser not to override the response content type. With the nosniff option, if the server says the content is text/html, the browser will render it as text/html.

Edit the httpd.conf file.

Add the the line Header set X-Content-Type-Options 'nosniff' and save the file, then restart Apache.

Load Balancing

This example shows how to setup and configure Apache on CentOS 7 and Kestrel on the same instance machine. However, in order to not have a single point of failure; using mod_proxy_balancer and modifying the VirtualHost would allow for managing mutliple instances of the web applications behind the Apache proxy server.

In the configuration file, an additional instance of the hellomvc app has been setup to run on port 5001 and a the Proxy section has been set with a balaber configuration with two members to load balance byrequests.

Rate Limits

Using mod_ratelimit, which is included in the htttpd module you can limit the amount of bandwidth of clients.

Apache httpd proxypreservehost

The example file limits bandwidth as 600 KB/sec under the root location.





Coments are closed