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.2.tar.gz
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2

./configure --prefix=/usr/local/nginx  //这里故意不加其他参数,  以后再编译, 正常要根据自己的实际需求加上需要编译的参数, 如 --with-http_ssl_module 

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

nginx.conf简单解析:

user nobody nobody;  //定义启动nginx的是哪个用户
worker_processes 2;  // 定义子进程有几个
error_log /usr/local/nginx/logs/nginx_error.log crit;  //错误日志
pid /usr/local/nginx/logs/nginx.pid;   //定义pid
worker_rlimit_nofile 51200; //nginx 最多可以打开多少个文件
use epoll;    //使用epoll模式
worker_connections 6000;  //进程最大连接数

每个server对应着1个虚拟主机



如果想php监听的是ip 则写成:

/usr/local/nginx/sbin/nginx -t
/etc/init.d/nginx  start
netstat -lntp | grep 80


测试php解析
vim /usr/local/nginx/html/1.php //加入如下内容
<?php
    echo "test php scripts.";
?>

curl localhost/1.php

12.7 默认虚拟主机

去掉 usr/local/nginx/conf/nginx.conf 中 的内容
Server 
{
.....
}

vim /usr/local/nginx/conf/nginx.conf //增加
include vhost/*.conf;   //注意;号别漏, 在conf目录下加个vhost目录

mkdir /usr/local/nginx/conf/vhost
cd !$
vim default.conf //加入如下内容

server
{
    listen 80 default_server;  // 有这个标记的就是默认虚拟主机
    server_name 120.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 120.com

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 aming      -c 创建 -m md5加密, 第二次用不用 -c创建了

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 -x127.0.0.1:80 test.com -I 访问状态码变为200      passwd改为自己的aming的密码 

-t &&  -s reload //测试配置并重新加载 

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

针对目录的用户认证
location  /admin/
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}

mkdir /data/wwwroot/test.com/admin
echo "admin in test.com">/data/wwwroot/test.com/admin/index.html
-t &&  -s reload //测试配置并重新加载

针对单个文件
location  ~ admin.php    // ~(.*)admin.php$ 更全面匹配 包含 admin php结尾的文件

12.9 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;
    }
}

permanent  301的意思, 想改成302用 redirect

server_name后面支持写多个域名,这里要和httpd的做一个对比
permanent为永久重定向,状态码为301,如果写redirect则为302

测试
curl -x127.0.0.1:80 test1.com/1.php -i

转载于:https://my.oschina.net/u/3746773/blog/1633579

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值