zoukankan      html  css  js  c++  java
  • Nginx详解

    Nginx功能模块汇总

    
    --with-http_core_module  #包括一些核心的http参数配置,对应nginx的配置为http区块部分
    --with-http_access_module  #访问控制模块,用来控制网站用户对nginx的访问
    --with-http_gzip_module  #压缩模块,nginx返回的数据压缩,属于性能优化模块
    --with-http_fastcgi_module  #FastCGI模块,和动态应用相关的模块,例如PHP
    --with-http_proxy_module  #proxy代理模块
    --with-http_upstream_module  #负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查
    --with-http_rewrite_module  #URL地址重写模块
    --with-http_limit_conn_module  #限制用户并发连接及请求数模块
    --with-http_limit_req_module  #根据定义的key限制nginx请求过程的sulv
    --with-http_log_module  #请求日志模块,以制定的个事记录nginx客户访问日志的信息
    --with-http_auth_basic_module  #web认证模块,设置web用户通过账号、密码访问nginx
    --with-http_ssl_module  #ssl模块,用于加密的http连接,如https
    --with-http_stub_status_module  记录nginx基本访问状态信息等的模块
    

    Nginx的目录结构说明

    
    conf   #这是nginx所有配置文件的目录
      fastcgi.conf            #fastcgi相关参数的配置文件
      fastcgi.conf.default    #fastcgi.conf的原始备份
      fastcgi_params       #fastcgi的参数文件
      fastcgi_params.default   
      koi_utf
      koi_win
      mime.types  #媒体类型
      mime.types.defualt
      nginx.conf  #Nginx默认的配置文件
      nginx.conf.default
      scgi_params  #scgi相关参数文件
      uwsgi_params  #uwsgi相关参数配置文件
    fastcgi_temp   #fastcgi临时配置文件
    html  
    logs  #默认的日志路径
      access.log  #默认访问日志文件
      error.log
      nginx.pid
    proxy_temp   #临时文件
    sbin       #nginx的命令目录
      nginx  #nginx的启动命令
    

    Nginx的配置文件说明

    cat nginx.conf.defaultuser  www www;worker_processes  2;#worker进程的数量
    
    pid        logs/nginx.pid;
    events {        #事件区块开始
        use epoll;
        worker_connections  2048;  #每个worker进程支持的最大连接数
    }
    
    http {        #http区块开始
        include       mime.types;  #ngninx支持的媒体类型库文件
        default_type  application/octet-stream;  #默认的媒体类型
        sendfile        on;        #开启高效传输模式
        keepalive_timeout  65;    #连接超时
    
      # 很重要的虚拟主机配置
        server {        #第一个server区块开始
            listen       80;  
            server_name  itoatest.example.com;    #提供服务的域名主机名ip/domain
    
            charset utf-8;
    
            location / {
            root   /apps/oaapp;      #站点的根目录
         index  index.html  index.htm;  #默认首页文件
         }   
        error_page  500  502  504  504  /50x.html #出现对应的http状态码时,使用50x。html回应客户
        local  = /50x.html{
          root html;
        }
    } 
    

     Nginx虚拟主机配置

     1.基于域名的nginx.conf配置文件

    
        server {  
            listen       80;  
            server_name  www.abc.com;  
      
            location / {  
                root   html/www;  
                index  index.html index.htm;  
            }  
        server {  
            listen       80;  
            server_name  blog.abc.com;  
      
            location / {  
                root   html/blog;  
                index  index.html index.htm;  
            }  
        server {  
            listen       80;  
            server_name  bbs.abc.com;  
      
            location / {  
                root   html/bbs;  
                index  index.html index.htm;  
            } 

      2.基于端口的虚拟主机

    
        server {  
            listen       80;  
            server_name  www.abc.com;  
      
            location / {  
                root   html/www;  
                index  index.html index.htm;  
            }  
        server {  
            listen       81;  
            server_name  blog.abc.com;  
      
            location / {  
                root   html/blog;  
                index  index.html index.htm;  
            }  
        server {  
            listen       82;  
            server_name  bbs.abc.com;  
      
            location / {  
                root   html/bbs;  
                index  index.html index.htm;  
            } 
    

     3.基于ip的虚拟配置

    
        server {  
            listen       10.0.0.1:80;  
            server_name  www.abc.com;  
      
            location / {  
                root   html/www;  
                index  index.html index.htm;  
            }  
        server {  
            listen       10.0.0.2:81;  
            server_name  blog.abc.com;  
      
            location / {  
                root   html/blog;  
                index  index.html index.htm;  
            }  
        server {  
            listen       10.0.0.3:82;  
            server_name  bbs.abc.com;  
      
            location / {  
                root   html/bbs;  
                index  index.html index.htm;  
            } 
    

      

    Nginx的规范优化配置文件

    主文件包含的所有虚拟主机的子配置文件会统一放在extra目录中。虚拟主机的配置文件按照网站的域名或功能名称取名,例如www.conf等

    
    events {        #事件区块开始
        use epoll;
        worker_connections  2048;  #每个worker进程支持的最大连接数
    }
    http {       #http区块开始
        include       mime.types;  #ngninx支持的媒体类型库文件
        default_type  application/octet-stream;  #默认的媒体类型
        sendfile        on;        #开启高效传输模式
        keepalive_timeout  65;       #连接超时
      
      include extra/www.conf;
      include extra/bbs.conf;
    
    
    } 
    
    
    [root@www conf]# cat extra/www.conf
        server {  
            listen       80;  
            server_name  www.abc.com;  
      
            location / {  
                root   html/www;  
                index  index.html index.htm;  
            } 
         

    location里面设置允许和禁止的ip段访问

    
    location /nginx_status{
            stub_status on;      #打开状态信息开关
            access_log off;
            allow 10.0.0.0/24;  #设置允许和禁止的IP段访问
            deny all;           #设置允许和禁止的ip段访问
    
        
    }
    

    Nginx错误日志配置

            错误日志常见的级别【debug|info|notice|warn|error|crit|alert|emerg】

            生存环境通过是warn|error|crit,注意不要配置info等较低级别,会损坏巨大磁盘IO

    通常配置如下:

    
    worker_processes 1;
    error_log    logs/error.logs   error;    #就在这里配置
    events    {
    .....
    }
    

    Nginx访问日志

    1.访问日志两个参数

            log_format  用来定义记录日志格式(可以定义多种日志个事,取不同名字)

            access_log  用来制定日志文件的路径和使用何种日志格式记录日志

    2.访问日志配置说明

            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         #记录访问网站的客户端地址
    $http_x_forwarded_for#当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的
    $remote_user         #远程客户端用户名称
    $time_local          #记录访问时间与时区
    $request             #用户的http请求其实行信息
    $status              #http状态码,记录请求返回的状态,例如200,404,301等
    $body_bytes_sents    #服务器发送给客户端的响应body字节数
    $http_referer        #记录此次请求是从那个链接访问过来的,可以根据referer进行防盗链设置
    $http_user_agent     #记录客户端访问信息。例如浏览器,手机客户端等
    

            access_log

     语法如下:

    
    access_log path [format [buffer=size [flush=time]] [if=condition]];
    access_log path format gzip[=level] [buffer=size] [flush=time][if=codition];
    access_log syslog:server=address[,parameter=value] [format [if=condition]];
    

    buffer=size  为存放访问日志的缓冲区大小

    flush=time   为将缓冲区的日志刷到磁盘的时间

    gzip[=level]  表示压缩级别

    [if=condition] 表示其他条件

    access_log off  表示不记录访问日志

    一般情况无须配置,极端优化才考虑

    样例

    
    events {        #事件区块开始
        use epoll;
        worker_connections  2048;  #每个worker进程支持的最大连接数
    }
    http {            #http区块开始
        include       mime.types;                  #ngninx支持的媒体类型库文件
        default_type  application/octet-stream;    #默认的媒体类型
        sendfile        on;                        #开启高效传输模式
        keepalive_timeout  65;                     #连接超时
      
    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;
    } 
    
    [root@www conf]# cat extra/www.conf
        server {  
            listen       80;  
            server_name  www.abc.com;  
      
            location / {  
                root   html/www;  
                index  index.html index.htm;  
            }  
      access_log logs/access_www.log main;
    
    }
    

      如果在高并发场景下提升网站的访问性能,可以加入buffer和flush选项

    
    access_log logs/access_www.log main gzip buffer=32k flush=5s;
    

     然后重启服务

     博客搬运地址

  • 相关阅读:
    nice -n 10 bash 和 chrt 10 bash 和 echo -17 > /proc/PID/oom_score_adj
    使用NGINX+LUA实现WAF功能 和nginx 防盗链
    hdfs 通过命令坏块监测和删除或者地址获取参数做监控
    kafka 的server.properties
    ntpd服务
    kafka笔记博客
    k8s高可用
    K8S集群Master高可用实践
    String:字符串常量池
    如何设计出优秀的Restful API?
  • 原文地址:https://www.cnblogs.com/clement-jiao/p/9384813.html
Copyright © 2011-2022 走看看