Nginx史上最强教程,看完醍醐灌顶!
时间:2026-09-12 22:11 来源:未知 人气:
1.认识 Nginx
Nginx 是一个免费开源的、高性能的 Web 和反向代理服务器。
比如我们去请求 www.zhifou.com,Nginx 监听到我们的请求之后,会将对应的服务器资源返回给我们。
Nginx 就像一个菜鸟驿站:
- 你去菜鸟驿站拿快递(访问网址,请求服务器资源)
- 菜鸟驿站员工根据取件码去货架拿快递给你(Nginx根据浏览器路径去请求服务器不同的资源)
当然了 Nginx 不止于“分发快递”这么简单,它还有很多牛逼的功能。我会在后面一一介绍给大家。
2. Linux 安装 Nginx
注:本篇文章的开发环境都是基于 Linux 系统。
# 安装 Nginx
sudo yum install nginx
# 启动 Nginx
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 检查 Nginx 状态
sudo systemctl status nginx
图片
Nginx 默认静态资源文件夹位置:
图片
Nginx 配置文件 nginx.conf 的位置:
图片
nginx.conf:
user nginx;
#Nginx进程,一般设置为和CPU核数一样
worker_processes auto;
#存放错误日志的目录
error_log /var/log/nginx/error.log;
#进程pid存放位置
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
# 单个后台进程的最大并发数
worker_connections 1024;
}
http {
#设置日志模式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#nginx访问日志存放位置
access_log /var/log/nginx/access.log main;
sendfile on; #开启高效传输模式
tcp_nopush on; #减少网络报文段的数量
tcp_nodelay on;
keepalive_timeout 65; #超时时间
types_hash_max_size 2048;
#gzip on; #开启gzip压缩
#包含的子配置项位置和文件
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
root /usr/share/nginx/html; #服务默认启动目录
index index.html index.htm; #默认访问文件
}
#错误状态码的显示页面
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
本文标签: