nginx 伪静态配置
2023-07-02 本文已影响0人
国服最坑开发
- 需求
/mypath/
下面所有的请求,都返回index.html
这个静态资源。
应用场景:单页面web应用,内部支持多路由进行页面渲染。
- 关键配置
location /mypath/ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers *;
add_header Cache-Control no-cache;
if ($request_method = 'OPTIONS') {
return 204;
}
default_type text/html;
# 实际会去目录 /app/nginx/static/mypath/ 下面找东西
root /app/nginx/static;
index index.html index.htm;
try_files $uri $uri/ @guiderewrite;
}
location @guiderewrite {
rewrite ^(.+)$ /mypath/index.html last;
}