美文网首页
nginx学习笔记一基础配置

nginx学习笔记一基础配置

作者: 讲道理很没道理 | 来源:发表于2019-09-28 14:02 被阅读0次

    Nginx简介
    Apache
    Apache仍然是时长占用量最高的web服务器,据最新数据统计,市场占有率目前是50%左右。主要优势在于一个是比较早出现的一个Http静态资源服务器,同时又是开源的。所以在技术上的支持以及市面上的各种解决方案都比较成熟。Apache支持的模块非常丰富。
    Nginx
    Nginx是俄罗斯人编写的一款高性能的HTTP和反向代理服务器,在高连接并发的情况下,它能够支持高达50000个并发连接数的响应,但是内存、CPU等系统资源消耗却很低,运行很稳定。目前Nginx在国内很多大型企业都有应用,据最新统计,Nginx的市场占有率已经到33%左右了。而Apache的市场占有率虽然仍然是最高的,但是是呈下降趋势。而Nginx的势头很明显。选择Nginx的理由也很简单:第一,它可以支持5W高并发连接;第二,内存消耗少;第三,成本低,如果采用F5、NetScaler等硬件负载均衡设备的话,需要大几十万。而Nginx是开源的,可以免费使用并且能用于商业用途

    架构中的作用
    介绍nginx在系统架构(网关入口)中的作用,总结如下:
    1、路由功能(与微服务对应):域名/路径,进行路由选择后台服务器
    2、负载功能(与高并发高可用对应):对后台服务器集群进行负载
    3、静态服务器(比tomcat性能高很多):在mvvm模式中,充当文件读取职责
    总结:实际使用中,这三项功用,会混合使用。比如先分离动静,再路由服务,再负载机器
    正向代理与反向代理
    1、代理:客户端自己请求出现困难。客户请了一个代理,来代自己做事,就叫代理。
    比如代理律师,代购,政府机关办事的代理人等等。
    2、反向代理,服务端推出的一个代理招牌。

    安装make:

    yum -y install autoconf automake make
    

    安装g++:

    yum -y install gcc gcc-c++ 
    

    安装nginx依赖的库

    #正则表达式模块
    yum -y install pcre pcre-devel    
    #原始库的
    yum -y install zlib zlib-devel
    #HTTP服务的包
    yum install -y openssl openssl-devel
    

    安装方式1:源码安装
    步骤1:下载源码

    wget http://nginx.org/download/nginx-1.9.15.tar.gz
    
    tar -zxvf nginx-1.9.0.tar.gz
    
    cd nginx-1.9.0
    

    步骤2:
    编译,指定目录,和安装模块
    --prefix指定安装目录
    --with-http_ssl_module安装https模块
    creating objs/Makefile 代表编译成功

    ./configure   --prefix=/usr/local/nginx --with-http_stub_status_module 
    

    步骤3:安装

    make && make install 
    
    whereis nginx
    
    image.png

    目录结构:

    •conf 配置文件
    •html 网页文件
    •logs 日志文件
    •sbin 二进制程序

    启动

    sbin/nginx
    

    输入nginx 服务器地址:http://106.52.69.141/

    启停命令:

    ./nginx -c nginx.conf的文件。如果不指定,默认为NGINX_HOME/conf/nginx.conf
    ./nginx -s stop 停止
    ./nginx -s quit退出
    ./nginx -s reload 重新加载nginx.conf
    发送信号的方式
    kill -QUIT 进程号 安全停止
    kil -TERM 进程号 立即停止

    nginx模型概念:

    Nginx会按需同时运行多个进程:
    一个主进程(master)和几个工作进程(worker),配置了缓存时还会有缓存加载器进程(cache loader)和缓存管理器进程(cache manager)等。
    所有进程均是仅含有一个线程,并主要通过“共享内存”的机制实现进程间通信。
    主进程以root用户身份运行,而worker、cache loader和cache manager均应以非特权用户身份(user配置项)运行。

    主进程主要完成如下工作:

    1. 读取并验正配置信息;
    2. 创建、绑定及关闭套接字;
    3. 启动、终止及维护worker进程的个数;
    4. 无须中止服务而重新配置工作特性;
    5. 重新打开日志文件;

    worker进程主要完成的任务包括:

    1. 接收、传入并处理来自客户端的连接;
    2. 提供反向代理及过滤功能;
    3. nginx任何能完成的其它任务;

    查看进程

    ps -ef | grep nginx
    
    image.png

    配置文件:


    image.png

    nginx.conf配置文件结构


    image.png

    原始配置文件

    #user  nobody;
    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 {
        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  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                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;
        #    }
        #}
    
    }
                                          
    

    配置文件解析

    #user  nobody;  #主模块命令, 指定Nginx的worker进程运行用户以及用户组,默认由nobody账号运行。
    worker_processes  1; #指定Nginx要开启的进程数。
    
    #用来定义全局错设日志文件的路径和日志名称。 
    # 日志输出级别有debug,info,notice,warn,error,crit
    #可供选择,其中debug输出日志最为详细,面crit(严重)输出日志最少。默认是error
    
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;  #用来指定进程id的存储文件位置。
    
    #event 设定nginx的工作模式及连接数上限,  其中参数use用来指定nginx的工作模式(
    #这里是epoll,epoll是多路
    #复用IO(I/O Multiplexing)中的一种方式)
    #nginx支持的工作模式有select ,poll,kqueue,epoll,rtsig,/dev/poll。
    #其中select和poll都是标准的工作模式,
    #kqueue和epoll是高效的工作模式,对于linux系统,epoll是首选
    /**
    worker_connection是设置nginx每个进程最大的连接数,默认是1024,所以nginx最大的连接数max_client=worker_processes * worker_connections。
    进程最大连接数受到系统最大打开文件数的限制,需要设置ulimit
    */
    events {
        worker_connections  1024;
    }
    
    
    /**
    http
     include       mime.types;主模块命令,对配置文件所包含文件的设定,减少主配置文件的复杂度,相当于把部分设置放在别的地方,然后在包含进来,保持主配置文件的简洁
      default_type  application/octet-stream; 默认文件类型,当文件类型未定义时候就使用这类设置的。
    **/
    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; #开启高效文件传输模式(zero copy 方式),避免内核缓冲区数据和用户缓冲区数据之间的拷贝。
        #tcp_nopush     on; #开启TCP_NOPUSH套接字(sendfile开启时有用)
    
        #keepalive_timeout  0; #客户端连接超时时间
        keepalive_timeout  65;
    
    # 浏览器解压,nginx 压缩,要统一格式
        #gzip  on;
    
        server {
            listen       80;  #虚拟主机的服务端口
            server_name  localhost; #用来指定ip或者域名,多个域名用逗号分开
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
    /** #地址匹配设置,支持正则匹配,也支持条件匹配,这里是默认请求地址,用户可以location命令对nginx进行动态和静态网页过滤处理**/
            location / {
                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;
        #    }
        #}
    
    }
                                          
    
    /usr/local/nginx/sbin/nginx -t
    #重启nginx
    /usr/local/nginx/sbin/nginx -s reload
    
    

    Nginx日志描述
    通过访问日志,你可以得到用户地域来源、跳转来源、使用终端、某个URL访问量等相关信息;通过错误日志,你可以得到系统某个服务或server的性能瓶颈等。因此,将日志好好利用,你可以得到很多有价值的信息。
    日志格式
    打开nginx.conf配置文件:vi /usr/local/nginx/conf/nginx.conf
    日志部分内容:
    access_log logs/access.log main;
    日志生成的到Nginx根目录logs/access.log文件,默认使用“main”日志格式,也可以自定义格式。

    image.png

    查看日志命令

    tail -f /usr/local/nginx/logs/access.log
    

    日志配置和及切割
    /etc/init.d/rsyslog start #系统日志,如不开启,看不到定时任务日志
    /etc/rc.d/init.d/crond start #定时任务开启

    步骤1:编写sh:logcat.sh

    #!/bin/bash
    #设置日志文件存放目录
    LOG_HOME="/usr/local/nginx/logs/"
    #备分文件名称
    LOG_PATH_BAK="$(date -d yesterday +%Y%m%d%H%M)"
    #重命名日志文件
    mv ${LOG_HOME}/access.log ${LOG_HOME}/access.${LOG_PATH_BAK}.log
    mv ${LOG_HOME}/error.log ${LOG_HOME}/error.${LOG_PATH_BAK}.log
    kill -USR1 'cat ${LOG_HOME}/nginx.pid'
    
    #向nginx主进程发信号重新打开日志
    

    步骤2:添加执行权限

    chmod +x logout.sh
    
    #查看是否安装了自动任务crontab
    rpm -qa |grep crontab
    crontab -e
    #重启定时任务
    service crond restart
    #查看定时任务的日志
    tail -f /var/log/cron
    

    来自享学 -peter

    相关文章

      网友评论

          本文标题:nginx学习笔记一基础配置

          本文链接:https://www.haomeiwen.com/subject/kjvpuctx.html