debian

如何通过Nginx配置提高网站排名

小樊
46
2025-12-11 23:01:38
栏目: 云计算

Nginx 配置提升网站排名的实操清单

一 核心原则

二 必做配置清单

三 可直接使用的配置片段

# 1) 统一域名与协议(示例:统一到 https://example.com)
server {
  listen 80;
  server_name www.example.com example.com;
  return 301 https://example.com$request_uri;
}

server {
  listen 443 ssl http2;
  server_name www.example.com;
  return 301 https://example.com$request_uri;
}

server {
  listen 443 ssl http2;
  server_name example.com;

  ssl_certificate     /path/to/fullchain.pem;
  ssl_certificate_key /path/to/privkey.pem;

  root /var/www/html;
  index index.html;

  # 2) 静态资源:长缓存 + 高效传输
  location ~* \.(jpg|jpeg|png|gif|ico|webp|svg|css|js)$ {
    expires 1y;
    add_header Cache-Control "public, immutable, no-transform";
    add_header Vary "Accept-Encoding";
    etag on;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    try_files $uri =404;
  }

  # 3) Gzip 动态压缩
  gzip on;
  gzip_vary on;
  gzip_min_length 256;
  gzip_comp_level 6;
  gzip_types text/plain text/css application/javascript application/json application/xml text/xml;

  # 4) 启用预压缩(需构建时生成 .gz 文件,如:gzip -k file.js)
  # 若未编译进模块,可移除或改为仅动态压缩
  gzip_static on;

  # 5) 智能路由(SPA 示例)
  location / {
    try_files $uri $uri/ /index.html;
  }

  # 6) 可选:反向代理时开启对代理响应的压缩
  # gzip_proxied any;
}

四 进阶优化与注意事项

0
看了该问题的人还看了