{"id":2048,"date":"2017-09-24T13:25:45","date_gmt":"2017-09-24T07:55:45","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=2048"},"modified":"2026-09-08T16:45:36","modified_gmt":"2026-09-08T11:15:36","slug":"setting-different-documentroot-subdirectory-nginx","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/setting-different-documentroot-subdirectory-nginx\/","title":{"rendered":"How to Configure a Different Document Root for a Subdirectory in Nginx"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx is a high-performance web server commonly used to serve websites and web applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In some cases, you may need to configure a <strong>different document root for a specific subdirectory<\/strong> while keeping the main domain&#8217;s document root unchanged.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Main domain document root: <code>\/var\/www\/html<\/code><\/li>\n\n\n\n<li>Subdirectory document root: <code>\/var\/www\/folder<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">With this configuration, the main website can continue using <code>\/var\/www\/html<\/code>, while requests to:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">http:\/\/domain.tld\/subdirectory\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">are served from:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/var\/www\/folder<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This guide explains how to configure this setup in Nginx.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before starting, make sure you have:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>An Nginx web server.<\/li>\n\n\n\n<li>SSH or terminal access to the server.<\/li>\n\n\n\n<li>Sufficient permissions to modify the Nginx configuration.<\/li>\n\n\n\n<li>Basic knowledge of Nginx configuration.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">If Nginx is not installed, refer to the following installation guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-and-configure-php-with-nginx-on-centos7\/\">How to install and configure PHP with Nginx on centos7<\/a><\/strong><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-setup-ssl-certificate-in-nginx-on-ubuntu-20-04\/\"><\/a><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-nginx-and-lets-encrypt-ssl-with-html-docker-ubuntu-20-04\/\">How to Install Nginx and Let\u2019s Encrypt SSL with HTML + Docker \u2013 Ubuntu 20.04<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Identify the Document Roots<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For this example, assume the domain name is:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">domain.tld<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The main domain uses:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/var\/www\/html<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The new directory that will be used for the subdirectory is:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/var\/www\/folder<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The objective is to serve:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">http:\/\/domain.tld\/subdirectory\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">from:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/var\/www\/folder<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Edit the Nginx Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The default Nginx configuration file is:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/etc\/nginx\/sites-enabled\/default<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If your domain uses a different Nginx configuration file, update the corresponding configuration file instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open the configuration file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">vi \/etc\/nginx\/sites-enabled\/default<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following configuration inside the <code>server<\/code> section:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">location \/subdirectory {\n\n    alias \/var\/www\/folder;\n\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>alias<\/code> directive maps the <code>\/subdirectory<\/code> URL path to the <code>\/var\/www\/folder<\/code> directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Verify the Nginx Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before restarting Nginx, verify that the configuration syntax is correct:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nginx -t<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the configuration test is successful, proceed with restarting Nginx.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Restart Nginx<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Restart the Nginx service:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">systemctl restart nginx<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Create Test Content<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create an <code>index.html<\/code> file inside the new document root:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">vi \/var\/www\/folder\/index.html<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add some test content to the file and save it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Test the Subdirectory<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Access the following URL from a browser:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">http:\/\/domain.tld\/subdirectory\/index.html<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The content from:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/var\/www\/folder\/index.html<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">should now be displayed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx can be configured to serve a specific URL subdirectory from a different document root while keeping the main website&#8217;s document root unchanged.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, <code>\/var\/www\/html<\/code> continues to serve the main domain, while <code>\/var\/www\/folder<\/code> serves requests under <code>\/subdirectory<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The key Nginx configuration is:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">location \/subdirectory {\n\n    alias \/var\/www\/folder;\n\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This approach can be useful when different sections of a website need to be served from separate directories while using the same domain.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Can a subdirectory use a different document root in Nginx?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Nginx&#8217;s <code>alias<\/code> directive can map a URL path to a different directory on the server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Where should the <code>location<\/code> block be added?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>location<\/code> block should be added inside the appropriate Nginx <code>server<\/code> block for the domain.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. How can I verify the Nginx configuration before restarting?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Run:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nginx -t<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This checks the Nginx configuration syntax before the service is restarted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-setup-ssl-certificate-in-nginx-on-ubuntu-20-04\/\">How to setup SSL Certificate in Nginx on Ubuntu 20.04<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/allow-cross-origin-regioncors-nginx\/\">How to Enable Cross-Origin Resource Sharing (CORS) in NGINX<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to Our Technology Experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Planning your AWS infrastructure or looking to improve an existing cloud environment? Our team can help with cloud architecture, infrastructure, DevOps, security, deployment, and ongoing optimization.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/pheonixsolutions.com\/contact\">Connect with our technology experts.<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Nginx is a high-performance web server commonly used to serve websites and web applications. In some cases, you may [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"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},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[271,379],"class_list":["post-2048","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-nginx","tag-subdirectory","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-x2","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2048"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2048\/revisions"}],"predecessor-version":[{"id":11548,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2048\/revisions\/11548"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}