Nginx |一台服务器上的 2 个域(1x 节点应用程序,1x 静态 HTML)

Nginx | 2 Domains (1x Node App, 1x Static HTML) on one server

我在两个单独的域上同时遇到 运行 一个节点应用程序和一个静态页面(只是 html)的问题。无论我尝试什么,静态域总是被重定向到节点应用程序(在端口 3000 上)

这是 "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 将不胜感激。

在我为 domain2 生成 Let's encrypt 证书、将两个域放在单独的配置中并删除默认设置之前,它工作正常。

提前致谢!

问题是您的静态域配置中没有 server_name 指令。结果,请求总是被您的默认服务器块捕获,这似乎是您的节点应用程序。

详情见: