LOGO 首页 OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 技术文档 其他文档  
 
网站管理员

零基础学 Nginx:安装、启动、核心配置全流程

admin
2026年7月18日 23:47 本文热度 38

      简单来说,Nginx就像一个“全能网关”,介于客户端和服务器之间,负责接收客户端的请求,再根据配置将请求分发到对应的服务,同时处理静态资源、优化请求效率,解决传统服务器的高并发瓶颈。

一、安装与命令

# Ubuntu/Debiansudo apt update && sudo apt install nginx -y
# CentOS/RHELsudo yum install nginx -y
# 常用命令nginx -t                 # 检查配置语法(改配置后必执行)nginx -s reload          # 平滑重载配置nginx -s stop            # 快速停止systemctl enable nginx   # 设置开机自启systemctl status nginx   # 查看运行状态

二、 静态托管

server {    listen 80;    server_name www.yourblog.com;
    root /var/www/blog/dist;    index index.html;
    # SPA路由支持(Vue/React刷新不404)    location / {        try_files $uri $uri/ /index.html;    }
    # 静态资源强缓存    location ~* \.(css|js|jpg|png|gif|ico|svg|woff2)$ {        expires 30d;        add_header Cache-Control "public, immutable";    }
    # 屏蔽敏感目录    location ~ /\.git {        deny all;    }}

三、反向代理

location /api/ {    # 有斜杠:转发时去掉 /api 前缀    proxy_pass http://127.0.0.1:8080/;
    # 必备Header:让后端获取真实客户端信息    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_connect_timeout 60s;    proxy_read_timeout 60s;}

四、 负载均衡

upstream backend_cluster {    # 加权轮询    server 192.168.1.10:8080 weight=3 max_fails=3 fail_timeout=30s;    server 192.168.1.11:8080 weight=1 max_fails=3 fail_timeout=30s;    server 192.168.1.12:8080 backup;  # 备用节点
    # 如需会话保持,取消下行注释    # ip_hash;}
server {    location / {        proxy_pass http://backend_cluster;        proxy_set_header Host $host;    }}

策略对比表

 策略
关键字
适用场景
 轮询
默认
后端性能一致
 加权轮询
weight
后端配置有差异
 IP哈希
ip_hash
需要Session粘滞
 最少连接
least_conn
请求处理时间差异大

五、静态缓存

http {    # 定义缓存区(放在 http 块)    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=blog_cache:10m max_size=1g inactive=7d;
    server {        location / {            proxy_cache blog_cache;            proxy_cache_key "$host$request_uri";            proxy_cache_valid 200 301 302 7d;            proxy_cache_valid 404 1m;            proxy_cache_valid any 5m;
            # 调试用:响应头显示 HIT/MISS            add_header X-Cache-Status $upstream_cache_status;
            proxy_pass http://127.0.0.1:8080;        }    }}

六、强制 HTTPS 跳转

# HTTP → HTTPS 永久重定向server {    listen 80;    server_name www.yourblog.com;    return 301 https://$server_name$request_uri;}
# HTTPS 服务server {    listen 443 ssl http2;    server_name www.yourblog.com;
    ssl_certificate     /etc/letsencrypt/live/yourblog.com/fullchain.pem;    ssl_certificate_key /etc/letsencrypt/live/yourblog.com/privkey.pem;
    # 启用 HSTS(强制浏览器一年内只走 HTTPS)    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    # SSL 安全加固    ssl_protocols TLSv1.2 TLSv1.3;    ssl_ciphers HIGH:!aNULL:!MD5;    ssl_prefer_server_ciphers on;
    # 正常业务配置...}

免费证书申请命令

sudo apt install certbot python3-certbot-nginx -ysudo certbot --nginx -d yourblog.com -d www.yourblog.com

七、性能优化组合拳

http {    # Gzip 压缩(减少传输体积)    gzip on;    gzip_min_length 1k;    gzip_types text/plain text/html text/css application/javascript application/json;    gzip_vary on;
    # 限流防刷(每IP每秒1个请求,允许突发5个)    limit_req_zone $binary_remote_addr zone=blog_req:10m rate=1r/s;
    server {        location / {            limit_req zone=blog_req burst=5 nodelay;            # ... 其他配置        }    }}

八、常见问题排查表

 现象
可能原因
排查命令/解决方案
 502 Bad Gateway
后端服务未启动/端口不对
netstat -tlnp
 检查后端端口;tail -f error.log
 403 Forbidden
目录权限不足或缺少 index 文件
chmod -R 755 /var/www
;检查 index 指令
 404 Not Found
Location 匹配错误或 root 路径写错
nginx -T
 查看最终配置;确认磁盘路径存在
 重定向死循环
HTTP 强制跳转 HTTPS 误判
检查 X-Forwarded-Proto 传递是否正确
 缓存不生效
proxy_cache_path 目录无写权限
chown -R nginx:nginx /var/cache/nginx

该文章在 2026/7/18 23:47:55 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2026 ClickSun All Rights Reserved  粤ICP备13012886号-9  粤公网安备44030602007207号