nginx 配置
二、常見路由規(guī)則配置示例
1. 基本路由轉(zhuǎn)發(fā)(反向代理)
將特定路徑轉(zhuǎn)發(fā)到其他服務(wù):
location /api/ {
proxy_pass
proxy_pass http://127.0.0.1:3000/; # 轉(zhuǎn)發(fā)到本地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. 偽靜態(tài)路由(適合 PHP 框架如 Laravel、ThinkPHP)
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last; # 轉(zhuǎn)發(fā)到index.php處理
break;
}
}
3. 靜態(tài)文件路由優(yōu)化
# 處理靜態(tài)文件
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires 30d; # 設(shè)置緩存30天
add_header Cache-Control "public, max-age=2592000";
access_log off; # 不記錄靜態(tài)文件訪問日志
}
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
}

浙公網(wǎng)安備 33010602011771號