Yuuki

Yuuki's Blog

Hacker. Code Enthusiast.
github
bilibili
twitter

Deploying a test reverse proxy using Apache and Nginx

Overview#

Apache, as a web server, listens on ports 8081 and 8082, providing different web services.

20240416_114107

Nginx acts as a reverse proxy server, determining which server and port to forward requests to based on the domain in the HTTP host header.

20240416_114250

In the following demonstration example, the web server and reverse proxy server will run on the same host, with the domain for the reverse proxy replaced with yuukiz.com. In a real scenario, this can be replaced according to the actual situation.

Operation Steps#

Install Apache and Nginx#

CentOS requires installation of httpd, while Ubuntu systems require installation of apache2.

# RH-based
yum install httpd

# Debian-based
apt-get install apache2

Install Nginx

# RH-based
yum install nginx

# Debian-based
apt-get install nginx

After installing the services, you can disable automatic startup.

systemctl disable httpd
systemctl disable apache2
systemctl disable nginx

Here, I also deleted the default homepage of apache2.

$ cd /var/www/html; mv index.html index.html.bak

I also created a website root directory for virtual hosts.

The local paths for the web servers are /var/www/html/8081 and /var/www/html/8082.

mkdir /var/www/html/8081
mkdir /var/www/html/8082

echo "8081 HTTP Server" > /var/www/html/8081/index.html
echo "8082 HTTP Server" > /var/www/html/8082/index.html

Configure Servers#

If you only need Apache as a web server, when configuring the listening ports, make sure to comment out the original service listening on port 80.

Configure Virtual Hosts in Apache2#

In Apache2, the configuration files are scattered in multiple directories, and configuring virtual hosts requires modifying multiple configuration files.

The configuration files for virtual hosts are usually located in the /etc/apache2/sites-available directory. You can create a new configuration file in this directory or modify an existing one to configure virtual hosts.

The following steps demonstrate creating two new files for configuring virtual hosts.

$ cat >> /etc/apache2/sites-available/8081.conf << EOF
<VirtualHost *:8081>
    DocumentRoot "/var/www/html/8081"
</VirtualHost>
EOF

$ cat >> /etc/apache2/sites-available/8082.conf << EOF
<VirtualHost *:8082>
    DocumentRoot "/var/www/html/8082"
</VirtualHost>
EOF

Next, you need to enable the new virtual hosts using the following command.

$ a2ensite 8081
$ a2ensite 8082

Modify the configuration file to make the Apache2 server listen on ports 8081 and 8082 (while disabling listening on port 80).

# Listen 80
Listen 8081
Listen 8082

After modifying the configuration file, restart the server. Test the server by accessing ports 8081 and 8082 in a browser.

$ systemctl restart apache2

Configure Virtual Hosts in Httpd#

Configuring virtual hosts in Httpd is much simpler than in Apache2.

The configuration file for Httpd is usually located at /etc/httpd/httpd.conf. Open the configuration file and add the following content.

# Port configuration
Listen 8081
Listen 8082

# Virtual host configuration
<VirtualHost *:8081>
    DocumentRoot "/var/www/html/8081"
</VirtualHost>

<VirtualHost *:8082>
    DocumentRoot "/var/www/html/8082"
</VirtualHost>

After modifying the configuration file, restart the server. Test the server by accessing ports 8081 and 8082 in a browser.

20240416_104820

Configure Reverse Proxy in Nginx#

The configuration file for Nginx is usually located at /etc/nginx/nginx.conf.

Add the following content at the appropriate location in the Nginx configuration file.

    server {
        listen 80;
        server_name 8081.yuukiz.com;
        location / {
            proxy_pass http://localhost:8081;
        }
    }

        server {
        listen 80;
        server_name 8082.yuukiz.com;
        location / {
            proxy_pass http://localhost:8082;
        }
    }

In this configuration, requests with the host 8081.yuukiz.com will be forwarded to http://localhost:8081.

Requests with the host 8002.yuukiz.com will be forwarded to http://localhost:8082.

After modifying the configuration file, restart the server.

Test Reverse Proxy#

Test by Manually Modifying the Host Header#

Test by directly accessing the server in a browser.

First, access the server directly, and the Nginx default page will be displayed.

20240416_111512

Use the modHeader extension to set the host in the request header to 8081.yuukiz.com and 8082.yuukiz.com for testing, and the reverse proxy will take effect.

20240416_112639

20240416_112057

Test by Configuring Domain Resolution#

After successfully testing by modifying the host header, go to the domain name provider platform and configure A records for the domain names, pointing both 8081.yuukiz.com and 8082.yuukiz.com to the IP address of the reverse proxy server.

image

Remove the host configuration in the modHeader plugin. After the domain names can be successfully resolved, you can directly access 8081.yuukiz.com and 8082.yuukiz.com to access the local servers through Nginx reverse proxy.

20240416_112824

20240416_112900

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.