一、主配置文件

[root@k8s03 ~]# cat /etc/nginx/nginx.conf
user nginx; ##运行nginx程序的独立账号
worker_processes auto; ##启用的work进程数量(CPU数量一致或auto)
error_log /var/log/nginx/error.log notice; ##错误日志存放位置
pid /var/run/nginx.pid;
events { ##事件
use epoll; ##事件驱动模型epoll(默认)
worker_connections 1024; ##每个worker进程允许处理的最大连接数,例如10240,65535
}
http {
include /etc/nginx/mime.types; ##文档和程序的关联记录
default_type application/octet-stream; ##字节流处理方式
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 /var/log/nginx/access.log main;
sendfile on; ##优化参数,高效传输文件的模式
#tcp_nopush on; ##优化参数,tcp_nopush=on会设置调用tcp_cork方法,这个也是默认的,,结果就是数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞。
keepalive_timeout 65; ##优化参数,长连接
#gzip on; ##压缩参数
include /etc/nginx/conf.d/*.conf; ##包含子配置文件夹
}

二、说明

1、分类

CoreModule 核心模块(进程数等)
EventsModule 事件驱动模块(工作模式等)
HttpCoreModule http内核模块(文档程序类型,配置文件等)

模块功能:
1、全局/核心块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
5、location块:配置请求的路由,以及各种页面的处理情况。

2、虚拟主机配置文件

[root@k8s03 ~]# cat /etc/nginx/conf.d/default.conf
server {
listen 80; ##监听端口
server_name localhost; ##FQDN
#charset koi8-r; ##网页字符类型
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; ##主目录
index index.html index.htm; ##默认主页名
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html { ##错误页面
root /usr/share/nginx/html; ##错误页面主目录
}
# 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;
#}
}

三、配置新的虚拟主机

vi /etc/nginx/conf.d/http.conf
server {
listen 80;
server_name qingchen.com;
location / {
root /qingchen;
index index.html;
}
}

mkdir /qingchen
echo "this is a test" >/qingchen/index.html

vi /etc/hosts
192.168.10.133 qingchen.com

nginx -t ##检查nginx语法是否正确
systemctl restart nginx

curl http://qingchen.com
this is a test