【问题标题】:Nginx server IP on droplet redirect to specific domain液滴上的 Nginx 服务器 IP 重定向到特定域
【发布时间】:2016-08-18 10:15:33
【问题描述】:

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

Domain1 是一个节点应用代理传递到 localhost:3000。 效果很好!

Domain2 是一个运行良好的静态站点。

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

Domain1 是一种私人网站,而 domain2 是公共博客。

我的问题是,当人们加载 IP 时,我必须进行哪些更改才能将其重定向到 domain2,以保护 domain1 不被​​轻易访问。 (VPS IP很容易查到)

这里是“站点可用”文件:

节点应用:

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

感谢任何帮助/提示,​​在此先感谢您!

【问题讨论】:

    标签: node.js nginx server vps


    【解决方案1】:

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

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

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

    【讨论】:

    • 嗨,Aldose,非常感谢您的帮助。得到它的工作,因为我应该感谢你的提示。再次感谢,祝您有美好的一天!
    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 2021-03-26
    • 2020-07-03
    • 2018-04-11
    • 2021-03-25
    • 2015-04-15
    • 2014-05-20
    • 1970-01-01
    相关资源
    最近更新 更多