{"id":721,"date":"2025-02-07T09:14:47","date_gmt":"2025-02-07T09:14:47","guid":{"rendered":"https:\/\/forumweb.hosting\/blog\/?p=721"},"modified":"2025-02-07T09:16:02","modified_gmt":"2025-02-07T09:16:02","slug":"configuring-nginx-as-a-reverse-proxy-for-multiple-websites-on-one-server","status":"publish","type":"post","link":"https:\/\/forumweb.hosting\/blog\/configuring-nginx-as-a-reverse-proxy-for-multiple-websites-on-one-server\/","title":{"rendered":"Configuring NGINX as a Reverse Proxy for Multiple Websites on One Server"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>NGINX is a powerful, high-performance web server often used as a reverse proxy, load balancer, and HTTP cache. Setting up NGINX as a reverse proxy allows you to host multiple websites on a single server efficiently. In this guide, you&#8217;ll learn how to configure NGINX as a reverse proxy for multiple domains, complete with detailed commands and explanations.<\/p>\n<h3>Prerequisites<\/h3>\n<p>Before you start, ensure you have:<\/p>\n<ul>\n<li>An Ubuntu 22.04 server<\/li>\n<li>Root or sudo privileges<\/li>\n<li>Two or more domain names pointed to your server\u2019s IP<\/li>\n<\/ul>\n<h3>Step 1: Install NGINX<\/h3>\n<p>First, update your package list and install NGINX:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo apt update<br \/>\nsudo apt install nginx -y<\/p><\/blockquote>\n<p>Once installed, verify that NGINX is running:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo systemctl status nginx<\/p><\/blockquote>\n<p>If it&#8217;s not running, start and enable the service:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo systemctl start nginx<br \/>\nsudo systemctl enable nginx<\/p><\/blockquote>\n<h3>Step 2: Configure the Firewall<\/h3>\n<p>Allow NGINX traffic through the UFW firewall to enable HTTP and HTTPS access:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo ufw allow &#8216;Nginx Full&#8217;<\/p><\/blockquote>\n<p>Then, verify the firewall status:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo ufw status<\/p><\/blockquote>\n<h3>Step 3: Set Up Directories for Each Website<\/h3>\n<p>Create directories for each website you plan to host:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo mkdir -p \/var\/www\/site1.com\/html<br \/>\nsudo mkdir -p \/var\/www\/site2.com\/html<\/p><\/blockquote>\n<p>Set the correct permissions to allow NGINX to access these files:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo chown -R $USER:$USER \/var\/www\/site1.com\/html<br \/>\nsudo chown -R $USER:$USER \/var\/www\/site2.com\/html<\/p><\/blockquote>\n<p>And assign proper read permissions:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo chmod -R 755 \/var\/www<\/p><\/blockquote>\n<h3>Step 4: Create Sample Web Pages<\/h3>\n<p>For testing, create simple HTML pages for both sites:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>echo &#8216;&lt;h1&gt;Welcome to Site1.com!&lt;\/h1&gt;&#8217; | sudo tee \/var\/www\/site1.com\/html\/index.html<br \/>\necho &#8216;&lt;h1&gt;Welcome to Site2.com!&lt;\/h1&gt;&#8217; | sudo tee \/var\/www\/site2.com\/html\/index.html<\/p><\/blockquote>\n<h3>Step 5: Configure NGINX Server Blocks<\/h3>\n<p>NGINX uses server blocks (similar to Apache&#8217;s virtual hosts) to manage multiple domains. Create configuration files for each domain:<\/p>\n<p><strong>For site1.com:<\/strong><\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo nano \/etc\/nginx\/sites-available\/site1.com<\/p><\/blockquote>\n<p>Add the following configuration:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>server {<br \/>\nlisten 80;<br \/>\nserver_name site1.com www.site1.com;<br \/>\nroot \/var\/www\/site1.com\/html;<br \/>\nindex index.html;<\/p>\n<p>location \/ {<br \/>\ntry_files $uri $uri\/ =404;<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p><strong>For site2.com:<\/strong><\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo nano \/etc\/nginx\/sites-available\/site2.com<\/p><\/blockquote>\n<p>And add:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>server {<br \/>\nlisten 80;<br \/>\nserver_name site2.com www.site2.com;<br \/>\nroot \/var\/www\/site2.com\/html;<br \/>\nindex index.html;<\/p>\n<p>location \/ {<br \/>\ntry_files $uri $uri\/ =404;<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<h3>Step 6: Enable Server Blocks<\/h3>\n<p>Enable both sites by creating symbolic links to the <code>sites-enabled<\/code> directory:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo ln -s \/etc\/nginx\/sites-available\/site1.com \/etc\/nginx\/sites-enabled\/<br \/>\nsudo ln -s \/etc\/nginx\/sites-available\/site2.com \/etc\/nginx\/sites-enabled\/<\/p><\/blockquote>\n<p>Test the NGINX configuration for syntax errors:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo nginx -t<\/p><\/blockquote>\n<p>If the test is successful, reload NGINX to apply the changes:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo systemctl reload nginx<\/p><\/blockquote>\n<h3>Step 7: Test Your Configuration<\/h3>\n<p>Open your browser and visit <code>http:\/\/site1.com<\/code> and <code>http:\/\/site2.com<\/code>. You should see the respective welcome messages.<\/p>\n<h3>Optional: Enabling HTTPS with Let&#8217;s Encrypt<\/h3>\n<p>For better security, enable HTTPS using Certbot:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo apt install certbot python3-certbot-nginx -y<br \/>\nsudo certbot &#8211;nginx -d site1.com -d www.site1.com<br \/>\nsudo certbot &#8211;nginx -d site2.com -d www.site2.com<\/p><\/blockquote>\n<p>Verify automatic renewal:<\/p>\n<blockquote class=\"td_quote_box td_box_left\"><p>sudo certbot renew &#8211;dry-run<\/p><\/blockquote>\n<h3>Conclusion<\/h3>\n<p>Congratulations! You&#8217;ve successfully configured NGINX as a reverse proxy to host multiple websites on a single server. This setup is highly scalable and can be extended to support additional domains as needed. Make sure to regularly update your server and NGINX configuration for optimal security and performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction NGINX is a powerful, high-performance web server often used as a reverse proxy, load balancer, and HTTP cache. Setting up NGINX as a reverse proxy allows you to host multiple websites on a single server efficiently. In this guide, you&#8217;ll learn how to configure NGINX as a reverse proxy for multiple domains, complete with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":723,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,14],"tags":[119,122,62,121,120,75,21],"_links":{"self":[{"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/posts\/721"}],"collection":[{"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":1,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"predecessor-version":[{"id":722,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/posts\/721\/revisions\/722"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/media\/723"}],"wp:attachment":[{"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/forumweb.hosting\/blog\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}