zoukankan      html  css  js  c++  java
  • Nginx的安装(笔记)

    0, 先决条件
    Nginx 依赖 zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre
    安装命令:
    yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

    pcre安装命令:
    wget http://downloads.sourceforge.net/project/pcre/pcre/8.41/pcre-8.41.tar.gz
    tar -zxvf pcre-8.41.tar.gz;
    cd pcre-8.41;
    ./configure
    make && make install
    pcre-config --version

    1,Nginx 安装
    1.1, 下载 Nginx,下载地址:http://nginx.org/download/nginx-1.13.0.tar.gz
    $ wget http://nginx.org/download/nginx-1.13.0.tar.gz
    1.2, 解压安装包
    $ tar -zxvf nginx-1.13.0.tar.gz
    1.3, 进入安装包目录
    $ cd nginx-1.13.0
    1.4, 编译安装
    $ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-7.8
    $ make
    $ make install
    1.5, 查看nginx版本
    $ /usr/local/nginx/sbin/nginx -v
    到此,nginx安装完成。

    2, Nginx 配置
    2.1, 创建 Nginx 运行使用的用户 www(可选)
    $ /usr/sbin/groupadd www
    $ /usr/sbin/useradd -g www www
    2.2, 配置nginx.conf 默认路径:/usr/local/nginx/conf/nginx.conf
    $ cat /usr/local/nginx/conf/nginx.conf
    # 访问的用户
    # user root;
    # 设置值和CPU核心数一致
    worker_processes 16;

    # 日志位置和日志级别
    #error_log logs/error.log;
    #error_log logs/error.log notice;
    #error_log logs/error.log info;

    #pid logs/nginx.pid;


    events {
    worker_connections 1024;
    }


    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;

    keepalive_timeout 65;

    gzip on;

    upstream system_server {
    server 127.0.0.1:8080;
    keepalive 2000;
    }

    server {
    listen 801;
    # listen 443 ssl;
    server_name 127.0.0.1;

    #
    # ssl on;
    # ssl_certificate /usr/local/nginx/conf/ssl/server.crt;
    # ssl_certificate_key /usr/local/nginx/conf/ssl/server.key;
    # ssl_session_timeout 5m;
    # ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    # ssl_prefer_server_ciphers on;


    # charset koi8-r;

    access_log logs/host.access.log main;

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

    # websocket
    location ^~ /socket {
    proxy_pass http://system_server;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    }

    # 正则表达式匹配路径
    location ~* ^/system/.*$ {
    proxy_pass http://system_server;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

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

    }

    2.3,检查配置文件ngnix.conf的正确性命令:
    $ /usr/local/webserver/nginx/sbin/nginx -t

    3, 启动 Nginx
    3.1 启动命令如下:
    $ /usr/local/webserver/nginx/sbin/nginx
    3.2 访问站点
    从浏览器访问我们配置的站点ip:http://127.0.0.1:9090

    4, 其它
    4.1 常用命令:
    # 重新载入配置文件
    /usr/local/webserver/nginx/sbin/nginx -s reload
    # 重启 Nginx
    /usr/local/webserver/nginx/sbin/nginx -s reopen
    # 停止 Nginx
    /usr/local/webserver/nginx/sbin/nginx -s stop

    4.2 location配置语法规则: location [=|^~|~|~*] /uri/ { … }
    = 开头表示普通字符开头,匹配规则:精确匹配;
    ^~ 开头表示普通字符开头,匹配规则:uri 最长路径匹配;
    ~ 开头表示特殊字符开头,匹配规则:正则匹配(区分大小写);
    ~* 开头表示特殊字符开头,匹配规则:正则匹配(不区分大小写);
    !~ 匹配规则:区分大小写不匹配的正则;
    !~* 匹配规则:不区分大小写不匹配的正则;
    / 通用匹配,任何请求都会匹配到。

    4.3 rewrite 重写规则语法: rewrite 正则 替换 标志位
    4.3.1 正则表达式
    ^/images/([a-z]{2})/([a-z0-9]{5})/(.*).(png|jpg|gif)$ ---->http://xxxx.com/images/aa/abc01/test.gif
    #其中
    $1=([a-z]{2}) #$1=aa
    $2=([a-z0-9]{5}) #$2=abc01
    $3=(.*) #$3=test
    $4=(png|jpg|gif) #$4=gif
    4.3.2 URI 重写
    /data?file=$3.$4 # rewrite之后的query http://data?file=test.gif
    4.3.3 尾部的标记 last return break
    last 标记之后会从新loaction ,继续rewrite 最多10次;
    break标记是直接跳槽rewrite和localtion 进行query的处理
    return标记停止rewrite 处理指令,进而控制主HTTP 模块处理请求,也就是HTTP请求也不处理了,直接给client 返回(结合error0page)

    注意:nginx不对url做编码。例如:请求:/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

    4.4 开机启动

    在/etc/init.d/nginx (没有nginx文件则新建)中输入如下命令:

    #!/bin/sh
    #
    # nginx - this script starts and stops the nginx daemon
    # official web       https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
    #
    # 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="/usr/sbin/nginx"
    prog=$(basename $nginx)
    
    NGINX_CONF_FILE="/etc/nginx/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:.*--user=" | sed 's/[^*]*--user=([^ ]*).*/1/g' -`
       if [ -n "$user" ]; then
          if [ -z "`grep $user /etc/passwd`" ]; then
             useradd -M -s /bin/nologin $user
          fi
          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
        fi
    }
    
    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

    自定义编译安装的nginx,需要根据安装路径修改下面这两项配置:
    nginx=”/usr/sbin/nginx” 修改成nginx执行程序的路径。
    NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路径。

    5, FAQs
    5.1 NGINX启动时提示错误
    /usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
    解决方法:
    #64位OS
    ln -s /usr/local/lib/libpcre.so.1 /lib64
    #32位OS
    ln -s /usr/local/lib/libpcre.so.1 /lib

  • 相关阅读:
    protobuf lib库的使用
    protobuf的下载、编译和使用
    使用python和pygame绘制繁花曲线
    经典方块游戏-俄罗斯方块
    经典方块游戏-贪吃蛇
    经典方块游戏-基础
    经典方块游戏一
    Python脚本管理
    SublimeText3设置显示空格及Tab显示为4个空格
    域名解析记录类型
  • 原文地址:https://www.cnblogs.com/cityspace/p/6806138.html
Copyright © 2011-2022 走看看