基于Express实现子域名配置(AWS MEAN栈场景)
Hey Robert, let's break this down step by step—getting your Express app running on app.domain.com instead of port 8000 is totally doable, and it mostly boils down to two key pieces: DNS routing and setting up a reverse proxy (since Express isn't built to handle direct domain traffic at scale, especially on AWS). Here's the simplest, most reliable way to make this work:
Before touching your code or server config, you need to tell the internet where app.domain.com should send traffic.
- If you use AWS Route 53 (their DNS service):
- Go to your hosted zone for
domain.com - Create either a CNAME record (name:
app, value: your EC2 instance's public DNS) or an A record (name:app, value: your instance's Elastic IP address—use an Elastic IP here so your IP doesn't change if you restart the instance)
- Go to your hosted zone for
- For other domain providers:
- Add a CNAME or A record in your domain's DNS settings, pointing
app.domain.comto your AWS instance's public IP address.
- Add a CNAME or A record in your domain's DNS settings, pointing
Express runs on port 8000, so we'll use Nginx to listen for traffic on port 80 (the default HTTP port) for app.domain.com, then forward that traffic to your Express app. This is the standard way to handle domain routing for Node apps.
Step 2.1 Install Nginx on Your EC2 Instance
SSH into your AWS instance, then run the appropriate command for your OS:
# For Ubuntu/Debian-based instances sudo apt update && sudo apt install nginx -y # For Amazon Linux 2/CentOS sudo yum install nginx -y
Step 2.2 Create a Custom Nginx Config for Your Subdomain
Don't edit the default Nginx config—create a dedicated server block for your subdomain:
sudo nano /etc/nginx/sites-available/app.domain.com
Paste this config (you can use localhost here since Nginx is on the same machine as your Express app):
server { listen 80; server_name app.domain.com; location / { # Forward traffic to your Express app's port proxy_pass http://localhost:8000; # These headers ensure your app gets the right request info proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_cache_bypass $http_upgrade; } }
Step 2.3 Enable the Config & Restart Nginx
Link the config file to Nginx's enabled sites directory:
sudo ln -s /etc/nginx/sites-available/app.domain.com /etc/nginx/sites-enabled/
Test the config for syntax errors (critical—don't skip this!):
sudo nginx -t
If you see "test is successful", restart Nginx to apply changes:
sudo systemctl restart nginx
Make sure your EC2 instance allows incoming HTTP traffic on port 80:
- Go to the AWS EC2 Console → Select your instance → Click on the Security Group linked to it
- Edit inbound rules → Add a new rule:
- Type: HTTP
- Protocol: TCP
- Port Range: 80
- Source: 0.0.0.0/0 (or restrict to your IP if you don't want public access yet)
- Keep port 8000 open if you need to test your app directly, but it's not required for the subdomain to work.
You don't need to rewrite your server.js—just double-check your app is listening on 0.0.0.0 (not just localhost) so Nginx can connect to it:
var express = require('express'); var app = express(); // ... your existing routes, middleware, etc. ... // Listen on all interfaces (0.0.0.0) instead of just localhost app.listen(8000, '0.0.0.0', () => { console.log('Express app running on port 8000, ready for Nginx traffic!'); });
Restart your Express app after making this change.
Wait 5-10 minutes for DNS to propagate (sometimes longer for first-time records), then visit http://app.domain.com in your browser. Your app should load exactly like it did when you accessed it via your-aws-ip:8000!
Troubleshooting If It's Not Working
- Check Nginx error logs for clues:
sudo tail -f /var/log/nginx/error.log - Verify your DNS is resolving: Run
nslookup app.domain.comon your local machine—should show your AWS instance's IP. - Confirm your Express app is running:
ps aux | grep nodeto see if the Node process is active.
内容的提问来源于stack exchange,提问作者Robert

