Nginx is a highly efficient web server that can also function as a reverse proxy. Combining Nginx and Apache allows you to take advantage of Nginx’s fast static file serving capabilities and use Apache for handling dynamic content. In this guide, we will show you how to configure Nginx as both a web server and reverse proxy for Apache on CentOS 8. This setup can be used on both Linux and Windows-based servers. If you are using a Windows VPS UK, similar configurations can be applied for seamless operation between Nginx and Apache.

Prerequisites

Before you begin, make sure you have the following:

  • A CentOS 8 server with root or sudo access.
  • Nginx and Apache installed on your server.
  • A basic understanding of web server configuration.

If you are using VPS UK Windows, you can create a similar environment using Nginx and Apache on Windows-based virtual servers.

Step 1: Install Nginx and Apache

Start by installing Nginx and Apache on your CentOS 8 system. Use the following commands to install both web servers:

sudo dnf install nginx httpd -y

Once installed, start and enable both services:

sudo systemctl start nginx
sudo systemctl enable nginx

sudo systemctl start httpd
sudo systemctl enable httpd

Step 2: Configure Apache to Run on a Different Port

By default, both Nginx and Apache listen on port 80. To avoid conflicts, we need to configure Apache to run on a different port, such as 8080.

Edit the Apache configuration file:

sudo nano /etc/httpd/conf/httpd.conf

Find the line that says Listen 80 and change it to:

Listen 8080

Save and exit the file, then restart Apache to apply the changes:

sudo systemctl restart httpd

Step 3: Configure Nginx as a Reverse Proxy

Now, configure Nginx to act as a reverse proxy and forward requests to Apache, which will be listening on port 8080. Create a new Nginx server block file:

sudo nano /etc/nginx/conf.d/mywebsite.conf

Add the following configuration:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Replace yourdomain.com with your actual domain name or IP address. This configuration tells Nginx to forward all incoming requests on port 80 to Apache running on port 8080.

Save and exit the file, then test the Nginx configuration to ensure everything is correct:

sudo nginx -t

If there are no errors, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 4: Adjust Firewall Settings

To ensure your server is accessible, allow traffic on both HTTP (port 80) and HTTPS (port 443) if you plan to add SSL later:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Step 5: Test the Setup

At this point, Nginx should be serving as a reverse proxy for Apache. Open your browser and navigate to http://yourdomain.com. You should see content served by Apache through Nginx.

Step 6: (Optional) Secure Your Site with Let's Encrypt SSL

If you want to secure your site with SSL, you can use Let's Encrypt to obtain a free SSL certificate. First, install Certbot:

sudo dnf install certbot python3-certbot-nginx -y

Then, obtain the SSL certificate and configure Nginx to use it:

sudo certbot --nginx -d yourdomain.com

Follow the prompts to complete the SSL setup, and Certbot will automatically configure Nginx to use HTTPS.

Benefits of Hosting on a VPS

Hosting Nginx as a reverse proxy for Apache on a Windows VPS or a virtual private server hosting Windows allows you to leverage the performance benefits of Nginx while still using Apache to handle complex dynamic content. Whether you're using a UK Windows VPS or a Windows VPS Italy, you get the flexibility and performance required for high-traffic websites.

Using Windows Virtual Dedicated Server Hosting provides even more resources and control, making it ideal for more complex setups involving multiple web servers.

Conclusion

Configuring Nginx as a reverse proxy for Apache on CentOS 8 is a powerful way to improve your website’s performance by taking advantage of Nginx’s ability to efficiently serve static content while allowing Apache to handle dynamic content. By following this guide, you can set up a robust and efficient web server infrastructure. For reliable hosting, consider using Windows VPS Hosting UK to ensure your web server operates at peak performance with dedicated resources.

© 2024 VPS Hosting Solutions

Was this answer helpful? 0 Users Found This Useful (0 Votes)