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

时间:2016-08-18 10:15:34

标签: node.js nginx server vps

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

Domain1是一个通过localhost:3000的节点应用程序代理。 效果很棒!

Domain2也是一个非常有用的静态网站。

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

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

我的问题是,每当用户加载IP时,我需要更改哪些内容才能重定向到domain2,以保护域1免受易于访问的影响。 (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;
}

感谢任何帮助/提示,​​提前谢谢!

1 个答案:

答案 0 :(得分:1)

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

您可以通过声明默认服务器或切换列出顺序来规避这一点。

我希望我可以提供帮助。一个不错的小参考http://nginx.org/en/docs/http/request_processing.html

相关问题