Nginx-学习目录
1、配置目录结构
1.1、查询安装的nginx主要有哪些目录
]# rpm -ql nginx
/etc/logrotate.d/nginx
...
1.2、配置文件介绍
1.2.1、Nginx主配置文件
/etc/nginx/nginx.conf nginx主配置文件
/etc/nginx/conf.d/default.conf 默认网站配置文件
1.2.2、Nginx代理相关参数文件
/etc/nginx/fastcgi_params Fastcgi代理配置文件
/etc/nginx/scgi_params scgi代理配置文件
/etc/nginx/uwsgi_params uwsgi代理配置文件
1.2.3、Nginx编码相关配置文件
/etc/nginx/mime.types Content-Type与扩展名
/etc/nginx/win-utf Nginx编码转换映射文件
/etc/nginx/koi-utf Nginx编码转换映射文件
/etc/nginx/koi-win Nginx编码转换映射文件
1.2.4、Nginx管理相关命令
/usr/sbin/nginx Nginx命令行管理终端工具
/usr/sbin/nginx-debug Nginx命令行与终端调试工具
1.2.5、Nginx日志相关目录与文件
/var/log/nginx Nginx默认存放日志目录
/etc/logrotate.d/nginx Nginx默认的日志切割
2、Nginx基本配置
Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。
2.1、CoreModule-核心模块
2.1.1、全局参数
user nginx; # Nginx进程所使用的用户
worker_processes auto; # Nginx运行的work进程数量(建议与CPU数量一致或auto)
error_log /var/log/nginx/error.log notice; # Nginx错误日志存放路径
pid /var/run/nginx.pid; # Nginx服务运行后产生的pid进程号
2.2、EventModule-事件驱动模块
2.2.1、events-事件模块
events {
worker_connections 1024; # 每个worker进程支持的最大连接数
use epoll; # 事件驱动模型,epoll默认
}
2.3、HttpCoreModule-http内核模块
2.3.1、http-内核模块
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; # 先将数据缓存到缓存区,存满后再发
keepalive_timeout 65; # 链接超时时间
#gzip on; # 是否开启压缩
include /etc/nginx/conf.d/*.conf; # 包含/etc/nginx/ conf.d/目录下所有以.conf结尾的文件。
}
2.3.2、http->server-内核模块
server { # 使用Server配置网站,每个Server{}代表一个网站(简称虚拟主机)
listen 80; # 监听端口:80
server_name localhost; # 提供的域名
access_log /var/log/nginx/host.access.log main; # 网络访问的日志
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;
}
}
2.4、Nginx中的http、server、location之间的关系
http : 标签主要用来解决用户的请求与响应。
server : 标签主要用来响应具体的某一个网站。
location : 标签主要用于匹配网站具体URL路径。
http{}层下允许有多个Server{},一个Server{}下,又允许有多个location{}
3、Nginx搭建网站-入门示例
3.1、准备项目代码
# 使用简单的代码演示
mkdir /opt/project1
echo "project1 page" >/opt/project1/index.html
chown nginx -R /opt/project1
3.2、配置nginx【增加一个server 】
cat >/etc/nginx/conf.d/test.cyc.com.conf<<'EOF'
server{
listen 80;
server_name test.cyc.com;
location / {
root /opt/project1;
index index.html;
}
}
EOF
3.3、检查配置语法是否正常
~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
3.4、重载nginx服务
3.4.1、配置hosts
192.168.10.101 test.cyc.com
3.4.2、测试访问
~]# curl test.cyc.com
project1 page