大数据学习之nginx——01安装及相关知识

1. 前提准备

  1. 需要安装gcc的环境。

    yum install gcc-c++
    

    在这里插入图片描述

  2. 安装第三方的开发包

    1. PCRE

      yum install -y pcre pcre-devel
      

      在这里插入图片描述

    2. zlib

      yum install -y zlib zlib-devel
      

      在这里插入图片描述

    3. openssl

      yum install -y openssl openssl-devel
      

      在这里插入图片描述

  3. 可以执行以下命令, 一次性安装

    yum install gcc openssl-devel pcre-devel zlib-devel -y
    

2. 安装tengine

  1. 解压

  2. 进入tengine目录下执行:

    # /usr/tengine-2.1.0/ 为安装路径
    ./configure --prefix=/usr/tengine-2.1.0/
    
  3. 安装

    make && make install
    

3. 运行

  1. 启动tengine
    进入tengine的sbin目录
    在这里插入图片描述

  2. 启动tengine

    ./nginx
    
  3. 访问http://192.168.152.100/
    在这里插入图片描述

  4. 重启tengine

    ./nginx -s reload
    
  5. 关闭tengine

    ./nginx -s stop
    

4. 设置nginx服务

  1. 进入: cd /etc/init.d 目录, 创建nginx文件

    vi /nginx
    
  2. nginx文件内容

    #!/bin/sh
    #
    # nginx - this script starts and stops the nginx daemon
    #
    # chkconfig:   - 85 15 
    # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
    #               proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config:      /etc/nginx/nginx.conf
    # config:      /etc/sysconfig/nginx
    # pidfile:     /var/run/nginx.pid
     
    # Source function library.
    . /etc/rc.d/init.d/functions
     
    # Source networking configuration.
    . /etc/sysconfig/network
     
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
    
    # 修改为自己的路径 
    nginx="/opt/tengine-2.1.0/sbin/nginx"
    prog=$(basename $nginx)
    
    # 修改为自己的路径  
    NGINX_CONF_FILE="/opt/tengine-2.1.0/conf/nginx.conf"
     
    [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
     
    lockfile=/var/lock/subsys/nginx
     
    make_dirs() {
       # make required directories
       user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
       options=`$nginx -V 2>&1 | grep 'configure arguments:'`
       for opt in $options; do
           if [ `echo $opt | grep '.*-temp-path'` ]; then
               value=`echo $opt | cut -d "=" -f 2`
               if [ ! -d "$value" ]; then
                   # echo "creating" $value
                   mkdir -p $value && chown -R $user $value
               fi
           fi
       done
    }
     
    start() {
        [ -x $nginx ] || exit 5
        [ -f $NGINX_CONF_FILE ] || exit 6
        make_dirs
        echo -n $"Starting $prog: "
        daemon $nginx -c $NGINX_CONF_FILE
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
    }
     
    stop() {
        echo -n $"Stopping $prog: "
        killproc $prog -QUIT
        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
    }
     
    restart() {
        configtest || return $?
        stop
        sleep 1
        start
    }
     
    reload() {
        configtest || return $?
        echo -n $"Reloading $prog: "
        killproc $nginx -HUP
        RETVAL=$?
        echo
    }
     
    force_reload() {
        restart
    }
     
    configtest() {
      $nginx -t -c $NGINX_CONF_FILE
    }
     
    rh_status() {
        status $prog
    }
     
    rh_status_q() {
        rh_status >/dev/null 2>&1
    }
     
    case "$1" in
        start)
            rh_status_q && exit 0
            $1
            ;;
        stop)
            rh_status_q || exit 0
            $1
            ;;
        restart|configtest)
            $1
            ;;
        reload)
            rh_status_q || exit 7
            $1
            ;;
        force-reload)
            force_reload
            ;;
        status)
            rh_status
            ;;
        condrestart|try-restart)
            rh_status_q || exit 0
                ;;
        *)
            echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
            exit 2
    esac
    
    
  3. 此时可以在任意目录下执行以下操作

    service nginx start
    service nginx stop
    service nginx reload
    

三. 配置文件讲解

1. 配置文件

#user  nobody;
# 电脑有几个CPU, 这里就写几, 或者是CPU数的2倍
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
	# work连接数
    worker_connections  1024;
}

# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
#    load ngx_http_fastcgi_module.so;
#    load ngx_http_rewrite_module.so;
#}

http {
    include       mime.types;
    default_type  application/octet-stream;

	# 日志格式化定义
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

	# 多久之后才断开连接, 防止网络中频繁的进行三次握手
	# 实验中设置为0, 生产中再开启
    keepalive_timeout  0;
    # keepalive_timeout  65;

	# 是否开启压缩, 节省I/O
    #gzip  on;

    server {
    	# 监听80端口
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
        	# root: 指访问目录(html表示nginx的html目录)
        	# 一个server中只能设置一个root
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

2. 内容分析

1. work连接数设置
  1. 配置文件

    events {
    	# work连接数
        worker_connections  1024;
    }
    
  2. 这里的连接数可以设置10000级别, 但是如果只修改这里,并不会生效, 因为内核默认最多只会分配1024

    ulimit -a
    

    在这里插入图片描述

  3. 修改内核参数:

    ulimit -$Hn 2048
    

    在这里插入图片描述

2. location
  1. location格式

    location / {
    	# root: 指访问目录(html表示nginx的html目录)
    	# 一个server中只能设置一个root
        root   html;
        index  index.html index.htm;
    }
    
  2. 在html目录下创建abc/test.html

    在这里插入图片描述

3. location匹配顺序:
  1. 形式

    # 绝对匹配
    location = / {
        [ configuration A ]
    }
    
    # 最大前缀匹配
    location / {
        [ configuration B ]
    }
    
    # 
    location /documents/ {
        [ configuration C ]
    }
    
    # 不会阻断正则
    location ^~ /images/ {
        [ configuration D ]
    }
    
    # 正则
    location ~* \.(gif|jpg|jpeg)$ { 
        [ configuration E ] 
    } 
    
  2. 先普通

    1. 顺序无关
    2. 最大前缀
    3. 匹配规则简单
  3. 打断

    1. ^~
    2. 完全匹配
  4. 正则

    1. 不完全匹配
    2. 正则特殊性: 一条URI可以和多条location匹配上
    3. 顺序有关
    4. 先匹配, 先应用, 即时退出匹配
4. 反向代理
  1. 例子

    location /ooxx {
           proxy_pass https://www.baidu.com/;
    }
    

    在这里插入图片描述

  2. 但是此时如果搜索内容又会报错:

    在这里插入图片描述
    在这里插入图片描述

  3. 内容是以/s开头的, 所以会和之前的内容匹配, 不会匹配到ooxx. 所以需要在nginx中重新配置一个以/s开头的反向代理配置

    在这里插入图片描述
    在这里插入图片描述

末尾加不加/:
如果proxy_pass 后面的url没有"/", 则会传递location中的内容; 否则不传递

5. 反向代理池

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值