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


    [root@lizhipenglinux01 php-5.6.30]# cd /usr/local/src
    [root@lizhipenglinux01 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz

    [root@lizhipenglinux01 src]# tar zxf nginx-1.12.1.tar.gz

    [root@lizhipenglinux01 src]# cd nginx-1.12.1/
    [root@lizhipenglinux01 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx

    [root@lizhipenglinux01 nginx-1.12.1]# make

    [root@lizhipenglinux01 nginx-1.12.1]# make install

    [root@lizhipenglinux01 nginx-1.12.1]# ls /usr/local/nginx/      conf配置文件
    conf html logs sbin

    [root@lizhipenglinux01 nginx-1.12.1]# vim /etc/init.d/nginx

    #!/bin/bash
    # chkconfig: - 30 21
    # description: http service.
    # Source Function Library
    . /etc/init.d/functions
    # Nginx Settings
     
    NGINX_SBIN="/usr/local/nginx/sbin/nginx"
    NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
    NGINX_PID="/usr/local/nginx/logs/nginx.pid"
    RETVAL=0
    prog="Nginx"
     
    start()
    {
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
    }
     
    stop()
    {
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
    }
     
    reload()
    {
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
    }
     
    restart()
    {
    stop
    start
    }
     
    configtest()
    {
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
    }
     
    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    reload)
    reload
    ;;
    restart)
    restart
    ;;
    configtest)
    configtest
    ;;
    *)
    echo $"Usage: $0 {start|stop|reload|restart|configtest}"
    RETVAL=1
    esac
     
    exit $RETVAL

    [root@lizhipenglinux01 nginx-1.12.1]# chmod 755 /etc/init.d/nginx
    [root@lizhipenglinux01 nginx-1.12.1]# chkconfig --add nginx

    [root@lizhipenglinux01 nginx-1.12.1]# chkconfig nginx on

    配置的模板已经存在,不用他,故改名备份一下

    [root@lizhipenglinux01 conf]# vim nginx.conf

    user nobody nobody;                                                 定义启动nginx服务的是哪个用户
    worker_processes 2;                                                  定义子进程有几个,2个
    error_log /usr/local/nginx/logs/nginx_error.log crit; 
    pid /usr/local/nginx/logs/nginx.pid;
    worker_rlimit_nofile 51200;                                       nginx最多打开多少个文件
     
    events
    {
    use epoll;
    worker_connections 6000;                                        进程最多有多少个连接
    }
     
    http
    {
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm
    application/xml;
     
    server                                                      每一个server对应一个主机
    {
    listen 80;
    server_name localhost;
    index index.html index.htm index.php;
    root /usr/local/nginx/html;
    location ~ .php$
    {
    include fastcgi_params;
    fastcgi_pass unix:/tmp/php-fcgi.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    }
    }
    }

    [root@lizhipenglinux01 conf]# /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

    s代表父进程,root是父进程,nobody是子进程

    默认虚拟主机

    [root@lizhipenglinux01 conf]# curl localhost/1.php
    This is nginx test page.[root@lizhipenglinux01 conf]#

    先来解释“虚拟主机”。在介绍httpd的时候,已经解释过默认虚拟主机的概念,在Nginx中也有默认虚拟主机,跟httpd类似,第一个被Nginx加载的虚拟主机就是默认主机。但和httpd不同的地方是,它还有一个配置用来标记默认虚拟主机。也就是说,如果没有这个标记,第一个虚拟主机默认虚拟主机。删除nginx.conf如下部分。

    添加如下部分

    意思是,/usr/local/nginx/conf/vhost/下面的所有以.conf结尾的文件都会加载,这样我们就可以把所有虚拟主机配置文件放到vhost目录下面

    [root@lizhipenglinux01 conf]# pwd
    /usr/local/nginx/conf
    [root@lizhipenglinux01 conf]# mkdir vhost
    [root@lizhipenglinux01 conf]# cd vhost/
    [root@lizhipenglinux01 vhost]# ls
    [root@lizhipenglinux01 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;
    }

    [root@lizhipenglinux01 vhost]# mkdir /data/wwwroot/default/

    [root@lizhipenglinux01 vhost]# cd /data/wwwroot/default/
    [root@lizhipenglinux01 default]# ls

    [root@lizhipenglinux01 default]# vim index.html

    This is the default site.

    [root@lizhipenglinux01 default]# /usr/local/nginx/sbin/nginx -t       检查有没有语法错误
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

    [root@lizhipenglinux01 default]# /usr/local/nginx/sbin/nginx -s reload   重新加载

    [root@lizhipenglinux01 default]# curl localhost
    This is the default site.
    [root@lizhipenglinux01 default]# curl -x127.0.0.1:80 bbb.com
    This is the default site.
    [root@lizhipenglinux01 default]# curl -x127.0.0.1:80 aaa.com
    This is the default site.

    [root@lizhipenglinux01 default]# 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;     用户名密码文件
    }
    }

    [root@lizhipenglinux01 default]# vim /usr/local/nginx/conf/vhost/test.com.conf
    [root@lizhipenglinux01 default]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd aming
    New password:
    Re-type new password:
    Adding password for user aming

    [root@lizhipenglinux01 default]# /usr/local/apache2.4/bin/htpasswd /usr/local/nginx/conf/htpasswd user1    第二个的时候不要-c,-c表示生成,会重置之前的
    New password:
    Re-type new password:
    Adding password for user user1
    [root@lizhipenglinux01 default]# cat /usr/local/nginx/conf/htpasswd
    aming:$apr1$tmIuz3nE$1j60Dm8yXQkDEJtWnF5s31
    user1:$apr1$erglUoeF$vY7Rnm9/A12JjRoc0fM4d.

    [root@lizhipenglinux01 default]# /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@lizhipenglinux01 default]# /usr/local/nginx/sbin/nginx -s reload

    这个是针对目录的

    [root@lizhipenglinux01 default]# /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@lizhipenglinux01 default]# /usr/local/nginx/sbin/nginx -s reload

    这个时候连接test.com不需要密码,但是/admin目录下 需要

    [root@lizhipenglinux01 default]# curl -uaming:lizhipeng -x127.0.0.1:80 test.com/admin/         
    test.com admin dir

    并不需要-u

    [root@lizhipenglinux01 default]# curl -x127.0.0.1:80 test.com/admin.php        这个就需要了
    <html>
    <head><title>401 Authorization Required</title></head>
    <body bgcolor="white">
    <center><h1>401 Authorization Required</h1></center>
    <hr><center>nginx/1.12.1</center>
    </body>
    </html>

    test4.com会去访问默认的虚拟主机

  • 相关阅读:
    sys模块详解
    os模块详解2
    tyvj 1203 机器分配
    洛谷 P1496 火烧赤壁
    P1204 [USACO1.2]挤牛奶Milking Cows
    bzoj 2120 数颜色
    P2056 采花
    P1972 [SDOI2009]HH的项链
    9.20模拟赛
    P2709 小B的询问
  • 原文地址:https://www.cnblogs.com/sisul/p/8541754.html
Copyright © 2020-2023  润新知