美文网首页
nginx1-概念、虚机主机

nginx1-概念、虚机主机

作者: 指尖架构141319 | 来源:发表于2019-11-04 17:54 被阅读0次

1.理论

1.nginx在系统架构(网关入口)中的作用:
  • 网关
  • 虚拟主机
  • 路由功能(与微服务对应):域名/路径,进行路由选择后台服务器
  • 负载功能(与高并发高可用对应):对后台服务器集群进行负载
  • 静态服务器(比tomcat性能高很多):在mvvm模式中,充当文件读取职责
    总结:实际使用中,这三项功用,会混合使用。比如先分离动静,再路由服务,再负载机器

2.安装

  • 安装make
    yum -y install autoconf automake make
  • 安装g++
    yum -y install gcc gcc-c++
  • 安装依赖库
    yum -y install pcre pcre-devel
    yum -y install zlib zlib-devel
    yum install -y openssl openssl-devel
  • 下载安装包,解压
    wget http://nginx.org/download/nginx-1.9.15.tar.gz
    tar -zxvf nginx-1.9.15.tar.gz
    cd nginx-1.9.15
    ./configure --prefix=/usr/local/application/nginx
    make
    make install

3.目录结构

  • Conf 配置文件
  • Html 网页文件
  • Logs 日志文件
  • Sbin 二进制程序

如果ifconfig突然丢失了ip地址,可以用一下命令刷新:
sudo dhclient eth0 更新IP地址
eth0对应的是自己的值,我的是:eno16777736

image.png

4.基本命令

进入到nginx的sbin目录下:

  • 启动
    ./nginx -c /usr/local/application/nginx/conf/nginx.conf
  • 停止
    ./nginx -s stop
  • 退出
    ./nginx -s quit
  • 重新加载nginx.conf
    ./nginx -s reload

5.nginx概念模型

1.nginx会按需同事运行多个进程
  • 一个主进程master和几个工作进行worker,缓存进程cache loader和缓存管理进程cache manager等。
  • 所有进程均是仅含有一个线程,并通过 “共享内存” 机制实现进程间通信。
  • 主进程以root用户身份进行,而worker、cache loader和cache manager均以非特权用户身份运行。
2.主进程主要完成如下工作:
  • 读取并验证配置信息
  • 创建、绑定及关闭套接字
  • 启动、终止及维护worker进程的个数
  • 无需终止服务而重新配置工作特性
  • 重新打开日志文件
3.worker进程主要完成的任务:
  • 接收、传入并处理来自客户端的连接
  • 提供反向代理及过滤功能
  • nginx任何能完成的其他工作

6.nginx.conf配置文件结构

image.png
#user  nobody;  主模块命令, 指定Nginx的worker进程运行用户以及用户组,默认由nobody账号运行。
worker_processes  1;  指定Nginx要开启的进程数

#error_log  logs/error.log; #用来定义全局错误日志文件的路径和日志名称
#error_log  logs/error.log  notice; #日志输出级别有debug,info,notice,warn,error,crit 可供选择,
                                    其中debug输出日志最为详细,面crit(严重)输出日志最少。默认是error
#error_log  logs/error.log  info;

#pid        logs/nginx.pid; 用来指定进程id的存储文件位置

events 设定nginx的工作模式及连接数上限,其中参数use用来指定nginx的工作模式(这里是epoll,epoll是多路复用
IO(I/O Multiplexing)中的一种方式),nginx支持的工作模式有select,poll,kqueue,epoll,rtsig,/dev/poll。
其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,对于linux系统,epoll是首选。

events {
    use epoll;
    worker_connections  1024;    是设置nginx每个进程最大的连接数,默认是1024,所以nginx最大的连接数
#max_client=worker_processes * worker_connections。进程最大连接数受到系统最大打开文件数的限制,需要设置
#ulimit。
}


http {
    include       mime.types;  主模块命令,对配置文件所包含文件的设定,减少主配置文件的复杂度,相当于把部分
                               设置放在别的地方,然后在包含进来,保持主配置文件的简洁
    default_type  application/octet-stream;    默认文件类型,当文件类型未定义时候就使用这类设置的。

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  指定nginx日志的格式
    #                  '$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;  客户端连接超时时间

    #gzip  on;   设置是否开启gzip模块

    server {
        listen       80;  虚拟主机的服务端口
        server_name  localhost;  用来指定ip或者域名,多个域名用逗号分开

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            地址匹配设置,支持正则匹配,也支持条件匹配,这里是默认请求地址,用户可以location命令对nginx进
            行动态和静态网页过滤处理
            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;
    #    }
    #}

}

6.1 日志
  • 通过访问日志,你可以得到用户地域来源、跳转来源、使用终端、某个URL访问量等相关信息;通过错误日志,你可以得到系统某个服务或server的性能瓶颈等。因此,将日志好好利用,你可以得到很多有价值的信息
  • 日志格式
    打开nginx.conf配置文件:vi /usr/local/application/nginx/conf/nginx.conf,日志部分内容:
    #access_log logs/access.log main;
    日志生成的到Nginx根目录logs/access.log文件,默认使用“main”日志格式,也可以自定义格式。
  • 默认main日志格式
$remote_addr 客户端的ip地址(代理服务器,显示代理服务ip)
$remote_user 用于记录远程客户端的用户名称(一般为“-”)
$time_local 用于记录访问时间和时区
$request 用于记录请求的url以及请求方法
$status 响应状态码,例如:200成功、404页面找不到等。
$body_bytes_sent 给客户端发送的文件主体内容字节数
$http_user_agent 用户所使用的代理(一般为浏览器)
$http_x_forwarded_for 可以记录客户端IP,通过代理服务器来记录客户端的ip地址
$http_referer 可以记录用户是从哪个链接访问过来的
  • 查看日志
    tail -f /usr/local/application/nginx/logs/access.log

相关文章

网友评论

      本文标题:nginx1-概念、虚机主机

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