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

Nginx配置与命令

Nginx 配置文件基础

  • 全局块(Main Context):配置影响全局的参数,如用户、进程数、日志路径等。

    user  nginx;                # 运行Nginx的用户和组
    worker_processes  auto;     # 工作进程数(通常设为CPU核心数)
    error_log  /var/log/nginx/error.log warn;  	# 错误日志路径及级别
    pid        /var/run/nginx.pid;     					# 进程PID文件路径
    
  • Events 块:配置网络连接相关参数。

    events {worker_connections  1024;   # 单个工作进程的最大并发连接数use epoll;                  # 使用高效的事件模型(Linux)
    }
    
  • HTTP 块:定义 HTTP 服务的全局配置,可包含多个 server 块(虚拟主机)。

    http {include       /etc/nginx/mime.types;   	# 包含MIME类型定义文件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  /var/log/nginx/access.log  main; # 访问日志路径sendfile        on;          # 启用高效文件传输模式keepalive_timeout  65;       # 客户端长连接超时时间# 包含其他配置文件(如虚拟主机配置)include /etc/nginx/conf.d/*.conf;
    }
    
  • Server 块(虚拟主机):定义单个网站的监听端口、域名、路由规则等。

    server {listen       80;            # 监听端口server_name  example.com;   # 域名或IPlocation / {                # 路由匹配规则root   /usr/share/nginx/html;  # 静态资源根目录index  index.html index.htm;   # 默认首页}# 错误页面配置error_page  500 502 503 504  /50x.html;location = /50x.html {root   /usr/share/nginx/html;}
    }
    

Nginx 常用配置指令

  • 静态资源服务

    location /static/ {alias /data/static/;    # 路径映射(末尾必须加/)expires 30d;            # 设置缓存过期时间access_log off;         # 关闭访问日志
    }
    
  • 反向代理

    location /api/ {proxy_pass http://backend_server;  # 转发到后端服务器proxy_set_header Host $host;       # 传递原始请求头proxy_set_header X-Real-IP $remote_addr;
    }
    
  • 负载均衡

    upstream backend_server {server 10.0.0.1:8080 weight=3;  # 权重分配server 10.0.0.2:8080;server 10.0.0.3:8080 backup;    # 备用服务器
    }server {location / {proxy_pass http://backend_server;}
    }
    
  • HTTPS 配置

    server {listen 443 ssl;server_name example.com;ssl_certificate      /etc/ssl/certs/example.com.crt;  # 证书路径ssl_certificate_key  /etc/ssl/private/example.com.key;ssl_session_cache    shared:SSL:10m;   	# SSL会话缓存ssl_session_timeout  10m;             	# 会话超时时间# 安全协议配置ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;location / {root   /usr/share/nginx/html;index  index.html;}
    }
    
  • URL重写

    location /old/ {rewrite ^/old/(.*)$ /new/$1 permanent;  # 永久重定向
    }# 正则匹配示例
    location ~ ^/user/(\d+) {rewrite ^/user/(\d+) /profile?id=$1 last;
    }
    

Nginx 常用命令

  • 启动与停止
nginx                     # 启动Nginx
nginx -s stop             # 立即停止
nginx -s quit             # 优雅停止(处理完当前请求后退出)
  • 重载配置
nginx -s reload           # 重新加载配置文件(不中断服务)
nginx -t                  # 测试配置文件语法是否正确
  • 日志管理
# 切割日志(需配合cron定时任务)
mv /var/log/nginx/access.log /var/log/nginx/access_$(date +%Y%m%d).log
nginx -s reopen           # 重新打开日志文件
  • 查看进程与版本
ps aux | grep nginx       # 查看Nginx进程
nginx -v                  # 查看版本
nginx -V                  # 查看编译参数及模块信息

相关文章:

  • ubuntu中已经存在python3.12.3, 如何安装python3.10.8且命令python3版本切换为python3.10.8
  • 得力标签打印机系统集成方案的技术应用与场景实践
  • 02 Nginx虚拟主机
  • 如何畅通需求收集渠道,获取用户反馈?
  • 软考IPSEC案例分析
  • Linux进程信号(三)之信号产生2
  • 短剧小程序系统开发源码上架,短剧项目市场分析
  • 考研数学积分学
  • 渗透测试核心技术:内网渗透与横向移动
  • 类魔方 :多变组合,灵活复用
  • Estimation(估算):业务分析师的“不确定性对抗术”
  • Python黑魔法与底层原理揭秘:突破语言边界的深度探索
  • 【VMware】开启「共享文件夹」
  • 软件架构之-论软件系统架构评估以及应用
  • 批量下载AlphaFold结构
  • 2.1.2
  • LORA 微调 - LoRA 介绍与 LoRA 微调指南
  • vue3 elementplus tabs切换实现
  • 程序代码篇---python获取http界面上按钮或者数据输入
  • “二维前缀和”算法原理及模板
  • 调查:“网约摩的”上线起步价五六元,合规性及安全性引质疑
  • 全中国最好的十个博物馆展陈选出来了!
  • 辽宁援疆前指总指挥王敬华已任新疆塔城地委副书记
  • 国际金价下跌,中概股多数上涨,穆迪下调美国主权信用评级
  • 一条铺过11年时光的科学红毯,丈量上海科创的“长宽高”
  • 圆桌丨全球化博弈与人工智能背景下,企业如何落地合规体系