Nginx builds file sharing server, alias, stub_status monitoring, log, location, 301/302 redirection function

Use nginx server to build website file sharing server

nginx module function: ngx_http_autoindex_module ( autoindex on )

vim www.conf

server {
  listen 80;
  server_name www.baidu.com;
  location / {
    root /html/www;
    #index index.html;
    autoindex on;  #开启nginx站点目录索引功能
  }
}
  • The index file is commented to be useless. You need to delete the index.html file or rename the index.html so that it cannot be found

  • mime.types media resource type file role

    grep jpg /etc/nginx/mime.types
    
    • There are extension information resources in the file, and the data information will be directly seen when accessing
    • The extension information resources that are not in the file will be downloaded directly when accessing
Website page directory data Chinese garbled solution: charset
vim www.conf

server {
  listen 80;
  server_name www.baidu.com;
  location / {
    root /html/www;
    #index index.html;
    autoindex on;  #开启nginx站点目录索引功能
    charset utf-8; #修改目录结构中出现的中文乱码问题
  }
}
Use nginx service configuration file alias function
  • The first journey: writing configuration files

    server_name www.oldboy.com old.com
    
    #old.com就是别名
    
  • The second process: configure the analysis information

    vim /etc/hosts
    10.0.0.7 www.oldboy.com old.com
    
  • effect

    • Write a website access test
    • Locate the web server to be accessed (access a specific one in the cluster)
Use the nginx status module function to monitor the website
  • Write a configuration file

    vim state.conf
    
    server {
      listen 998;
      server_name baidu.com;
      stub_status;
    }
    
  • visit url

    Active connections: 2 
    server accepts handled requests
     58 58 102 
    Reading: 0 Writing: 1 Waiting: 1
    
    information explain
    Active connections Active connections information (how many users are accessing)
    accepts The summary of the number of connections sent (integrated) (TCP)
    can be cleared to 0 after restarting the service
    handled Aggregate Number of Connections Handled (Overall) (TCP)
    requests The total number of requests (HTTP) (one TCP can send multiple HTTPs)
    If you want to set it as a short connection, set keepalive_timeout in the configuration file to 0
    Reading nginx server reads the number of request packets
    Writing The number of nginx server response message information
    Waiting nginx queue mechanism, to be processed (read or respond to the message for storage) monitoring
nginx log function configuration

Log cutting: /etc/logrotate.d/nginx

  • Access log (/var/log/nginx/access.log)

    server {
        log_format ‘...‘
    							 '...'
        access_log  /www/wwwlogs/access.log;
    }
    
    • log_format: Define the log content format (only in http)

    • access_log: call log format (can be placed in the server, not in the location, and can be used for multiple pages of logs)

      parameter explain
      $remote_addr Display user access source ip address information
      $remote_user Display the authenticated username information
      $time_local time of visit
      $request Request line information of the request message
      $status User access status code information
      $body_bytes_sent Display response data size information (statistical traffic)
      $http_referer Record the connection address information for calling website resources (can prevent users from hotlinking, and check when the traffic usage is too much)
      $http_user_agent Record what client the user uses to access the page
      $http_x_forwadrded_for load balancing
    • The log format refers to which options are logged

      默认的日志格式: main
      
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
      
      '$status $body_bytes_sent "$http_referer" '
      
      '"$http_user_agent" "$http_x_forwarded_for"';
      
      如默认的main日志格式,记录这么几项
      
      远程IP- 远程用户/用户时间 请求方法(如GET/POST) 请求体body长度 referer来源信息
      
      http-user-agent用户代理/蜘蛛 ,被转发的请求的原始IP
      
      http_x_forwarded_for:在经过代理时,代理把你的本来IP加在此头信息中,传输你的原始IP
      
      2: 声明一个独特的log_format并命名
      
      log_format  mylog '$remote_addr- "$request" '
      
      '$status $body_bytes_sent "$http_referer" '
      
      '"$http_user_agent" "$http_x_forwarded_for"';
      
      在下面的server/location,我们就可以引用 mylog
      
      在server段中,这样来声明
      
      Nginx允许针对不同的server做不同的Log ,(有的web服务器不支持,如lighttp)
      
      access_log logs/access_8080.log mylog;
      
      声明log   log位置          log格式;
      
      
  • Error log (/var/log/nginx/error.log)

    • edit configuration file

      errror_log /var/log/nginx/error.log error
      
    • Error level (from top to bottom, the higher the level, the lower the setting, the more information. It is recommended to set error)

      level explain
      debug Debug level, service running status information and error information detailed display
      info Information level: only important running information and error information are displayed
      notice Notification level: more important information for notification instructions
      warn Warning level: There may be some error messages, but it does not affect the service operation
      error Error level: The service has encountered an error and needs to be corrected
      crit Severity: Modification required
      alert Severe warning level: that is, warning, and do not fix errors
      emerg Disaster level: the service is no longer functioning normally
  • PS: The log file information needs to be cut and processed

Description of the role of nginx service location

Belonging module: ngx_http_core_module

location to match (url)

  • The error page displays gracefully:

    location / {
      root /html/www;
      error_page 404 oldboy.jpg;
    }
    
    #oldboy.jpg要在/html/www里
    
  • Exact match (highest priority 01)

    location = / {
    	return 404;  #返回状态码
    }
    
  • Default match (if none of the others match, this is the match, with the lowest priority)

    location / {
    	return 403;
    }
    
  • Match by directory (priority 03)

    location /documents {
    	return 501;
    }
    
  • Priority matching (priority 02), not the symbol information in the other uri information, no need to escape

    location ^~ /images {
    	return 502;
    }
    
  • Case-insensitive matching (priority 03, matching with three suffixes)

    location ~* \.(gif|jpg|jpeg)$ {
    	return 500;
    }
    
  • case sensitive

    location ~ \.(gif|jpg|jpeg)$ {
    	return 500;
    }
    
Use nginx to realize page jump function

Module: http_rewrite_module

  • Instructions:rewrite 匹配的正则信息 替换成什么信息

  • configure

    server {
      listen 456;
      server_name linglong.fun;
      rewrite ^/(.*) http://linglong.fun/$1 permanent;
    }
    
  • Rewrite rule configuration

    rewrite ^/(.*) http://linglong.fun/$1 permanent;
    
    ^/:url
    (.*):uri
    
    $0:url
    $1:uri
    
    • Permanent jump: permanent 301. Jump information will be cached
    • Temporary jump: redirect 302. Jump information will not be cached
  • How to solve the infinite jump:

    • Use different server block configurations

      server {
        listen 80;
        server_name oldboy.com;
        rewrite ^/(.*) http://www.oldboy.com/$1 permanent;
      }
      server{
        listen 80;
        server_name www.oldboy.com;
        access_log /var/log/nginx/www_access/log main;
        location / {
          root /html/www;
          index index.html;
        }
      }
      
    • Use if to judge to break the loop and only jump to oldboy.com

      server{
        listen 80;
        server_name www.oldboy.com;
        if ($host ~* "^oldboy.com$"){
          rewrite ^/(.*) http://www.oldboy.com/$1 permanent;
        }
        access_log /var/log/nginx/www_access/log main;
        location / {
          root /html/www;
          index index.html;
        }
      }
      
jump directly
server {
  listen 80;
  server_name oldboy.com;
  return 301 http://www.baidu.com;
}

Guess you like

Origin blog.csdn.net/s_frozen/article/details/128940403