8月13日任务12.6-12.9 nginx安装 虚拟主机 用户认证 域名重定向

12.6 Nginx安装
12.7 默认虚拟主机
12.8 Nginx用户认证
12.9 Nginx域名重定向

12.6 Nginx安装

cd /usr/local/src

wget http://nginx.org/download/nginx-1.12.1.tar.gz

tar zxf nginx-1.12.1.tar.gz

./configure --prefix=/usr/local/nginx

make && make install

vim /etc/init.d/nginx //复制如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )

chmod 755 /etc/init.d/nginx

chkconfig --add nginx

chkconfig nginx on

cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak

vim nginx.conf //写入如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)

 

/usr/local/nginx/sbin/nginx -t

/etc/init.d/nginx start

netstat -lntp |grep 80

ca185fa128f123763ef709f6ea2b63df82e.jpg

 

/usr/local/nginx/conf/ngnix.conf

ced2b95e81174c58aca3eaf4273fc951e26.jpg

ade60a079803e11a777572c4699fa02683c.jpg

a9371568ca15858c06aa6ee88f230bf6fb5.jpg

12.7 默认虚拟主机

vim /usr/local/nginx/conf/nginx.conf //增加

include vhost/*.conf

mkdir /usr/local/nginx/conf/vhost

cd !$;

vim default.conf //加入如下内容

server

{

listen 80 default_server;

server_name aaa.com;

index index.html index.htm index.php;

root /data/wwwroot/default;

}

mkdir -p /data/wwwroot/default/

echo “This is a default site.”>/data/wwwroot/default/index.html

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

curl localhost

curl -x127.0.0.1:80 123.com

8e1e7b7d63189851d4cdc26bb36e20ed964.jpg

c5edd99c5cb10a8e1783c86d148b2e1abb6.jpg

server

{

listen 80 default_server; // 有这个标记的就是默认虚拟主机

server_name 111.com;

index index.html index.htm index.php;

root /data/wwwroot/default;

}

12.8 Nginx用户认证

vim /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;

}

}

 

yum install -y httpd

htpasswd -c /usr/local/nginx/conf/htpasswd xqiang

7c040ef55bbb5d90fc92404261f3226d218.jpg

-t && -s reload

 

mkdir /data/wwwroot/test.com

echo “test.com”>/data/wwwroot/test.com/index.html

curl -x127.0.0.1:80 test.com -I//状态码为401说明需要验证

curl -uaming:passwd 访问状态码变为200

编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗

针对目录的用户认证

location /admin/

{

auth_basic "Auth";

auth_basic_user_file /usr/local/nginx/conf/htpasswd;

}

0f1b4aebde253b50fb49c8c1c7871193412.jpg

9e6ee5e4ce44f9f41117d07bfb3e81eda54.jpg

e72c473be1acaa3c9b830d1b363d56996bf.jpg

12.9 Nginx域名重定向

虚拟主机路径:

[root@aming-02 vhost]# pwd

/usr/local/nginx/conf/vhost

 

修改虚拟主机配置文件:

[root@aming-02 vhost]# vim 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;

}

}

6880e963c4ca406040a06838d151a174040.jpg

 

server_name后面支持写多个域名,这里要和httpd的做一个对比

permanent为永久重定向,状态码为301,如果写redirect则为302

检验结果:

 

[root@aming-02 vhost]# curl -x127.0.0.1:80 test1.com/index.html -I

5b9fa96e5fa1bb5948d04a134e113d9c508.jpg

 

 

 

 

 

转载于:https://my.oschina.net/u/3873515/blog/1929179

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值