当前位置: 首页 > news >正文

nginx保姆及教学

一、概览与核心概念

NGINX 是高性能的反向代理、负载均衡器、HTTP 静态服务器与 TCP/UDP(stream)代理。它以事件驱动、异步非阻塞著称,适合高并发场景。
nginx.org

配置文件以 nginx.conf 为入口,常见结构:全局指令 → events → http(或 stream)→ server → location。全局还能 load_module 动态模块。

二、常用命令

nginx -t # 配置测试

nginx -s reload # 平滑重载(不丢失连接)

systemctl start nginx
systemctl enable nginx

nginx -V # 查看编译参数和模块

nginx完整配置

点击查看代码
# nginx.conf - 完整示例(注释详尽)
user  nginx;
worker_processes auto;         # 工作进程数量配置 建议 auto(等同 CPU core 数) 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;# 全局 worker 级别事件配置
events {use epoll;                 # Linux 推荐 use epoll  高性能事件驱动机制处理网络请求。worker_connections  10240; # 每 worker 最大连接数(和 ulimit / sysctl 协同)multi_accept on;   #一次性把所有等待的 accept 队列全部接收
}# 全局 http 配置
http {include       /etc/nginx/mime.types;    #作用:告诉 NGINX 使用哪个文件来映射文件类型default_type  application/octet-stream;     #当 NGINX 无法从 mime.types 中找到匹配的文件类型时,使用这个默认 MIME 类型。sendfile        on;             # 零拷贝    机制,将文件直接从磁盘传输到网络,不经过用户空间。tcp_nopush      on;             # 延迟发送 TCP 包,把头和内容合并发 大文件下载、sendfile 静态文件tcp_nodelay     on;             # 立即发送 TCP 包,关闭 Nagle WebSocket、低延迟 API、RPCkeepalive_timeout  65;          # 客户端连接保持的时间types_hash_max_size 2048;       # # 日志格式log_format  main  '$remote_addr - $remote_user [$time_local]  ''"$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent" "$request_time"';access_log  /var/log/nginx/access.log  main;error_log   /var/log/nginx/error.log warn;# 缓存(proxy_cache)路径与 zoneproxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=one:100m max_size=10g inactive=60m use_temp_path=off;# 代理连接池到上游upstream backend_app {least_conn;server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;keepalive 32; # 与 upstream keepalive 合理搭配}# HTTP/2 + TLS 推荐(示例)server {listen 443 ssl http2;server_name example.com www.example.com;ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;   # ssl证书的crt的位置ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;   #ssl证书key的位置# 使用 Mozilla 推荐配置(见文档)ssl_protocols TLSv1.2 TLSv1.3;   #指定允许的 TLS 协议版本ssl_ciphers 'TLS_AES_...'; # 按 Mozilla 推荐生成ssl_prefer_server_ciphers off;ssl_session_cache shared:SSL:10m;add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;# 反向代理示例(含缓存)location / {proxy_pass http://backend_app;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_read_timeout 90;proxy_connect_timeout 5s;proxy_send_timeout 30s;proxy_cache one;proxy_cache_valid 200 302 10m;proxy_cache_valid 404 1m;proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;add_header X-Proxy-Cache $upstream_cache_status;}# WebSocket 支持(保持 Upgrade 头)location /ws/ {proxy_pass http://backend_app;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "Upgrade";proxy_set_header Host $host;}# 上传大小client_max_body_size 50m;}# HTTP -> HTTPS 重定向server {listen 80;server_name example.com www.example.com;return 301 https://$host$request_uri;}# stream 块请在主配置中单独定义(见下文 stream 示例)
}
http://www.gsyq.cn/news/79685.html

相关文章:

  • 2025 激光焊接机权威榜单出炉!10 大厂家硬核 PK,国产化技术领跑全球
  • Xcode16
  • 大学生必备 APP 清单,错过血亏!
  • 留学必备APP全攻略:从学习到生活,这几款神器让你轻松适应海外生活
  • 在.NET中实现一库多租户(Single Database Multi-Tenancy)模式,主要通过共享数据库但隔离数据的方式实现。
  • 学习机大揭秘:哪个品牌才是孩子的最佳拍档?
  • 权威解析:十大留学机构深度评测与2025精英选择指南
  • VMware 等企业软件固件下载
  • 市场变天了!2025 选学习机别只看大牌,这两个新趋势要抓住
  • 可对话的赛博分身:用 Claude Code 分析 GitHub 日记
  • 2025年进口电动蒸汽截止阀制造企业权威推荐榜单:进口气动蒸汽球阀‌/进口蒸汽截止阀‌/进口自力式蒸汽调节阀源头厂家精选
  • 重练算法(代码随想录版) day35 - 动态规划part3
  • 2025 天线厂家 TOP10 推荐:科普选型指南,靠谱品牌助力通信升级
  • 2025年四川小程序开发方案权威推荐榜单:小程序平台/小程序定制/商城小程序方案服务商精选
  • 商家是否要在小红书做推广❓1分钟让你想明白
  • 2025年国内光伏线缆厂家最新权威推荐排行榜
  • TOP10留学机构干货:服务细节聚焦文本优势双保障
  • 交通设施行业公路护栏优质品牌推荐指南多场景适配
  • 留学机构排行榜TOP10:好文书如何改写你的录取结果
  • 这是新建的随笔,第二篇
  • 2025博士申请十大机构实测:学术引航,申请不迷路
  • 2025年乳化机设备订制厂家权威推荐榜单:高剪切混合乳化机‌/真空制膏机‌/实验室乳化机源头厂家精选
  • 文书深度打磨!留学机构排名TOP10适配名校偏好
  • 2025年青岛初三辅导班机构权威推荐榜单:高一辅导班‌/高二辅导班‌/高三辅导班源头机构精选
  • 多重比较校正
  • 小动物影像分析资源
  • FY3D/MERSI 哈默投影 NDVI/EVI - EPSG:4326 投影转换 - Littlefish
  • 02_mysql数据库的数据类型
  • 为你的STM32毕设项目加点“料”:AI智能照明助手光环境自适应控制系统
  • 甘肃全屋定制五大推荐,欧比亚全屋定制公司领衔品质之选:涵盖旧房改造、装修公司、家具定制、全屋整装