{"id":974,"date":"2017-01-06T11:46:44","date_gmt":"2017-01-06T06:16:44","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=974"},"modified":"2026-09-08T09:31:56","modified_gmt":"2026-09-08T04:01:56","slug":"enable-authorizationpassword-protected-page-nginx-ubuntu","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/enable-authorizationpassword-protected-page-nginx-ubuntu\/","title":{"rendered":"Enable Authorization(password protected) page on nginx &#8211; Ubuntu"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In some situations, you may want to restrict access to a specific page or directory on a website. Password protection can prevent unauthorised users from accessing sensitive content and can also help prevent unwanted access by automated users or bots.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This guide explains how to configure <strong>HTTP Basic Authentication<\/strong> on an Nginx web server running on Ubuntu.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If Nginx is not installed on the server, refer to the related Nginx installation article mentioned below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before implementing password protection, make sure you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ubuntu server.<\/li>\n\n\n\n<li>Nginx web server installed.<\/li>\n\n\n\n<li>Root or <code>sudo<\/code> access.<\/li>\n\n\n\n<li>Nginx configuration directory located at <code>\/etc\/nginx<\/code>.<\/li>\n\n\n\n<li>A website or directory that you want to protect.<\/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 Directory to Protect<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, assume that the <code>web<\/code> folder needs to be protected with a username and password.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The directory 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\/html\/web<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If there is only one domain on the server, the 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\">For a domain-specific configuration, edit the appropriate Nginx virtual host configuration file instead.<\/p>\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\">Open the Nginx 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> block:<\/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 \/web\/ {\n    auth_basic \"Restricted Content\";\n    auth_basic_user_file \/var\/www\/html\/web\/.htpasswd;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>auth_basic<\/code> directive enables password authentication, while <code>auth_basic_user_file<\/code> specifies the file containing the authentication credentials.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, the password 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=\"\">\/var\/www\/html\/web\/.htpasswd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can store the <code>.htpasswd<\/code> file in another location if you prefer to keep it outside the document root.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Install htpasswd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>htpasswd<\/code> command is used to create and manage the password file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the command is not available, install the <code>apache2-utils<\/code> package:<\/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=\"\">apt-get install apache2-utils<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Create the Password File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create the password file and add a user:<\/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=\"\">htpasswd -c \/var\/www\/html\/.htpasswd username<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You will be prompted to enter and confirm the password:<\/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=\"\">New password:\nRe-type new password:\nAdding password for user username<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> The original configuration points Nginx to <code>\/var\/www\/html\/web\/.htpasswd<\/code>, while the example <code>htpasswd<\/code> command creates <code>\/var\/www\/html\/.htpasswd<\/code>. Make sure both paths match. For the configuration above, create the file at <code>\/var\/www\/html\/web\/.htpasswd<\/code>, or update <code>auth_basic_user_file<\/code> to the actual file location.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Test the Nginx Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before restarting Nginx, check the configuration for syntax errors:<\/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\">A successful configuration test should return output similar 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=\"\">nginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Restart Nginx<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Restart Nginx to apply the configuration:<\/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 7: Access the Protected Page<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open the following URL in your 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:\/\/IPADDRESS\/web<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The browser will display an authentication prompt requesting the configured username and password.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After providing valid credentials, the protected page will become accessible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx HTTP Basic Authentication provides a simple way to restrict access to specific website directories. By configuring <code>auth_basic<\/code>, creating an <code>.htpasswd<\/code> file, testing the Nginx configuration, and restarting the service, you can password-protect a particular URL or directory on an Ubuntu server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For sensitive applications, use HTTPS so that credentials are not transmitted over an unencrypted HTTP connection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">1. What is Nginx Basic Authentication?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx Basic Authentication is an HTTP authentication mechanism that requires users to provide a username and password before accessing a protected location.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. What is the purpose of the <code>.htpasswd<\/code> file?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>.htpasswd<\/code> file stores the usernames and password hashes used by Nginx to authenticate users.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. How can I check whether my Nginx configuration is valid?<\/h4>\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 for syntax errors before you restart or reload the service.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. Can I protect only one directory instead of the entire website?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. The <code>location<\/code> block can be configured for a specific path, such as:<\/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 \/web\/ {\n    auth_basic \"Restricted Content\";\n    auth_basic_user_file \/var\/www\/html\/web\/.htpasswd;\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5. Is Basic Authentication secure?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Basic Authentication should be used with <strong>HTTPS<\/strong>. Without HTTPS, the credentials are not adequately protected during transmission.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>How to Hide Web Server Information \/ Modify Server Header in Nginx<\/strong> \u2013 Learn how to modify Nginx server header information to reduce unnecessary web server details being exposed.<br><a href=\"https:\/\/pheonixsolutions.com\/blog\/hide-webserver-informationmodify-server-header-nginx\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noreferrer noopener\">Read the article<\/a><\/li>\n\n\n\n<li><strong>Adding Automatic PHP Extension in Nginx<\/strong> \u2013 Learn how to configure Nginx to handle PHP files automatically.<br><a href=\"https:\/\/pheonixsolutions.com\/blog\/adding-automatic-extension-php-nginx\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noreferrer noopener\">Read the article<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/contact\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In some situations, you may want to restrict access to a specific page or directory on a website. Password [&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":[1024,1050],"tags":[261,271,159],"class_list":["post-974","post","type-post","status-publish","format-standard","hentry","category-security","category-ubuntu","tag-linux","tag-nginx","tag-webserver","psol-cat-security"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-fI","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/974","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=974"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/974\/revisions"}],"predecessor-version":[{"id":11497,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/974\/revisions\/11497"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}