惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
腾讯CDC
D
Docker
G
Google Developers Blog
D
DataBreaches.Net
雷峰网
雷峰网
Blog — PlanetScale
Blog — PlanetScale
S
SegmentFault 最新的问题
The Cloudflare Blog
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
量子位
美团技术团队
aimingoo的专栏
aimingoo的专栏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Engineering at Meta
Engineering at Meta
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Lafite-1820

linux 常用的命令 mac m1 使用vpn之后浏览器连不上网处理 - Lafite-1820 Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!! 5分钟搭建完整后端服务,这款开源的快速开发神器太牛了! 高并发IoT监控系统的Go实践:Goroutine池设计与并发安全指南 服务器设置成美国时间 Linux crond nginx 配置 mysql mac m1 报错处理 PostgreSQL 为什么不选择 B+ 树索引? 秒杀系统的架构(Golang 实现) git 突然 403 Vue 项目接入Google第三方登录的详细流程 mysql 添加账号 [HY000][1366] Incorrect string value: '\xF0\x9F\x8C\x9F",...' ; - Lafite-1820 mac 外接硬盘系统软件 linux 常用命令 crontab 半小时执行一次 mac berw 安装deepseek linux mv 限时文件个数
nginx 配置
Lafite-1820 · 2025-08-03 · via 博客园 - Lafite-1820

二、常见路由规则配置示例

1. 基本路由转发(反向代理)

将特定路径转发到其他服务:

location /api/ {
    proxy_pass
        proxy_pass http://127.0.0.1:3000/;  # 转发到本地3000端口
        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;
    }
}

2. 伪静态路由(适合 PHP 框架如 Laravel、ThinkPHP)

location / {
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=$1  last;  # 转发到index.php处理
        break;
    }
}

3. 静态文件路由优化

# 处理静态文件
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
    expires 30d;  # 设置缓存30天
    add_header Cache-Control "public, max-age=2592000";
    access_log off;  # 不记录静态文件访问日志
}

4. 特定路径重定向

# 将/http重定向到/https
location /http {
    return 301 /https$request_uri;
}

# 将根路径重定向到/admin
location = / {
    return 302 /admin;
}

5. 限制特定 IP 访问

location /admin/ {
    allow 192.168.1.100;  # 允许特定IP
    deny all;  # 拒绝其他所有IP
}