Ajax请求Servlet返回404错误排查求助
Hey there, let's break down your 404 issue step by step—since it works locally but fails on AWS EC2, we can narrow down the root cause pretty effectively. Here's what to check:
1. First Rule Out Network & Firewall Issues
Since you suspect firewall problems after configuring SSL, start here:
- AWS Security Group Check: Make sure your EC2 security group allows inbound traffic on your Tomcat HTTPS port (default is
8443, or whatever custom port you set inserver.xml). Double-check the source IP range isn't restricted to only your local machine. - Ubuntu UFW Firewall: Run
sudo ufw statusto confirm the HTTPS port is allowed. If not, add it withsudo ufw allow 8443/tcp. - Test Port Connectivity: From your local machine, run
curl -v https://<your-ec2-public-ip>:8443/your-api-endpointortelnet <your-ec2-public-ip> 8443. If the connection times out, the problem is definitely network/firewall-related.
2. Validate Tomcat SSL Configuration
Mismatched SSL settings between local and EC2 often cause silent failures:
- Compare
server.xmlConfigs: Check your EC2server.xml's SSL Connector block against your local one. Ensure:- The port number matches what you're using in AJAX requests
- The keystore path is an absolute path (Linux doesn't recognize relative paths the same way as Windows)
- The Tomcat user has read permissions for the keystore file (run
sudo chown tomcat:tomcat /path/to/your/keystore.jksif needed)
- Check Tomcat Logs: Look at
/var/log/tomcat9/catalina.out(adjust path for your Tomcat version) for SSL initialization errors or request-related warnings. Logs will tell you if requests are even reaching Tomcat.
3. Fix AJAX & Backend Route Mismatches
Linux is case-sensitive, which is a common gotcha when moving from local Windows/macOS:
- Verify AJAX Request URL: Ensure the URL in your JS code uses the correct EC2 public IP/domain, HTTPS, and port. For example, if local uses
http://localhost:8080/api/data, EC2 should behttps://<ec2-ip>:8443/api/data—no typos, no missing context paths. - Check Backend Route Annotations: If you're using Spring MVC/JAX-RS, confirm your
@RequestMapping(or equivalent) path exactly matches the AJAX request path. A lowercase/uppercase mismatch (e.g.,/Api/Datavs/api/data) will cause 404 on Linux but work locally.
4. Audit Your
web.xml Configuration Here are key web.xml settings to verify:
- Servlet Mappings: Ensure your Java backend servlet's
<url-pattern>covers your AJAX endpoint. For example, if your servlet maps to/rest/*, your AJAX request should targethttps://<ec2-ip>:8443/rest/data, not/api/data. - Context Path: If you deployed your app to a non-ROOT context on EC2 (e.g.,
/myapp), your AJAX requests must include this prefix. Local might use ROOT, so you won't notice the difference until deployment. - Filter Interactions: Check if any security filters (like CORS filters) are blocking HTTPS requests. Make sure filter mappings apply to your API paths and don't restrict access to local-only traffic.
5. Isolate the Issue with a Quick Test
To rule out backend vs network issues:
- SSH into your EC2 instance and run
curl https://localhost:8443/your-api-endpointdirectly on the server.- If this returns a valid response: The backend works—problem is in external network access or AJAX URL configuration.
- If this also returns 404: The issue is with your backend routing or Tomcat deployment, not the firewall.
内容的提问来源于stack exchange,提问作者Tyler
相关产品推荐
相关产品推荐

