You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot本地静态资源正常,部署到VPS(WHM/Cpanel)无法访问

Hey Brian, let's work through this static resource issue you're seeing with your Spring Boot app on WHM/cPanel. I've troubleshooted similar deployments before, so here are the key configurations to check and tweak:

1. Fix File Permissions First (Most Common Fix)

First things first—cPanel is strict about file permissions, and incorrect settings are the #1 reason static resources fail to load.

  • Log into your cPanel account, open File Manager, and navigate to where your static resources are stored (either in public_html/staticlayouts or inside your deployed Spring Boot app's static directory).
  • For folders: Set permissions to 755 (read/execute for everyone, write for owner).
  • For individual files (like blah.html): Set permissions to 644 (read for everyone, write for owner).
  • If you're deploying a WAR file, make sure when it extracts, the permissions are inherited correctly—sometimes you might need to run a recursive permission update via cPanel's terminal (using chmod -R 755 /path/to/static/folder).
2. Adjust Apache Virtual Host & Rewrite Rules

WHM/cPanel uses Apache to handle web requests, and if your rewrite rules are forwarding all requests to Spring Boot's DispatcherServlet, static resources won't be served directly by Apache (which is faster and avoids Spring routing issues).

  • Log into WHM, go to Apache Configuration > Virtual Hosts Editor, and select your domain's virtual host.
  • Add these rewrite rules to ensure static resources are served directly:
RewriteEngine On
# Skip forwarding requests to your Spring Boot app for staticlayouts resources
RewriteCond %{REQUEST_URI} !^/staticlayouts/
# Forward all other requests to your Spring Boot application (adjust the path if your app is in a subfolder)
RewriteRule ^(.*)$ /ROOT/$1 [L]

Note: If your Spring Boot app is deployed under a context path (like /my-app), replace /ROOT/ with /my-app/ in the RewriteRule.

3. Tweak Spring Boot's Static Resource Configuration

If your app's static resource mapping doesn't align with the URL you're using, you'll need to adjust its internal settings:

  • Open your Spring Boot project's application.properties (or application.yml) file and add these lines:
# Map requests to /staticlayouts/** to your app's internal static resources folder
spring.mvc.static-path-pattern=/staticlayouts/**
# Define where your static resources are stored internally (default is classpath:/static/)
spring.web.resources.static-locations=classpath:/static/

This tells Spring Boot to route any request starting with /staticlayouts/ to the files in your src/main/resources/static directory (which is where your blah.html should be).

4. Check Java App Deployment Context Root

If you used cPanel's Setup Java App tool to deploy your Spring Boot app, the context root setting might be causing issues:

  • In cPanel, go to Setup Java App, find your deployed application, and check the Context Root field.
  • If it's set to something like /my-app, your static resource URL would need to be http://www.example.com/my-app/staticlayouts/blah.html. If you want to use the root path (/), change the context root to / and redeploy the app.

Start with the file permissions and Apache rewrite rules—those are the most frequent fixes for this exact scenario. Let me know if you run into any issues while adjusting these!

内容的提问来源于stack exchange,提问作者Brian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:09:55