【问题标题】:Nginx routing on IP addressIP地址上的Nginx路由
【发布时间】:2020-08-18 15:05:33
【问题描述】:

我有我的域 example.com,所以当有人点击 www.example.com 或 example.com 时,请求会自动定向到 https://example.com - 这很好用。但是,当我使用节点应用程序 1.2.3.4 的 IP 地址时,它不会路由到启用 SSL 的https://example.com。如果我使用 IP 地址,它会显示相同的页面,但没有挂锁图标。

那么当有人输入节点应用的IP地址时,我如何将请求路由到https://example.com

我的 Node JS APP 托管在 AWS EC2 实例上,我还使用 certbot (LetsEncrpyt) 安装了 ssl。这是我的 nginx 文件。

 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;
  }
}
}

【问题讨论】:

    标签: node.js nginx amazon-ec2


    【解决方案1】:

    SSL 不适用于 IP,因为为域名颁发了证书(因此没有挂锁图标)

    您可以在端口 80(默认 HTTP 端口)上侦听 IP 并重定向到 https://example.com

    server {
       listen 80;
       server_name XXX.XXX.XXX.XXX;
       return 301 https://www.example.com$request_uri;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 2020-01-26
      • 2020-05-06
      • 2010-10-06
      • 1970-01-01
      • 2013-12-19
      • 2019-05-06
      • 2014-09-04
      • 1970-01-01
      相关资源
      最近更新 更多