一,Nginx基本介绍
1,nginx概念
Nginx 是一款轻量级、高性能的服务器软件,核心能力是 “处理网络请求”,被广泛用于网站、App 的后端架构中。
Nginx 就像一个 “高效的网络交通指挥官”,核心价值是用最少的资源,处理最多的请求,让整个系统又快又稳。无论是小网站还是像淘宝、抖音这样的巨头,几乎都离不开它 —— 它是现代互联网架构的 “基础设施” 之一。
(与浏览器为两端
2,nginx优势
- 高并发:事件驱动模型 + 单线程 Worker 进程,单台服务器可支撑数万并发连接;
- 高可靠:Master-Worker 进程隔离,Worker 故障自动重启,服务可用性高;
- 高性能:非阻塞 I/O 减少等待,内存占用低(通常几 MB 到几十 MB);
- 高扩展:模块化设计支持按需扩展,第三方模块丰富;
- 易运维:支持平滑重载配置、版本升级,运维操作不中断服务。
二,nginx主配置文件
/usr/local/nginx/conf/nginx.conf
[root@web1 conf]# cat nginx.conf
1,基本信息
#user nobody; 使用的nginx账号
worker_processes 1; 工作进程数量(看CPU核数量
#error_log logs/error.log; 错误日志配置
#error_log logs/error.log notice; notice级别
#error_log logs/error.log info; info级别
#pid logs/nginx.pid; pid文件路径
events { }事件模块
events {
worker_connections 1024; 最大并发数 1024
}
2,http模块
http { }模块,处理HTTP协议
http {
include mime.types; 引入MINE类型映射文件
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 logs/access.log main; 访问日志路径
sendfile on; 开启 sendfile 机制(零拷贝技术),加速静态文件(如图片、HTML)的传输效率
#tcp_nopush on; 网络传输次数
#keepalive_timeout 0; 关闭长连接
keepalive_timeout 65; 65秒长连接
#gzip on; GZIP压缩
server{ } 模块,在http模块 虚拟主机配置
server {
listen 80; 监听的端口
server_name localhost; 匹配的域名
#charset koi8-r; 字符集配置
#access_log logs/host.access.log main; 该虚拟主机的访问日志
location / { 匹配的根路径
root html; 网站根目录
index index.html index.htm; 默认首页文件
}
#error_page 404 /404.html; 404错误跳转页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; 服务错误(500/501/502/503/504
location = /50x.html { 精确·匹配 /50x.html页面
root 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
# PHP 相关配置,对接PHP服务
#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
# 禁止访问 .htaccess 文件(默认注释,若与 Apache 共用目录时启用)
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
3,https模块
HTTPS 服务示例
# HTTPS server
#
#server {
# listen 443 ssl; 监听 443 端口 (HTTPS默认端口 并启用SSL
# server_name localhost;
# ssl_certificate cert.pem; SSL 证书文件(公钥
# ssl_certificate_key cert.key; SSL 私钥文件
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
三,nginx的目录结构
[root@dns-nfs conf]# cd /usr/local/nginx8
[root@dns-nfs nginx8]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@dns-nfs nginx8]#
1,client_body_temp
:客户端请求体临时存储目录
2,conf:核心目录
root@load2 conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@load2 conf]#
nginx.conf Nginx 主配置文件
mine.types MIME 类型映射文件
koi-utf win 字符集转换映射表
(.default是默认备份文件
3,html 默认静态资源根目录
(快速部署静态服务)
[root@load2 nginx8]# cd html
[root@load2 html]# ls
50x.html index.html
[root@load2 html]#
index.html nginx 默认欢迎页,内容为 “Welcome to nginx!”,是 Nginx 安装完成后,访问 http://服务器IP
或 http://localhost
时默认返回的页面;
50x.html nginx默认错误页面
(可以在nginx主配置文件server模块修改
server {listen 80;server_name www.my-static.com; # 自定义域名root /var/www/my-static; # 自定义静态资源目录(需手动创建)index index.html index.htm; # 默认首页优先级
}
4,logs 日志核心目录
[root@load2 logs]# ls
access.log error.log nginx.pid
[root@load2 logs]#
access.log 访问日志
error.log 错误日志
nginx.pid 进程 ID 文件(管理 Nginx 服务) pi进程标识符
5,sbin
[root@load2 nginx8]# cd sbin
[root@load2 sbin]# ls
nginx
[root@load2 sbin]#
编译安装的
.nginx启动服务