Nginx web service access authentication
This environment is based on the Centos 7.8 system to build the Nginx learning environment. For
specific construction, please refer to Nginx-1.18.0 Environment Deployment
Nginx rewrite is the same as Apache and other web service software, the main function of Nginx rewrite is to realize URL address rewriting. Nginx's rewrite rules need the support of PCRE software, that is, rule matching is performed through Perl compatible regular expression grammar.
1. Access control based on IP address
[root@node01 ~]# vim /etc/nginx/conf.d/host.conf
server {
listen 192.168.5.11:80;
server_name bbs.123.cn;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
deny 192.168.5.12;
allow 192.168.5.0/24;
deny all;
}
}
[root@node01 ~]# nginx -s reload
test
Physical machine
rhel7
node02
Two, based on user access control
[root@node01 ~]# yum install httpd-tools -y
[root@node01 ~]# htpasswd -c /etc/passwd wan
User Xiaoming failed to access
wan user access successfully
Three, based on file access control
[root@node01 ~]# vim /etc/nginx/conf.d/host.conf
server {
listen 192.168.5.11:80;
server_name bbs.123.cn;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
location ~ \.txt{
deny all;
}
}
}
[root@node01 ~]# nginx -s reload
test