[LNMP]用户认证与域名重定向

本文介绍如何使用Nginx配置基本的HTTP用户认证,并实现多个域名指向同一站点及永久重定向功能。通过具体步骤展示了创建虚拟主机、添加用户认证文件、设置域名重定向等操作。

用户认证

1、创建虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@juispan vhost]# vi /usr/local/nginx/conf/vhost/test.com.conf
server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
     
location  /            ##也可以针对目录或网页做认证
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}

2、创建用户

1
2
3
4
5
[root@juispan vhost]# yum install -y httpd
[root@juispan vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd juispan
New password: 
Re-type new password: 
Adding password for user juispan

3、检查与启动

1
2
3
4
[root@juispan vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@juispan vhost]# /usr/local/nginx/sbin/nginx -s reload

4、测试效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@juispan vhost]# mkdir /data/wwwroot/test.com
[root@juispan vhost]# echo “test.com”>/data/wwwroot/test.com/index.html
[root@juispan vhost]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Tue, 08 Aug 2017 14:32:30 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
 
[root@juispan vhost]#  curl -ujuispan:mypasswd.com -x127.0.0.1:80 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 08 Aug 2017 14:33:17 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Tue, 08 Aug 2017 14:32:28 GMT
Connection: keep-alive
ETag: "5989cb7c-f"
Accept-Ranges: bytes


域名重定向

1、编辑虚拟主机

1
2
3
4
5
6
7
8
9
10
11
[root@juispan vhost]# vi /usr/local/nginx/conf/vhost/test.com.conf
server
{
    listen 80;
    server_name test.com test1.com test2.com;  ##可以指定多个域名
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;  ##永久重定向
    }
}

2、检查与启动

1
2
3
4
[root@juispan vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@juispan vhost]# /usr/local/nginx/sbin/nginx -s reload

3、测试效果

1
2
3
4
5
6
7
8
[root@juispan vhost]# curl -x127.0.0.1:80 test1.com/abc.txt -I
HTTP/1.1 301 Moved Permanently   ##permanent 301,redirect 302
Server: nginx/1.12.1
Date: Tue, 08 Aug 2017 14:41:58 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/abc.txt











本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1955287,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值