我有一個指向 EC2 實體上托管的 Web 服務器的子域sub.example.com。
- 在 AWS Route53 控制臺中,我創建了一個 A 記錄,指向該實體的公共 EIP。
- 我已經檢查了 DNS 記錄,
nslookup它們看起來不錯。 - 我可以使用其公共 IP 地址從瀏覽器訪問子域 Web 服務器。
但是如果我嘗試使用域名訪問,瀏覽器會將請求重定向到父域:http://sub.example.com-> http://example.com。我使用 Nginx 作為反向代理和 NodeJs 作為后端服務器。
我需要做什么才能讓它發揮作用?
編輯
我可以訪問它,如果我使用 www. 前綴 ( www.sub.example.com )。但是沒有“www”,瀏覽器只會將我重定向到父域..
組態檔
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;
}
}
}
uj5u.com熱心網友回復:
- 在 Route53 中創建兩個型別“A”的 DNS 記錄(
xxx.yyy.zzz.aaa是您的 EC2 實體的公共 IP 地址,例如18.185.121.30):
sub.example.com -> xxx.yyy.zzz.aaa
www.sub.example.com -> xxx.yyy.zzz.aaa
- 使用標準
nginx配置(不要指定任何 DNS 名稱server_name- 使用默認值,即server_name _;):
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 _;
# 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;
}
}
}
- 客戶端(瀏覽器)和服務器(nginx)都可能快取回應。使用瀏覽器的隱身模式或
curl測驗:
curl -I -H "Cache-Control: no-cache" http://sub.example.com
要有耐心。DNS 記錄需要一些時間(生存時間或 TTL)才能在全球傳播。您可以減少 Route53 中的 TTL 并減少等待。
要除錯 DNS 問題,請使用以下 Linux 命令:
dig -t a sub.example.com
我也喜歡這個網路服務,它可以幫助您在全球范圍內跟蹤 DNS 傳播。
更新:這是node.js我在埠 5000 上運行的示例Web 服務器:
var http = require('http');
var server = http.createServer(function (req, res) {
if (req.url == '/') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<html><body><p>This is home Page.</p></body></html>');
res.end();
}
});
server.listen(5000);
console.log('Node.js web server at port 5000 is running..')
uj5u.com熱心網友回復:
問題出在 dns 決議器快取中。它快取了指向舊 IP 的過時 A 記錄。更新 dns 快取后,問題就消失了。
謝謝@maslick 的回復。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/362023.html
標籤:节点.js 亚马逊网络服务 http nginx 亚马逊-route53
上一篇:Node.JS:由于node-sass和node-gyp,嘗試使用docker-composeup退出運行docker專案
