企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 一、history路由模式的特殊配置 hash 模式通用性好,而且不依赖服务器的配置,省心省力,但是缺点是不够优雅。相比于 hash 模式来说,history 模式则更加美观; 对于hash路由模式,nginx无需特殊配置,对于history路由模式,则需要增加额外配置项才可以; history 模式会有一个问题,就是当页面刷新时,如果没有合适的配置,会出现页面 404 的错误。因此需要额外的服务器配置,对于找不到的 url,将首页 html 返回; ``` location / { try_files $uri $uri/ /index.html; } ``` ## 二、设置前端服务器的IP地址 如果使用了反向代理,后端就无法获取到真实客户端IP,必须增加头,才可以; ``` proxy_set_header X-Real-IP $remote_addr; ``` ## 三、history模式地址重定向为hash路由模式 ``` location / { rewrite ^/home/register(.*) /home#/register permanent; } ``` ![](https://img.kancloud.cn/6c/27/6c27a6806554b80cfdbffb45053073a7_732x165.png) ## 四、一个服务器部署两个应用 ### **根应用需重写地址** ``` location / { root html/mobile; index index.html index.htm; try_files $uri $uri/ /index.html; index index.html index.htm; rewrite ^/home/register(.*) /home#/register permanent; rewrite ^/login(.*) /#/login permanent; } location /admin/ { alias /usr/lib/app/nginx/html/admin/; try_files $uri $uri/ /index.html; index index.html index.htm; } ``` ### **非根应用需重写地址:** ``` location / { root html/www; index index.html index.htm; try_files $uri $uri/ /index.html; } location /cardapply { alias /usr/lib/app/nginx/html/cardapply/; try_files $uri $uri/ /index.html; index index.html index.htm; rewrite ^/cardapply/applycard(.*) /cardapply/#/applycard permanent; } ``` ## 五、根据浏览器类型选择手机版或PC版 ``` set $is_mobile false; if ($http_user_agent ~* (harmonyos|android|ip(ad|hone|od)|kindle|blackberry|windows\s(ce|phone))) { set $is_mobile true; } location / { if ( $is_mobile = true ) { root html/wwwmobile; } if ($is_mobile = false ) { root html/www; } index index.html index.htm; try_files $uri $uri/ /index.html; } ```