Linux centosVMware Nginx安装、 默认虚拟主机、Nginx用户认证、Nginx域名重定向

一、 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/

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

/etc/init.d/nginx start netstat -lntp |grep 80

 

配置文件conf,不用系统配置文件

mv nginx.conf nginx.conf.1

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

先拷贝一份

进入网址复制脚本

 

测试是否成功

 

在此文件下面定义过

二、 默认虚拟主机

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

include vhost/*.conf

mkdir /usr/local/nginx/conf/vhost

cd !$

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

include vhost/*.conf;    一定要有;

server

{

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

     server_name aaa.com;

     index index.html index.htm index.php;

     root /data/wwwroot/default;

}

把以前的server注释掉,并添加如上

创建

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

重启

 

或者重新加载

或者

[root@davery default]# /usr/local/nginx/sbin/nginx -s reload

检测一下

 随便访问都默认是者个域名,都指向这个虚拟主机

[root@davery default]# cd /usr/local/nginx/conf/

 

三、Nginx用户认证

vim /usr/local/nginx/conf/vhost/test.com.conf//写入如下内容

[root@davery vhost]# vim aaa.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 davery  执行

cat一下就生成文件了

生成第二个

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

 /usr/local/nginx/sbin/nginx -s reload //测试配置并重新加载

aaa.com就可以,test.com就不能访问

[root@davery vhost]# mkdir /data/wwwroot/test.com
[root@davery vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@davery vhost]# curl -unaming:davery -x127.0.0.1:80 test.com

具体内容访问:centos7.aminglinux.com

四、Nginx域名重定向

更改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;

}

}

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

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

猜你喜欢

转载自www.cnblogs.com/davery/p/8964756.html
今日推荐