如何通过Amazon Route 53实现无端口访问AWS EC2上的Tomcat应用
Hey there! Let's get your app loading directly when someone types xyz.com—no extra port number or project path needed. I've got two reliable approaches for you to pick from, depending on your setup:
方法1:直接修改Tomcat配置(无需额外软件)
This is perfect if you don't want to add extra services and just want to tweak Tomcat's default settings:
修改Tomcat监听端口为80
Find theconf/server.xmlfile in your Tomcat installation directory, locate the<Connector>tag that listens on port 8080, and changeport="8080"toport="80".Heads up: On Linux, binding to port 80 requires root privileges. You can either:
- Start Tomcat with
sudo(e.g.,sudo ./bin/startup.sh) - Use
authbindto grant Tomcat permission to bind port 80 without running as root (safer for production)
- Start Tomcat with
Set ABCProject as Tomcat's default root app
Choose one of these options:- The easiest way: Rename your
ABCProject.warfile toROOT.war, drop it into Tomcat'swebappsdirectory, then restart Tomcat. It'll automatically load as the root application. - More flexible (if you don't want to rename your war): Add a
<Context>element inside the<Host>tag inconf/server.xml, like this:
Replace<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- Keep your existing Host config here --> <Context path="" docBase="/path/to/your/ABCProject" reloadable="true" /> </Host>/path/to/your/ABCProjectwith the actual path to your project (or your WAR file), then restart Tomcat.
- The easiest way: Rename your
方法2:用Nginx做反向代理(推荐,更灵活)
If you might add SSL, load balancing, or other services later, reverse proxying with Nginx is the better long-term solution:
Install Nginx
Run the right command for your EC2 OS:- Ubuntu/Debian:
sudo apt update && sudo apt install nginx - CentOS/RHEL:
sudo yum install nginx
- Ubuntu/Debian:
Configure reverse proxy rules
Create a new Nginx config file—like/etc/nginx/sites-available/xyz.com(Ubuntu/Debian) or/etc/nginx/conf.d/xyz.conf(CentOS/RHEL)—and paste this:server { listen 80; server_name xyz.com www.xyz.com; location / { proxy_pass http://localhost:8080/ABCProject/; # Pass critical headers to avoid app issues proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }Enable the config and restart Nginx
- Ubuntu/Debian: Create a symlink to enable the config, validate it, then restart:
sudo ln -s /etc/nginx/sites-available/xyz.com /etc/nginx/sites-enabled/ sudo nginx -t # Check for syntax errors sudo systemctl restart nginx - CentOS/RHEL: Validate and restart directly:
sudo nginx -t sudo systemctl restart nginx
- Ubuntu/Debian: Create a symlink to enable the config, validate it, then restart:
Critical Checks Don't Forget
- AWS Security Group: Make sure you add an inbound rule to your EC2 security group allowing HTTP (port 80) from
0.0.0.0/0(or your desired IP range)—otherwise external users can't reach your app on port 80. - SELinux (CentOS/RHEL only): If Nginx can't connect to Tomcat, run
sudo setsebool -P httpd_can_network_connect 1to allow Nginx to make network connections. - System Firewall: Ensure your EC2 instance's firewall (ufw, firewalld) allows local access to port 8080 so Nginx can talk to Tomcat.
内容的提问来源于stack exchange,提问作者Sachin Goyal

