Ich habe meine Domain example.com, also wenn jemand aufwww.beispiel.comoder example.com wird die Anfrage automatisch weitergeleitet anhttps://example.com- das funktioniert gut. Wenn ich jedoch die IP-Adresse der Node-App 1.2.3.4 verwende, wird sie nicht weitergeleitet anhttps://example.comdas SSL aktiviert ist. Wenn ich die IP-Adresse verwende, wird mir dieselbe Seite angezeigt, jedoch ohne das Vorhängeschlosssymbol.
Wie leite ich eine Anfrage weiter anhttps://example.comwenn jemand die IP-Adresse der Node-App eingibt?
Meine Node JS-App wird auf einer AWS EC2-Instanz gehostet. Ich habe auch SSL mit Certbot (LetsEncrpyt) installiert. Dies ist meine Nginx-Datei.
events {
worker_connections 4096; ## Default: 1024
}
http {
include conf/mime.types;
include /etc/nginx/proxy.conf;
include /etc/nginx/fastcgi.conf;
index index.html index.htm;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128; # this seems to be required for some vhosts
# Settings for normal server listening on port 80
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
root /usr/share/nginx/html;
# location / {
# }
# Redirect non-https traffic to https
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
# Settings for a TLS enabled server.
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name example.com www.example.com;
root /usr/share/nginx/html;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_dhparam "/etc/pki/nginx/dhparams.pem";
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
}
Antwort1
Sie können das Abhören der Server-IP-Adresse konfigurieren und dann nach dem Host-Header filtern und beispielsweise eine 301-Umleitung senden:
events {
worker_connections 4096; ## Default: 1024
}
http {
index index.html index.htm;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128; # this seems to be required for some vhosts
# Settings for normal server listening on port 80
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com 1.2.3.4;
root /usr/share/nginx/html;
# location / {
# }
# Redirect non-https traffic to https
if ($host = 1.2.3.4){
return 301 https://www.example.com;
}
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
# Settings for a TLS enabled server.
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name example.com www.example.com 1.2.3.4;
root /usr/share/nginx/html;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_dhparam "/etc/pki/nginx/dhparams.pem";
if ($host = $server_addr){
return 301 https://www.example.com;
}
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
}