{"id":2555,"date":"2022-05-15T00:53:44","date_gmt":"2025-03-31T08:03:40","guid":{"rendered":""},"modified":"2025-02-02T00:46:18","modified_gmt":"2025-02-01T23:46:18","slug":"how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-centos-8","status":"publish","type":"post","link":"https:\/\/netcloud24.com\/knowledgebase\/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-centos-8\/","title":{"rendered":"How to Configure Nginx as a Web Server and Reverse Proxy for Apache on CentOS 8"},"content":{"rendered":"<p>\u00a0<\/p>\n<\/p>\n<header>\n<h1>\u00a0<\/h1>\n<\/header>\n<article>\n<p>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\u2019s 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 , similar configurations can be applied for seamless operation between Nginx and Apache.<\/p>\n<h2>Prerequisites<\/h2>\n<p>Before you begin, make sure you have the following:<\/p>\n<ul>\n<li>A CentOS 8 server with root or sudo access.<\/li>\n<li>Nginx and Apache installed on your server.<\/li>\n<li>A basic understanding of web server configuration.<\/li>\n<\/ul>\n<p>If you are using , you can create a similar environment using Nginx and Apache on Windows-based virtual servers.<\/p>\n<h2>Step 1: Install Nginx and Apache<\/h2>\n<p>Start by installing Nginx and Apache on your CentOS 8 system. Use the following commands to install both web servers:<\/p>\n<pre><code>sudo dnf install nginx httpd -y<\/code><\/pre>\n<p>Once installed, start and enable both services:<\/p>\n<pre><code>sudo systemctl start nginx\r\nsudo systemctl enable nginx\r\n\r\nsudo systemctl start httpd\r\nsudo systemctl enable httpd<\/code><\/pre>\n<h2>Step 2: Configure Apache to Run on a Different Port<\/h2>\n<p>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.<\/p>\n<p>Edit the Apache configuration file:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf<\/code><\/pre>\n<p>Find the line that says <code>Listen 80<\/code> and change it to:<\/p>\n<pre><code>Listen 8080<\/code><\/pre>\n<p>Save and exit the file, then restart Apache to apply the changes:<\/p>\n<pre><code>sudo systemctl restart httpd<\/code><\/pre>\n<h2>Step 3: Configure Nginx as a Reverse Proxy<\/h2>\n<p>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:<\/p>\n<pre><code>sudo nano \/etc\/nginx\/conf.d\/mywebsite.conf<\/code><\/pre>\n<p>Add the following configuration:<\/p>\n<pre><code>server {\r\n    listen 80;\r\n    server_name yourdomain.com;\r\n\r\n    location \/ {\r\n        proxy_pass http:\/\/127.0.0.1:8080;\r\n        proxy_set_header Host $host;\r\n        proxy_set_header X-Real-IP $remote_addr;\r\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\n        proxy_set_header X-Forwarded-Proto $scheme;\r\n    }\r\n}<\/code><\/pre>\n<p>Replace <code>yourdomain.com<\/code> 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.<\/p>\n<p>Save and exit the file, then test the Nginx configuration to ensure everything is correct:<\/p>\n<pre><code>sudo nginx -t<\/code><\/pre>\n<p>If there are no errors, reload Nginx to apply the changes:<\/p>\n<pre><code>sudo systemctl reload nginx<\/code><\/pre>\n<h2>Step 4: Adjust Firewall Settings<\/h2>\n<p>To ensure your server is accessible, allow traffic on both HTTP (port 80) and HTTPS (port 443) if you plan to add SSL later:<\/p>\n<pre><code>sudo firewall-cmd --permanent --add-service=http\r\nsudo firewall-cmd --permanent --add-service=https\r\nsudo firewall-cmd --reload<\/code><\/pre>\n<h2>Step 5: Test the Setup<\/h2>\n<p>At this point, Nginx should be serving as a reverse proxy for Apache. Open your browser and navigate to <code>http:\/\/yourdomain.com<\/code>. You should see content served by Apache through Nginx.<\/p>\n<h2>Step 6: (Optional) Secure Your Site with Let&#8217;s Encrypt SSL<\/h2>\n<p>If you want to secure your site with SSL, you can use Let&#8217;s Encrypt to obtain a free SSL certificate. First, install Certbot:<\/p>\n<pre><code>sudo dnf install certbot python3-certbot-nginx -y<\/code><\/pre>\n<p>Then, obtain the SSL certificate and configure Nginx to use it:<\/p>\n<pre><code>sudo certbot --nginx -d yourdomain.com<\/code><\/pre>\n<p>Follow the prompts to complete the SSL setup, and Certbot will automatically configure Nginx to use HTTPS.<\/p>\n<h2>Benefits of Hosting on a VPS<\/h2>\n<p>Hosting Nginx as a reverse proxy for Apache on a  or a <a href=\"https:\/\/ie.netcloud24.com\">virtual private server hosting Windows<\/a> allows you to leverage the performance benefits of Nginx while still using Apache to handle complex dynamic content. Whether you&#8217;re using a <a href=\"https:\/\/ie.netcloud24.com\">UK Windows VPS<\/a> or a <a href=\"https:\/\/ie.netcloud24.com\">Microsoft SQL VPS Windows<\/a>, you get the flexibility and performance required for high-traffic websites.<\/p>\n<p>Using <a href=\"https:\/\/ie.netcloud24.com\">Windows VPS<\/a> provides even more resources and control, making it ideal for more complex setups involving multiple web servers.<\/p>\n<h2>Conclusion<\/h2>\n<p>Configuring Nginx as a reverse proxy for Apache on CentOS 8 is a powerful way to improve your website\u2019s performance by taking advantage of Nginx\u2019s 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 <a href=\"https:\/\/ie.netcloud24.com\">Windows VPS Hosting UK<\/a> to ensure your web server operates at peak performance with dedicated resources.<\/p>\n<\/article>\n<footer>\n<p>\u00a9 2024 VPS Hosting Solutions<\/p>\n<\/footer>\n<div class=\"post-author-box\" style=\"border-top:1px solid #ddd;margin-top:20px;padding-top:15px;\">\n<p><strong>Author:<\/strong> \u0141ukasz Bodziony<\/p>\n<p><strong>Website:<\/strong> <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a><\/p>\n<p><em>\u0141ukasz Bodziony is the CEO and founder of <a href=\"https:\/\/netcloud24.com\" target=\"_blank\" rel=\"dofollow\">NETCLOUD24<\/a>, a global VPS hosting brand proudly originating from Poland. With extensive experience in cloud computing, virtualization, and server management, he delivers high-performance <strong>Windows VPS<\/strong> and <strong>Remote Desktop Services (RDS)<\/strong> solutions to clients across Europe, North America, and beyond.<\/em><\/p>\n<p><em>His expertise covers a wide range of technologies, including <strong>Microsoft Azure<\/strong>, <strong>Proxmox VE<\/strong>, <strong>Amazon Web Services (AWS)<\/strong>, and numerous other virtualization and cloud platforms.<\/em><\/p>\n<p><em>Beyond running his hosting business, \u0141ukasz also provides <strong>professional paid server configuration and optimization services<\/strong> for companies and individuals. Outside of work, he is dedicated to caring for his children and building a secure future for them.<\/em><\/p>\n<p><em>If you are interested in working with him or need expert assistance with your hosting, cloud environment, or server setup, feel free to reach out via <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a>.<\/em><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 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\u2019s\u2026<\/p>\n","protected":false},"author":1,"featured_media":3421,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[],"tags":[14,12,11,23,20,21,22,17,7,8,6,10,18,19,15,24,16,5,13,9],"class_list":["post-2555","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","tag-cheapvps","tag-cloudvps","tag-hostingvps","tag-rds","tag-rdscal","tag-remotedesktop","tag-remotedesktopvps","tag-servervps","tag-ukvps","tag-virtualserver","tag-vpshosting","tag-vpsserver","tag-vpssolutions","tag-vpswindows","tag-vpswithwindows","tag-windowsrds","tag-windowsserver","tag-windowsvps","tag-windowsvpshosting","tag-windowsvpsuk"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/2555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=2555"}],"version-history":[{"count":0,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/2555\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media\/3421"}],"wp:attachment":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=2555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=2555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=2555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}