nginx配置网站服务
nginx配置网站服务
- 一、http协议介绍
- 1、网站类型
- 2、涉及的软件
- 3、http协议介绍
- 二、nginx安装、启动
- 1、nginx介绍
- 2、nginx安装
- 3、nginx启动管理
- 三、nginx配置文件
- 1、配置文件语法结构
- 2、全局配置
- 3、事件驱动模型的配置
一、http协议介绍
1、网站类型
-
静态网站
内容是固定的,任何用户访问看到的内容是一样的
开发语言: html, jquery, js, div+css
网页文件: xxxx.html -
动态网站
一段程序代码,根据传递的参数不同返回不同的结果
开发语言:
PHP, xxxxx.php
JAVA, xxxxx.jsp
2、涉及的软件
- httpd
- nginx
- tomcat
3、http协议介绍
http, 明文, 超文本传输协议
https, 密文
-
http/0.9
仅支持传输纯文本数据 -
http/1.0
引入MIME机制,支持传输非文本数据(图片、视频、音频、动画)
引入缓存机制,提升IO速度 -
http/1.1
引入长连接(keepalive)机制,提升速度, 限制长连接的超时时间、最大请求数
引入管道机制,提升速度, 支持同时发送多个请求
增强缓存管理(静态数据、热点数据、过期时间) -
http/2
改进管道机制,支持请求、响应同时发送
二、nginx安装、启动
1、nginx介绍
跨平台、模块化
高并发 C10K、高性能
支持epoll(通知机制)事件驱动模型
2、nginx安装
- 下载nginx安装包
[root@node01 ~]# wget https://nginx.org/download/nginx-1.26.2.tar.gz
- 安装依赖
[root@node01 ~]# yum install -y gcc openssl-devel zlib-devel pcre-devel
- 编译安装nginx
[root@node01 ~]# tar xf nginx-1.26.2.tar.gz
[root@node01 ~]# cd nginx-1.26.2/
[root@node01 nginx-1.26.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
[root@node01 nginx-1.26.2]# make
[root@node01 nginx-1.26.2]# make install
- nginx核心目录
安装目录/sbin:nginx命令
安装目录/conf:存放配置文件,主配置文件nginx.conf
安装目录/logs: 存放日志,访问日志、错误日志
安装目录/html: 默认网页目录
3、nginx启动管理
- 启动nginx
[root@node01 ~]# /usr/local/nginx/sbin/nginx
[root@node01 ~]# netstat -tunlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7975/nginx: master
[root@node01 ~]# ps -elf | grep nginx
1 S root 7975 1 0 80 0 - 11502 sigsus 14:24 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nobody 7976 7975 0 80 0 - 11614 ep_pol 14:24 ? 00:00:00 nginx: worker process
主进程:负责读取配置文件、记录日志、派生子进程
工作进程:接收、处理客户端请求
- 开机自启动
[root@node01 ~]# vim /etc/rc.d/rc.local
/usr/local/nginx/sbin/nginx
[root@node01 ~]# chmod a+x /etc/rc.d/rc.local
- 关闭nginx
[root@node01 ~]# /usr/local/nginx/sbin/nginx -s stop
- 重新加载配置文件
[root@node01 ~]# /usr/local/nginx/sbin/nginx -s reload
- 检测配置文件语法
[root@node01 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- 查看nginx版本、安装参数
[root@node01 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.26.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
三、nginx配置文件
1、配置文件语法结构
全局配置
事件驱动模型
events {
}
http的配置
http {
server {
location {
}
location {
}
}
server {
}
}
server {}:代表一个虚拟主机,虚拟主机支持在同一个服务器部署多套网站
location{}:用于匹配客户端的访问请求,根据不同的请求做不同的处理
2、全局配置
- 指定工作进程的用户
user nobody;
- 指定工作进程的数量
worker_processes 8;
建议和CPU数量一致,或两倍
- 定义错误日志、级别
error_log logs/error.log notice;
支持的级别: debug, info, notice, warn, error, crit, alert, or emerg
- 定义pid文件
pid logs/nginx.pid;
3、事件驱动模型的配置
events {
use epoll;
worker_connections 4096; // 每个工作进程处理的最大连接数
}
注意:nginx要运行BSD系列的Linux上,需要修改为use kqueue;