Reading Time: 2minutes
If you're migrating a website from Apache Webserver to Nginx to boost performance and better Utilize your servers hardware and the websites (Virtualhosts) has sections with implemented Apache .htaccess / .htaccess password authentication, you will have to migrate also Apache directory password protection to Nginx.
This is not a hard task as NginX's password protection uses same password format as Apache and Nginx password protection files are generated with standard htpasswd part of apache2-utils package (on Debian / Ubuntu servers) and httpd-tools on CentOS / Fedora / RHEL. If you're migrating the Apache websites to Nginx on a fresh new installed server and website developers are missing htpasswd tool to install it depending on Linux distro:
On Debian / Ubuntu deb based servers, install htpasswd with:
apt-get install –yes apache2-utils
…
On CentOS / Fedora … other RPM based servers:
yum -y install httpd-tools
…
Once installed if you need to protect new section site still being in development with password with Nginx, do it as usual with htpasswd
htpasswd -c /home/site/nginx-websitecom/.htpaswd admin
Note that if .htpasswd file has already exist and has other user records, to not overwritted multiple users / passes and let all users in file login to Nginx HTTP auth with separate passwords, do:
htpasswd /var/www/nginx-websietcom/.htpasswd elijah
Now open config file of Nginx Vhost and modify it to include configuration like this:
server {
listen 80;
server_name www.nginx-website.com nginx-website.com;
root /var/www/www.nginx-website.com/www;
[…]
location /test {
auth_basic "Restricted";
auth_basic_user_file /var/www/www.example.com/.htpasswd;
}
[…]
}
Do it for as many Vhosts as you have and to make the new settings take affect restart Nginx:
/etc/init.d/nginx restart
Enjoy 🙂
More helpful Articles

Tags: com, file, htpasswd, HTTP, passwords, protection, servers, var, website, www
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36
One other tool you might want to check is PNGSlim
View CommentView CommentMozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36
On Windows useful tool to optimize images is pngout
View CommentView CommentMozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Nice guide. Thanks for taking the time to write it and its helped me a lot.
View CommentView CommentMozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 YaBrowser/18.1.1.840 Yowser/2.5 Safari/537.36
Please let me know if you’re looking for a writer for
View CommentView Commentyour weblog. You have some really good posts and I
feel I would be a good asset. If you ever want to take some of the load off, I’d really like to write some content for your blog
in exchange for a link back to mine. Please shoot me an email if interested.
Regards!
hire Bodyguards in London recently posted…hire Bodyguards in London