01. Nginx 简介
Nginx 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
Nginx 的特点:
- 高并发处理能力(事件驱动架构)
- 内存占用低
- 配置简洁
- 反向代理和负载均衡
- 静态文件服务高效
02. 安装与基本操作
bash
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install nginx
# CentOS/RHEL
sudo yum install epel-release
sudo yum install nginx
# 基本操作
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl status nginx
sudo nginx -t # 测试配置
sudo nginx -s reload # 重新加载03. 配置文件结构
nginx
# /etc/nginx/nginx.conf 主配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
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"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
# 引入站点配置
include /etc/nginx/conf.d/*.conf;
}04. 虚拟主机配置
nginx
# /etc/nginx/conf.d/example.conf
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# 静态文件缓存
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}知识测验
第 1/4 题正确 0
nginx -t 命令的作用是?
下一节
下一节 静态文件服务