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 //复制以下内容php

参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginxhtml

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 //写入以下内容linux

参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.confnginx

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//写入以下内容git

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.confvim

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
相关文章
相关标签/搜索