【发布时间】:2021-12-30 22:47:04
【问题描述】:
我有一个子域 sub.example.com,它指向托管在 EC2 实例上的 Web 服务器。
- 在 AWS Route53 控制台中,我创建了一个指向该实例的公共 EIP 的 A 记录。
- 我检查了
nslookup的 DNS 记录,它们看起来没问题。 - 我可以使用其公共 IP 地址从浏览器访问子域 Web 服务器。
但是如果我尝试使用域名访问,浏览器会将请求重定向到父域:http://sub.example.com -> http://example.com。我使用 Nginx 作为反向代理和 NodeJs 作为后端服务器。
我需要做什么才能让它工作?
编辑
如果我使用 www,我可以访问它。前缀 (www.sub.example.com)。但如果没有“www”,浏览器只会将我重定向到父域..
nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name sub.example.com www.sub.example.com;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
# Redirect all HTTP request to the node.js
location / {
proxy_redirect off;
proxy_pass "http://127.0.0.1:5000";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}
【问题讨论】:
-
您的预期输出是什么?您只想路由 sub.x.com 而不是 x.com?还是两者都路由到 ECC2 实例?
-
@Ermiya Eskandary 我希望可以通过“www.sub.x.com”或“sub.x.com”访问该子域。但如果我不使用“www”部分,它会将我重定向到父域。
-
听起来您只有一个指向 EC2 实例的
www.sub.example.com的 DNS 记录。您还需要为sub.exammple.com添加一条指向 EC2 实例的 DNS A 记录。 -
@MarkB 我有“sub.x.com”的 A 记录 > “www.sub.x.com” > “sub.x.com”的 ip 和别名记录。跨度>
-
你能提供你的nginx配置吗?
标签: node.js amazon-web-services http nginx amazon-route53