zoukankan      html  css  js  c++  java
  • 8.13 nginx安装、nginx默认虚拟主机、nginx用户认证、nginx域名重定向

    1、nginx安装

      通过weget下载安装包,执行tar zxvf命令进行解压

      然后执行./configure --prefix=/usr/local/nginx,编译时,可以根据实际需求,增加ssl等模块

      执行make&&install

      查看nginx的目录,存在以下目录

      

      conf目录存放配置文件

      html存放网页样例

      logs存在日志

      sbin存放核心进程

      创建启动脚本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

      更改权限chmod 755 /etc/init.d/nginx

      添加服务chkconfig --add nginx

      增加开机启动chkconfig nginx on

      修改配置文件,/usr/local/nginx/conf目录下已有nginx.conf文件,但是不使用此文件,所以需要执行mv nginx.conf nginx.conf1来重命名此文件,然后创建配置文件vim nginx.conf,增加如下配置:

    user nobody nobody;
    worker_processes 2;
    error_log /usr/local/nginx/logs/nginx_error.log crit;
    pid /usr/local/nginx/logs/nginx.pid;
    worker_rlimit_nofile 51200;  

    #最多可以打开多少个文件

    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
    {
    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;

    #指定php-fpm的监听地址,可以监听IP地址,写法为fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    }
    }
    }

      执行/usr/local/nginx/sbin/nginx -t检查配置文件是否正确

      开启服务services nginx start,执行ps aux |grep nginx查看nginx服务状态

       可以执行curl命令测试nginx页面

     2、nginx默认虚拟主机

      修改配置文件nginx.conf,删除如下部分

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

       然后增加如下配置:

      include vhost/*.conf;

      然后在/usr/local/nginx/conf/目录下创建vhost子目录,mkdir vhost

      创建配置文件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

      在该目录下定义index.html,并在该文件中随便写点内容

      然后执行/usr/local/nginx/sbin/nginx -t检查配置文件是否正确

      执行/usr/local/nginx/sbin/nginx -s reload重新加载配置文件

       此时使用curl进行访问时,则与之前不同

      由于该站点为默认站点,所以访问其他不存在的站点,也会默认访问该站点

    3、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安装httpd服务,然后使用htpasswd工具生产密码文件

      /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd aming

      此时该密码文件中就有相关的数据

      如果还需要再添加用户/usr/local/apache2.4/bin/htpasswd  /usr/local/nginx/conf/htpasswd user1,不需要使用-c选项

      然后再使用-t选项和-s reload选项去检查配置文件然后重新加载配置文件

      此时访问该站点时,提示401需要进行用户认证

      然后增加用户名密码再去访问时,可以正常访问

      以上用户认证针对的是整站,如果需要针对目录进行认证,则只需要修改配置文件中的location的参数为相应的目录,如/admin/

      此时,只有进行访问admin目录时才会提示401需要进行用户验证

      如果需要针对某一个url进行用户验证,则需要在配置文件中进行匹配,如果对admin.php进行用户验证

    4、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;

    //如果访问的域名不为test.com,则跳转到test.com

      }

    }

      然后检查配置文件并重新加载,使用curl访问test2.com时,被重定向至test.com

  • 相关阅读:
    慕课网-安卓工程师初养成-2-6 Java中的数据类型
    慕课网-安卓工程师初养成-2-5 如何命名Java变量
    慕课网-安卓工程师初养成-2-4 变量是什么
    慕课网-安卓工程师初养成-2-3 练习题
    慕课网-安卓工程师初养成-2-2 认识Java标识符
    慕课网-安卓工程师初养成-2-1 Java中的关键字
    慕课网-安卓工程师初养成-1-10 练习题
    Java编程——万年历
    排队购票
    猴子爬山
  • 原文地址:https://www.cnblogs.com/w494129131/p/9463340.html
Copyright © 2011-2022 走看看