• Nginx 服务介绍


    静态 / 动态 Web 服务

    静态Web服务:nginx,apache,IIS,lighttpd,tengine,openresty-nginx

    动态Web服务:tomcat,resin,php,weblogic,jboss

    静态Web服务 无法直接处理动态请求(调用数据库),动态Web服务 可以直接处理动态请求(调用数据库)

    Nginx 简介

    Nginx (engine x) 是一个可靠的高性能的 HTTP 和反向代理 服务, 用于部署轻量级的 Web服务器 / 反向代理服务器 以及 电子邮件(IMAP/POP3)代理服务器,特点是占有内存少,并发能力强

    Nginx作为负载均衡服务:Nginx 既可以在内部直接支持 Python 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务

    img

    Nginx 的优点

    • Nginx非常轻量,功能模块少,源代码仅保留HTTP与核心模块代码,其余不够核心代码会作为插件来安装
    • 代码模块化 (易读,便于二次开发,对于开发人员非常友好)
    • Nginx技术成熟,具备的功能是企业最常使用而且最需要的
    • 适合当前主流架构趋势,微服务、云架构、中间层
    • 统一技术栈,降低维护成本, 降低技术更新成本
    • Nginx 采用了Epoll 模型(Apache 采用了 Select 模型)

    Nginx 和 Apache 的比较

    区别有点多,传送去看一下

    Nginx 的安装

    选择 Nginx 官网的安装包,若不想源码安装,可以配置yum仓库,配置nginx官方源,可以省去安装依赖包的麻烦

    # 添加 Nginx 官方源(稳定版)
    [root@web01 ~]# vi /etc/yum.repos.d/nginx.repo 
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true
    
    # 安装
    [root@web01 ~]# yum -y install nginx
    
    # Nginx 管理
    [root@web01 ~]# systemctl enable nginx
    [root@web01 ~]# systemctl start nginx
    [root@web01 ~]# systemctl stop nginx
    [root@web01 ~]# systemctl restart nginx
    [root@web01 ~]# systemctl reload nginx
    [root@web01 ~]# nginx
    [root@web01 ~]# nginx -s stop
    [root@web01 ~]# nginx -s reload
    
    # 查看 Nginx 配置文件格式是否有错误
    [root@web01 logrotate.d]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
    # 查看 Nginx 的版本
    [root@web01 logrotate.d]# nginx -v
    nginx version: nginx/1.18.0
    
    # 查看 Nginx 的版本 以及 编译信息(可选模块,文件目录,......)
    [root@web01 logrotate.d]# nginx -V
    nginx version: nginx/1.18.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
    [root@web01 logrotate.d]# 
    

    Nginx 相关文件

    # 查看 nginx 所有的 配置文件
    [root@web01 ~]# rpm -qc nginx
    /etc/logrotate.d/nginx
    /etc/nginx/conf.d/default.conf
    /etc/nginx/fastcgi_params
    /etc/nginx/koi-utf
    /etc/nginx/koi-win
    /etc/nginx/mime.types
    /etc/nginx/nginx.conf
    /etc/nginx/scgi_params
    /etc/nginx/uwsgi_params
    /etc/nginx/win-utf
    /etc/sysconfig/nginx
    /etc/sysconfig/nginx-debug
    
    # nginx 的主配置文件
    [root@web01 ~]# ll /etc/nginx/nginx.conf 
    -rw-r--r-- 1 root root 643 Apr 21 23:05 /etc/nginx/nginx.conf
    # nginx 的默认配置文件(删除即可)
    [root@web01 ~]# ll /etc/nginx/conf.d/default.conf 
    -rw-r--r-- 1 root root 1093 Apr 21 23:05 /etc/nginx/conf.d/default.conf
    # nginx 的代理文件
    [root@web01 ~]# ll /etc/nginx/*_params
    -rw-r--r-- 1 root root 1007 Apr 21 23:07 /etc/nginx/fastcgi_params
    -rw-r--r-- 1 root root  636 Apr 21 23:07 /etc/nginx/scgi_params
    -rw-r--r-- 1 root root  664 Apr 21 23:07 /etc/nginx/uwsgi_params
    # nginx 的字符编码文件
    [root@web01 ~]# ll /etc/nginx/*-*
    -rw-r--r-- 1 root root 2837 Apr 21 23:07 /etc/nginx/koi-utf
    -rw-r--r-- 1 root root 2223 Apr 21 23:07 /etc/nginx/koi-win
    -rw-r--r-- 1 root root 3610 Apr 21 23:07 /etc/nginx/win-utf
    # 返回给浏览器 Content Type 的MIME类型
    [root@web01 ~]# ll /etc/nginx/mime.types 
    -rw-r--r-- 1 root root 5231 Apr 21 23:07 /etc/nginx/mime.types
    # nginx 相关命令文件
    [root@web01 ~]# ll /usr/sbin/nginx*
    -rwxr-xr-x 1 root root 1342640 Apr 21 23:07 /usr/sbin/nginx
    -rwxr-xr-x 1 root root 1461544 Apr 21 23:07 /usr/sbin/nginx-debug
    # nginx 日志文件
    [root@web01 ~]# ll /var/log/nginx/*.log
    -rw-r----- 1 nginx adm  588 May 14 15:24 /var/log/nginx/access.log
    -rw-r----- 1 nginx adm 1212 May 14 22:25 /var/log/nginx/error.log
    # nginx 日志切割配置文件
    [root@web01 ~]# ll /etc/logrotate.d/nginx 
    -rw-r--r-- 1 root root 351 Apr 21 23:05 /etc/logrotate.d/nginx
    

    Nginx 主配置文件

    Nginx 的主配置文件即 /etc/nginx/nginx.conf,共划分为三个模块,分别是CoreModule(核心模块)EventModule(事件驱动模块)HttpCoreModule(http内核模块)

    让我们再次传送到一篇详细 Blog !!!

    ######################### CoreModule(核心模块)##############################
    # Nginx进程所使用的用户
    user nginx;           
    # Nginx运行的work进程数量(建议与CPU数量一致或auto)
    worker_processes 1;                  	
    # Nginx错误日志存放路径,以及相应的日志级别(warn,error,crit)
    # 共有 debug,info,notice,warn,error,crit 几个级别,如果是 warn 输出 warn 级别以后的全部级别内容
    # error_log /log/nginx/error.log  notice;  
    # error_log /log/nginx/error.log  info;  
    error_log /log/nginx/error.log warn; 	
    # Nginx服务运行后,进程ID存储文件的位置
    pid /var/run/nginx.pid;         	   
    
    ######################### EventModule(事件驱动模块) ##########################
    events {            
        # 每个worker进程支持的最大连接数
        worker_connections 25535;  
        # 事件驱动模型, 默认为 epoll
        use epoll;                  
    }
    
    ######################### HttpCoreModule(http内核模块) ######################
    http {
        # http 传输的文件类型
        include       /etc/nginx/mime.types;
        # 默认文件类型,当文件类型未定义时使用此类型(text.txt 在浏览器中点击可以直接查看, text 则会被下载 )
        default_type  application/octet-stream;
        # 指定 nginx 日志的输出格式,可以自定义(diy)
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
         log_format  diy  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';                     
        # 指定访问日志的路径,以及日志的输出格式
        access_log  /var/log/nginx/access.log  main;
        access_log  /var/log/nginx/diy_access.log  diy;
        # 开启/关闭 高效文件传输模式
        sendfile        on;
        # 搭配sendfile使用
        #tcp_nopush     on;
        # 客户端连接超时时间(长连接)
        keepalive_timeout  65;
        # 是否开启 gzip 压缩
        #gzip  on;
        # 包含以下目录中所有以.conf结尾的 子配置文件
        include /etc/nginx/conf.d/*.conf;  
    } 
    

    Nginx 虚拟主机配置文件

    # 虚拟主机配置
    server {
        # 监听 80端口
        listen       80;
        # 域名,或者IP
        server_name  localhost;
        # 字符集
        #charset koi8-r;
        # 日志 ( server 中的局部 access_log 配置,若此处配置,主配置文件 nginx.conf 中 HTTP层中的局部 access_log 配置失效)
        #access_log  /var/log/nginx/host.access.log  main;
        # 网站访问的 URL 路径 (根目录)
        location / {
            # 站点目录
            root   /usr/share/nginx/html;
            # 默认页面
            index  index.html index.htm;
            # 日志 ( location 中的局部 access_log 配置,若此处配置,server 中的局部 access_log 配置失效)
            #access_log  /var/log/nginx/host.access.log  main;
        }
        # 网站访问的 URL 路径 (/abc 目录)
        location /abc {
            # 站点目录
            root   /usr/share/nginx/html/abc;
            # 默认页面
            index  abc.html abc.htm;
            # 日志 ( location 子 URL 路径(/abc目录)中的局部 access_log 配置,若此处配置, location URL 父路径(根目录)中的局部 access_log 配置失效)
            #access_log  /var/log/nginx/abc.access.log  main;
        }
        # 404 错误页面的路径
        #error_page  404              /404.html;
        
        # redirect server error pages to the static page /50x.html
        # 5xx 错误页面的路径
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
        # PHP 代理配置
        # 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;
        #}
    }
    
    

    虚拟主机配置方案

    基于IP

    基于服务器主机多 IP 的配置,两种方式:

    • 配置单台服务器多网卡,多 IP 的方式
    • 配置单台服务器单网卡,多 VIP 的方式
    # 配置多网卡(略)
    # 配置单网卡,多 VIP 的方式
    [root@web01 ~]# ifconfig eth0:0 10.0.0.77/24
    [root@web01 ~]# ifconfig eth0:1 10.0.0.88/24
    [root@web01 ~]# ip a s eth0
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:dd:24:04 brd ff:ff:ff:ff:ff:ff
        inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute eth0
           valid_lft forever preferred_lft forever
        inet 10.0.0.77/24 brd 10.0.0.255 scope global secondary eth0:0    <------- 虚拟网卡 eth0:0
           valid_lft forever preferred_lft forever
        inet 10.0.0.88/24 brd 10.0.0.255 scope global secondary eth0:1    <------- 虚拟网卡 eht0:1
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fedd:2404/64 scope link 
           valid_lft forever preferred_lft forever
    
    # 虚拟主机配置文件
    [root@web01 ~]# ll /etc/nginx/conf.d/
    total 12
    -rw-r--r-- 1 root root 122 May 15 11:10 blog.wqh.com.conf
    -rw-r--r-- 1 root root 126 May 15 11:09 download.wqh.com.conf
    -rw-r--r-- 1 root root 124 May 15 11:10 search.wqh.com.conf
    [root@web01 ~]# cat /etc/nginx/conf.d/blog.wqh.com.conf
    server {
        listen 80;
        server_name 10.0.0.7;
        location / {
          root /code/blog;
          index index.html;
        }
    }
    [root@web01 ~]# cat /etc/nginx/conf.d/download.wqh.com.conf
    server {
        listen 80;
        server_name 10.0.0.77;
        location / {
          root /code/download;
          index index.html;
        }
    }
    [root@web01 ~]# cat /etc/nginx/conf.d/search.wqh.com.conf
    server {
        listen 80;
        server_name 10.0.0.88;
        location / {
          root /code/search;
          index index.html;
        }
    }
    # 检查 nginx 格式
    [root@web01 ~]# nginx -t
    # 重新加载 nginx 服务
    [root@web01 ~]# systemctl reload nginx
    

    配置单台服务器单网卡,多 VIP 的方式:

    img

    基于端口

    [root@web01 ~]# cat /etc/nginx/conf.d/blog.wqh.com.conf 
    server {
        listen 81;
        server_name 10.0.0.7;
        location / {
          root /code/blog;
          index index.html;
        }
    }
    [root@web01 ~]# cat /etc/nginx/conf.d/download.wqh.com.conf 
    server {
        listen 82;
        server_name 10.0.0.7;
        location / {
          root /code/download;
          index index.html;
        }
    }
    [root@web01 ~]# cat /etc/nginx/conf.d/search.wqh.com.conf 
    server {
        listen 83;
        server_name 10.0.0.7;
        location / {
          root /code/search;
          index index.html;
        }
    }
    # 重新加载 nginx 服务
    [root@web01 ~]# systemctl reload nginx
    

    基于域名

    # 需要在 C:WindowsSystem32driversetchosts 文件中添加本地域名解析,才可以用网页打开
    [root@web01 ~]# cat /etc/nginx/conf.d/blog.wqh.com.conf 
    server {
        listen 80;
        server_name blog.wqh.com;
        location / {
          root /code/blog;
          index index.html;
        }
    }
    [root@web01 ~]# cat /etc/nginx/conf.d/download.wqh.com.conf 
    server {
        listen 80;
        server_name download.wqh.com;
        location / {
          root /code/download;
          index index.html;
        }
    }
    [root@web01 ~]# cat /etc/nginx/conf.d/search.wqh.com.conf 
    server {
        listen 80;
        server_name search.wqh.com;
        location / {
          root /code/search;
          index index.html;
        }
    }
    # 重新加载 nginx 服务
    [root@web01 ~]# systemctl reload nginx
    

    Nginx 日志管理

    log_format 详解

    # log_format 日志输出格式 语法
    Syntax: log_format name [escape=default|json] string ...;
    Default: log_format combined "...";
    Context: http
    # 主配置文件中默认的 log_format 日志输出格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
    # 各字段含义
    $remote_addr        # 记录客户端IP地址
    $remote_user        # 记录客户端用户名
    $time_local         # 记录通用的本地时间
    $time_iso8601       # 记录ISO8601标准格式下的本地时间
    $request            # 记录请求的方法以及请求的http协议
    $status             # 记录请求状态码(用于定位错误信息)
    $body_bytes_sent    # 发送给客户端的资源字节数,不包括响应头的大小
    $bytes_sent         # 发送给客户端的总字节数
    $msec               # 日志写入时间。单位为秒,精度是毫秒。
    $http_referer       # 记录从哪个页面链接访问过来的
    $http_user_agent    # 记录客户端浏览器相关信息
    $http_x_forwarded_for #记录客户端IP地址
    $request_length     # 请求的长度(包括请求行, 请求头和请求正文)。
    $request_time       # 请求花费的时间,单位为秒,精度毫秒
    # 注:如果Nginx位于负载均衡器,nginx反向代理之后, web服务器无法直接获取到客 户端真实的IP地址。
    # $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中,
    # 增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。
    
    # access_log 日志配置 语法(文件路径[path],日志输出格式[format],缓冲区大小[buffer].......)
    Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
    access_log off;                                     <---------------------    不开启日志
    Default: access_log logs/access.log combined;
    Context: http, server, location, if in location, limit_except
    
    # 日志配置示例
    server {
        listen 80;
        server_name www.wqh.com;
    
        #将当前的server网站的访问日志记录至对应的目录,使用main格式
        access_log /var/log/nginx/www.wqh.com.log main;
        location / {
            root /code/html/;
        }
    
        #当有人请求 favicon.ico 时,不记录日志, favicon.ico 一般是网站标签的图标
        location /favicon.ico {
            access_log off;
            return 200;
        }
    }
    

    nginx 日志切割

    # 查看系统日志切割配置文件
    [root@web01 ~]# cat /etc/logrotate.d/nginx 
    # 日志切割的文件路径
    /var/log/nginx/*.log {
            # 切割日志的周期,此处为 1 天
            daily
            # 忽略丢失日志 ———————— 执行切割日志的过程中,任何错误将被忽略,如果日志不存在,则忽略该日志的警告信息
            missingok
            # 留存的日志的周期数,此处为 52 天
            rotate 52
            # 已经切割的日志,将使用 gzip 进行压缩
            compress
            # 延迟 gzip 压缩,不立即执行压缩
            delaycompress
            # 忽略空文件  ———————— 如果日志文件为空,则不会切割(不做操作)
            notifempty
            # 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件(access.log-xxxxxxxx.gz)
            create 640 nginx adm
            sharedscripts
            # 在所有其它指令完成后,postrotate 和 endscript 里面指定的命令将被执行
            postrotate	
                    if [ -f /var/run/nginx.pid ]; then
            # USR1 通常被用来告知应用程序重载配置文件,此处重载 nginx 守护进程(重新加载 access_log 配置)
                            kill -USR1 `cat /var/run/nginx.pid`
            # 重新加载 access_log 配置后,就会重新创建一个新的日志文件,写入日志内容
                    fi
            endscript
    }
    
    # 其他字段含义
    compress                        # 通过gzip压缩转储以后的日志
    nocompress                      # 不压缩
    copytruncate                    # 用于还在打开中的日志文件,把当前日志备份并截断
    nocopytruncate                  # 备份日志文件但是不截断
    create mode owner group         # 转储文件,使用指定的文件模式创建新的日志文件
    nocreate                        # 不建立新的日志文件
    delaycompress 和 compress       # 一起使用时,转储的日志文件到下一次转储时才压缩
    nodelaycompress                 # 覆盖 delaycompress 选项,转储同时压缩。
    errors address                  # 专储时的错误信息发送到指定的Email 地址
    ifempty                         # 即使是空文件也转储,这个是 logrotate 的缺省选项。
    notifempty                      # 如果是空文件的话,不转储
    mail address                    # 把转储的日志文件发送到指定的E-mail 地址
    nomail                          # 转储时不发送日志文件
    olddir directory                # 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
    noolddir                        # 转储后的日志文件和当前日志文件放在同一个目录下
    prerotate/endscript             # 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
    postrotate/endscript            # 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
    daily                           # 指定转储周期为每天
    weekly                          # 指定转储周期为每周
    monthly                         # 指定转储周期为每月
    rotate COUNT                    # 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
    tabootext [+] list 让logrotate  # 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~ 
    size SIZE                       # 当日志文件到达指定的大小时才转储,bytes(缺省)及KB(sizek)或MB(sizem)
    
    # 日志切割的文件(没有 16号 日志文件(为空),没有切割)
    [root@web01 ~]# ll /var/log/nginx/
    total 36
    -rw-r----- 1 nginx adm 8012 May 17 17:09 access.log
    -rw-r----- 1 nginx adm  234 May 14 15:24 access.log-20200515.gz
    -rw-r----- 1 nginx adm 4299 May 15 11:10 access.log-20200516
    -rw-r----- 1 nginx adm 7213 May 17 17:09 error.log
    -rw-r----- 1 nginx adm  341 May 14 22:25 error.log-20200515.gz
    -rw-r----- 1 nginx adm 2770 May 15 11:10 error.log-20200516
    
  • 相关阅读:
    ActiveMQ JBDC巨坑记录:java.sql.SQLException: Cannot create PoolableConnectionFactory (Communications link failure,Cannot create PoolableConnectionFactory (Communications link failure
    请求https接口时的SSLHandshakeException
    MySQL授权命令grant注意事项
    一次docker服务启动失败的总结
    zabbix-agent和zabbix-agent2的区别,zabbix-agent的主动和被动模式
    MySQL alter修改语句
    zabbix监控tcp的11种状态
    MySQL创建数据库并且插入数据
    MySQL的基础语法
    设置MySQL密码
  • 原文地址:https://www.cnblogs.com/zzzwqh/p/12890624.html
Copyright © 2020-2023  润新知