Droplet 上的 Nginx 服务器 IP 重定向到特定域

Nginx server IP on droplet redirect to specific domain

我在 DigitalOcean Droplet 上有 2 个域 运行 nginx

域 1 是传递给 localhost:3000 的节点应用程序代理。 效果很好!

Domain2 是一个静态站点,也很好用。

然而,每当我加载服务器 IP(没有端口 3000)时,我总是被重定向到域 1(节点应用程序)。

域 1 是一种私人站点,而域 2 是 public 博客。

我的问题是我必须更改什么才能让人们在加载 IP 时被重定向到 domain2,以保护 domain1 不被​​轻易访问。 (VPSIP好查)

这是 "sites-available" 个文件:

节点应用程序:

server {
    listen [::]:80;
    listen 80;


server_name www.domain1.com domain1.com;

# and redirect to the https host (declared below)
return 301 https://domain1.com$request_uri;
}

server {
    listen 443;
    server_name domain1.com www.domain1.com;

    ssl on;
    # Use certificate and key provided by Let's Encrypt:
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';


    location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://localhost:3000/;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;
            proxy_redirect off;
    }
}

静态的:

server {
    listen [::]:80;
    listen 80;


   server_name www.domain2.com domain2.com;

   root /var/www/html/domain2;
   index index.html index.htm;


   return 301 https://domain2.com$request_uri;
}

server {

   listen [::]:443 ssl;
   listen 443 ssl;

   root /var/www/html/domain2;

   index index.html index.htm;

   ssl_certificate /etc/letsencrypt/live/domain2.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/domain2.com/privkey.pem;
}

感谢任何help/hint,提前致谢!

所以你的两个域都在端口 80 上侦听。当你的 nginx 服务器收到请求时,它会在确定其路由之前检查域...但是因为当你只输入 ip 时没有要检查的域地址,它将默认为第一个列出的服务器(我猜是 domain1)

您可以通过声明默认服务器或切换它们的列出顺序来规避此问题。

希望对您有所帮助。一个不错的小参考 http://nginx.org/en/docs/http/request_processing.html