(004)Nginx默认配置语法解析及演示

  1、基础配置

  Nginx的主配置文件:/etc/nginx/nginx.conf,当读到下面include一行会加载/etc/nginx/conf.d下,以 *.conf结尾的文件。

  user:设置nginx服务的系统使用用户

  worker_processes:工作进程数,最好设置为与cpu个数一致

  error_log:nginx的错误日志

  pid:nginx服务启动时候pid

  events.worker_connections:每个进程允许最大连接数,可以调到65535,一般调节到10000左右 

  events.use:定义内核模型

  keepalive_timeout:客户端与服务端超时时间,单位秒

  2、常用配置

  常用配置在 /etc/nginx/conf.d/default.conf 中,第二个文本框列出了主要部分

vim /etc/nginx/conf.d/default.conf
http{
  ... ...
  server{
    listen 80;
    server_name localhost;

    location / {
      root /usr/share/nginx/html;
      index index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html{
      root /usr/share/nginx/html;
    }
  }

  server{
    ... ...
  }
}

  最外层是 http,一个 http下可以有多个 server,一个 server下可以有多个 location;

  上面的 location / 定义了首页的位置是 /usr/share/nginx/html 里面的 index.html,如果没有 index.html 则访问 index.htm;

  error_page 定义了当返回码是500/502/503/504时,跳转到 50x.html,该页面的路径是 /usr/share/nginx/html/50x.html;

  3、简单演示

  启动nginx

systemctl start nginx.service

  输入ip访问:http://192.168.7.151

  

  访问的是 /usr/share/nginx/html/index.html 页面,下面修改一下,重新访问:

  

  

  修改配置文件:/etc/nginx/conf.d/default.conf ,增加404的返回码,映射到50x.html

vim /etc/nginx/conf.d/default.conf

  

  

  重启服务,并访问:http://192.168.7.151/ssssss.html

systemctl reload nginx.service

  

  

原文地址:https://www.cnblogs.com/javasl/p/12818115.html