• nginx安装及优化


    1.pcre及nginx安装包下载 wget

    http://www.pcre.org/   pcre用yum安装即可

    http://nginx.org/en/download.html

    2.安装

    -安装所需依赖  yum install -y openssl openssl-devel   gcc*  -y

    -创建安装目录/app

    -进入/app,  wget  http://nginx.org/en/download.html/nginx2.2.tar.gz

    -解压  tar xf nginx2.2.tar.gz

    -创建用户 useradd nginx -s /sbin/nologin -M

    -安装  cd  /app/ngnix2.2

         ./configure   --user=nginx   --group=nginx   --prefix=/app/nginx2.2  --with-http_stub_status_module   --with-http_ssl_module

     make

     make install

    -创建软连接

    ln -s /app/nginx2.2  /app/nginx

    3.安装完成后,检查配置语法

    /app/nginx/sbin/nginx -t

    出现以下语句为正常

    nginx: the configuration file /app/nginx-1.10.3//conf/nginx.conf syntax is ok
    nginx: configuration file /app/nginx-1.10.3//conf/nginx.conf test is successful

    4.启动服务

    /app/nginx/sbin/nginx

    5.查看nginx是否启动

    [root@localhost nginx]# lsof -i :80

    出现以下为正常
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    nginx 1495 root 6u IPv4 13304 0t0 TCP *:http (LISTEN)
    nginx 1496 nginx 6u IPv4 13304 0t0 TCP *:http (LISTEN)

    6.客户端检查服务是否正常可用

    linux:wget 127.0.0.1

    windows: 浏览器输入127.0.0.1

    7.部署一个站点,域名为www.wt.com  与bbs.wt.com

    为了使各个子站点分开,将他们放在不同的目录最好,配置也分开

    ngnix的网页存放位置:/app/nginx/html目录,在该目录下创建www和bbs两个子目录

    /app/nginx/html目录
    ├── 50x.html:默认错误网页
    ├── bbs
    │   └── index.html
    ├── index.html
    └── www
    └── index.html默认网页

    8.编写配置文件,/app/ngnix/conf/ngnix.conf是主配置文件,再在该目录下新建一个子目录erxtra,在/app/nginx/conf/extra目录下新建两个bbs.conf,www.conf,status.conf配置文件,

    为了安全起见,状态信息页面不要让外部用户访问

    .
    ├── extra
    │   ├── bbs.conf
    │   ├── status.conf  #ngnix状态监测网页
    │   └── www.conf
    ├── fastcgi.conf
    ├── fastcgi.conf.default
    ├── fastcgi_params
    ├── fastcgi_params.default
    ├── koi-utf
    ├── koi-win
    ├── mime.types
    ├── mime.types.default
    ├── nginx.conf
    ├── nginx.conf.bak
    ├── nginx.conf.default
    ├── scgi_params
    ├── scgi_params.default
    ├── uwsgi_params
    ├── uwsgi_params.default
    └── win-utf

    nginx.conf

    #user  nobody;
    worker_processes  1;   #work进程数量
    
    error_log  logs/error.log;  #错误日志记录  目录为/app/nginx/logs/
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    events {#事块区
        worker_connections  1024;#每个worker进程支持的最大连接数
    }
    http { #http区
        include       mime.types;         #  nginx支持的媒体类型库文件
        include       extra/www.conf;  #子站点的配置文件
        include       extra/bbs.conf;  #子站点配置文件
        include       extra/status.conf;#状态监测配置文件
        default_type  application/octet-stream;#默认的媒体类型
        sendfile        on;#开启高效传输模式
        keepalive_timeout  65;#连接超时时间  65秒
    }

    bbs.conf

        server {
            listen       80;
            server_name  bbs.wt.com;#站点别名
    
            location / {  #网页存放位置根目录,这里的‘/’根目录是/app/nginx/html
                root   html/bbs;  存放子站点网页文件的根目录
                index  index.html index.htm;#首页文件格式
            }
            error_page   500 502 503 504  /50x.html;#指定错误网页,这里的'/'表示/app/nginx/html
            location = /50x.html {
                root   html;
            }
        }

    www.conf

        server {
            listen       80;
            server_name  www.wt.com wt.com;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }

    status.conf

    server {
            listen       80;
            server_name  status.wt.com;
            location / {
                stub_status  on;#状态信息开关  打开
                access_log   off;
            }
    }

    9.配置完后,检查配置语句,并平滑重启

    /app/ngnix/sbin/nginx -t  检查配置语句

    /app/ngnix/sbin/nginx -s reload  平滑重启

    10.浏览器测试

    Windows要配置c:/windows/System32/drivers/etc/hosts  来做dns解析

    192.168.1.55     www.wt.com    bbs.wt.com  wt.com   status.wt.com

    11.日志记录配置,错误日志及访问日志

       nginx.conf

    需要注意的是:log_format配置必须放在http区块内,否则会出现报错:

    #user  nobody;
    worker_processes  1;
    
    error_log  logs/error.log;#错误日志记录
    error_log /dev/null; #关闭错误日志 events { worker_connections
    1024; } http { include mime.types; #######设置访问日志格式,日志格式一定要放在include前边才行,下边的main为日志格式的名称######### log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; include extra/www.conf; include extra/bbs.conf; include extra/status.conf; default_type application/octet-stream; sendfile on; keepalive_timeout 65; }

    日志变量含义:

    $server_name:虚拟主机名称。
    $remote_addr:远程客户端的IP地址。
    -:空白,用一个“-”占位符替代,历史原因导致还存在。
    $remote_user:远程客户端用户名称,用于记录浏览者进行身份验证时提供的名字,如登录百度的用户名scq2099yt,如果没有登录就是空白。
    [$time_local]:访问的时间与时区,比如18/Jul/2012:17:00:01 +0800,时间信息最后的"+0800"表示服务器所处时区位于UTC之后的8小时。
    $request:请求的URI和HTTP协议,这是整个PV日志记录中最有用的信息,记录服务器收到一个什么样的请求
    $status:记录请求返回的http状态码,比如成功是200。
    $uptream_status:upstream状态,比如成功是200.
    $body_bytes_sent:发送给客户端的文件主体内容的大小,比如899,可以将日志每条记录中的这个值累加起来以粗略估计服务器吞吐量。
    $http_referer:记录从哪个页面链接访问过来的。 
    $http_user_agent:客户端浏览器信息
    $http_x_forwarded_for:客户端的真实ip,通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
    $ssl_protocol:SSL协议版本,比如TLSv1。
    $ssl_cipher:交换数据中的算法,比如RC4-SHA。 
    $upstream_addr:upstream的地址,即真正提供服务的主机地址。 
    $request_time:整个请求的总时间。 
    $upstream_response_time:请求过程中,upstream的响应时间。

      extra/bbs.conf     access_log后边的gzip  buffer  flush可以优化高并发下网站访问性能

    如果定义了buffer和gzip其中一个参数,日志默认会先写入缓存中,当缓存满了之后,通过gzip压缩缓存中的日志并写入文件,启用了gzip压缩必须保证nginx安装的时候添加了gzip模块。

    缓存大小默认为64K。可以配置gzip的1~9的压缩级别,级别越高压缩效率越大,日志文件占用的空间越小,但要求系统性能也越高。默认值是1。

        server {
            listen       80;
            server_name  bbs.wt.com;
    
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
            access_log   logs/access_bbs.log   main  gzip  buffer=32k   flusk=5s;#只需添加这一句即可,access_bbs.log为日志名,其中(gzip  buffer=32k  flush=5s为优化高并发下提升
    网站访问性能) }

     extra/bbs.conf

        server {
            listen       80;
            server_name  wwww.wt.com;
    
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
            access_log  logs/access_www.log  main; #指定日志名即可
        }

    12.配置完后,检查配置语句,并平滑重启

    /app/ngnix/sbin/nginx -t  检查配置语句

    /app/ngnix/sbin/nginx -s reload  平滑重启

    13.日志切割

    设置一个定时任务,媒体00点执行/app/nginx/cut_logs.py切割日志

    00  00  *  *  *  /usr/bin/python  /app/nginx/cut_logs.py  > /dev/null/  2>&1

    脚本内容:

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    # author:wt
    
    import datetime,time
    t = time.strftime('%Y-%m-%d',time.localtime(time.time()))
    print(t)
    try:
        sh.mv('/app/nginx/logs/access_www.log','/app/nginx/logs/access_www_%s.log'%t)
    except:
        p = 1

     14.location  URI匹配

        ~:正则,用于区分大小写匹配

        ~*:正则,用于不区分大小写匹配

        ^~:在做完常规字符串匹配后,不做正则匹配

        匹配优先级:

      1."location = / {}"  精确匹配,当用户的请求为空或为“/”时,请求就直接到这个大括号里来

      2."location ^~  /images/  {}"  路径优先匹配,当URI是以/images/开头的路径时,就不再进行正则匹配检查了,请求就直接到这个大括号里

      3."location ~*  .(gif|jpg|jpeg)$  {}" 正则匹配,当URI是以“.gif或.jpg或.jpeg”结尾的时候,请求就直接到这个大括号里来

      4."location  /doc/  {}"   匹配常规字符串,如果有正则,则优先匹配正则    如: www.wt.com/doc/1.jpg这个URI会直接被nginx分配到3的大括号里去

      5."location  /  {}" 以上所有location都不能匹配上时,就默认把请求分配到这个大括号里

          nginx  location配置实例:

          cat /app/nginx/conf/extra/www.conf

    server {
            listen       80;
            server_name  www.wt.com wt.com;
            root   html/www;
            location / {
                return 400;
             #   index  index.html index.htm;
            }
            location = / {
                return 402;
            }
            location  /doc/ {
                return 403;
            }
            location ^~   /images {
                return 404;
            }
            location ~* .(gif|jpg|jpeg)$ {
                return 555;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }

    15.nginx重定向 (rewrite)

    应用位置:location块、server块、if

    语法:

    rewrite   正则表达式   要重定向到的地址   flag

    注意:

      在根location中(location  /  {})和server块中,flag最好用last,在其他地方最好用break

          flag参数:

          last:本条规则匹配完后,继续向下匹配新的location URI规则,浏览器地址不变

          break:本条规则匹配完成即终止,浏览器地址不变

          redirect:返回302临时重定向,浏览器显示跳转后的地址

          permanent:返回301永久重定向,浏览器显示跳转后的地址

    例子:

    访问域名  
    www.adc.com/image  自动跳转到  www.adc.com/make/image  
    这个如何写

    这种需求有多种方法可以实现:
    1. 利用Nginx rewrite 内部跳转实现:       
    location /image {
              rewrite ^/image/(.*)$     /make/image/$1 last;
    }


    2.利用alias映射
    location  /image  {
           alias  /make/image;  #这里写绝对路径
    }


    3.利用root映射:
    location  /image {
          root   /make;
    }


    4.利用nginx的permanent 301绝对跳转实现
    location /image {
            rewrite ^/image/(.*)$   http://www.adc.com/make/image/$1;
    }
       


    5.判断uri实现
    if ( $request_uri ~* ^(/image)){
            rewrite ^/image/(.*)$ /make/image/$1 last; 
    }

    16.Nginx设置访问认证

    应用位置:http块、server块、location块、limit_except块

    配置两个参数:

      auth_basic      在登录框显示服务器提示的内容

      auth_basic_user_file   存放用户名密码文件的绝对路径

    设置访问认证,需要用户名密码:

    yum install httpd -y

    [root@bogon nginx]# htpasswd -bc conf/htpasswd wt 123456
    Adding password for user wt
    [root@bogon nginx]# chmod 400 conf/htpasswd 
    [root@bogon nginx]# chown nginx conf/htpasswd 
    [root@bogon nginx]# cat conf/htpasswd 
    wt:5pVjNZUxOKNq2    #生成的密码是密文

    配置实例:

    [root@bogon nginx]# cat conf/extra/www.conf 
    server {
            listen       80;
            server_name  www.wt.com wt.com;
            root   html/www;
            location / {
              #  return 400;
                index  index.html index.htm;
                auth_basic  "wt";
                auth_basic_user_file  /app/nginx/conf/htpasswd;
            }
            location = / {
                return 402;
            }
            location  /doc/ {
                return 403;
            }
            location ^~   /images {
                return 404;
            }
            location ~* .(gif|jpg|jpeg)$ {
                return 410;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }

    17.nginx优化 

    http://blog.csdn.net/xifeijian/article/details/20956605

  • 相关阅读:
    盛最多水容器
    罗马数字和整数互相转化
    v-if和v-for
    扫描二维码登录
    使用Promise实现红绿灯交替重复亮
    利用promise实现间隔1s打印1,2,3
    原生js拖拽
    react项目引入使用element-react报错
    php实现类似淘宝最近浏览商品的功能模型代码
    教你如何把php项目打包成EXE文件发布
  • 原文地址:https://www.cnblogs.com/wt11/p/6420442.html
Copyright © 2020-2023  润新知