php nginx 域名重定向,Nginx默认虚拟主机、用户认证、域名重定向

Nginx默认虚拟主机定义默认虚拟主机配置文件,在http下面加入include vhost/*.conf

a31f7cefca052443749a7d8f99e398ac.png

在/usr/local/nginx/conf/下创建目录

#mkdir vhost/ //创建vhost目录

#cd vhost/ //进入目录

#vim aaa.com.conf //编辑文件

server

{

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

server_name aaa.com;

index index.html index.htm index.php;

root /data/wwwroot/default;

}

#mkdir /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 //重新加载nginx

#curl -x127.0.0.1:80 aaa.com //测试

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; //密码所在的位置文件

}

}

htpasswd是apache的一个工具,该工具主要用于建立和更新存储用户名、密码的文本文件,主要用于对基于http用户的认证。htpasswd的安装很简单,它是随apache的安装而生成。

#yum install -y httpd //下载httpd包

#htpasswd -c /usr/local/nginx/conf/htpasswd zenwen //生成密码文件

#/usr/local/nginx/sbin/nginx -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说明需要验证

c2139447444909875daadd98981798c9.png

#curl -uzenwen:passwd -x127.0.0.1:80 test.com -I // 访问状态码变为200

40b1aaa62ac23c0391e05e8c48d5add7.png

针对目录的用户认证

当访问admin时,只需要在location /加上admin目录就可以对admin进行用户的验证

location /admin/

{

auth_basic "Auth";

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

}

nginx域名重定向

更改test.com.conf

server{

listen 80;

server_name test.comtest1.comtest2.com;

index index.html index.htm index.php;

root /data/wwwroot/test.com;

if ($host != 'test.com' ) {

rewrite ^/(.*)$ http://test.com/$1 permanent;

}

}

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

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

#/usr/local/nginx/sbin/nginx -t //检测语法

#/usr/local/nginx/sbin/nginx -s reload //重新加载

#curl -x127.0.0.1:80 test2.com/index.html -I //测试

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.1

Date: Wed, 03 Jan 2018 02:45:07 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http://test.com/index.html //直接重定向到了test.com

#curl -x127.0.0.1:80 test1.com/index.html -I //显示状态码为301

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.1

Date: Wed, 03 Jan 2018 06:53:13 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http://test.com/index.html

下面关于Nginx的文章您也可能喜欢,不妨参考下:

Nginx 的详细介绍:请点这里

Nginx 的下载地址:请点这里

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值