iMisty的技术栈

iMisty的技术栈

nginx.conf核心配置文件

937
2020-06-30
  1. 设置worker进程的用户,指的linux中的用户,会涉及到nginx操作目录或文件的一些权限,默认为nobody user root;

  2. worker进程工作数设置,一般来说CPU有几个,就设置几个,或者设置为N-1也行worker_processes 1;

  3. nginx 日志级别debug | info | notice | warn | error | crit | alert | emerg,错误级别从左到右越来越大

  4. 设置nginx进程 pid ,pid logs/nginx.pid;

  5. 设置工作模式

events {
    # 默认使用epoll
    use epoll;
    # 每个worker允许连接的客户端最大连接数
    worker_connections  10240;
}
  1. http 是指令块,针对http网络传输的一些指令配置

    http {
    ...
    }
    
  2. include 引入外部配置,提高可读性,避免单个配置文件过大 include mime.types;

  3. 设定日志格式,main为定义的格式名称,如此 access_log 就可以直接使用这个变量了

 #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;
参数名参数意义
$remote_addr客户端ip
$remote_user远程客户端用户名,一般为:’-’
$time_local时间和时区
$request请求的url以及method
$status响应状态码
$body_bytes_send响应客户端内容字节数
$http_referer记录用户从哪个链接跳转过来的
$http_user_agent用户所使用的代理,一般来时都是浏览器
$http_x_forwarded_for通过代理服务器来记录客户端的ip
  1. sendfile使用高效文件传输,提升传输性能。启用后才能使用tcp_nopush,是指当数据表累积一定大小后才发送,提高了效率。

    sendfile        on;
    tcp_nopush      on;
    
  2. keepalive_timeout设置客户端与服务端请求的超时时间,保证客户端多次请求的时候不会重复建立新的连接,节约资源损耗。

    #keepalive_timeout  0;
    keepalive_timeout  65;
    
  3. gzip启用压缩,html/js/css压缩后传输会更快 gzip on;

  4. server可以在http指令块中设置多个虚拟主机

  • listen: 监听端口
  • server_name: localhost、ip、域名
  • location: 请求路由映射,匹配拦截
  • root : 请求位置
  • index : 首页设置
    server {
            listen       88;
            server_name  localhost;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    }