nginx:兩個網域 -> 兩個應用程式 -> 一台伺服器

nginx:兩個網域 -> 兩個應用程式 -> 一台伺服器

這本來應該很簡單,但我一整天都在努力設定它。這是我的conf檔:

# Expires map
  map $sent_http_content_type $expires {
    default                    off;
    text/html                  epoch;
    text/css                   max;
    application/javascript     max;
    ~image/                    max;
}


server {
    listen  80;
    server_name www.firstdomain.com firstdomain.com;
    root /path/to/project/;
    error_log /var/log/nginx/error.log;

}

server {
    listen 443 ssl http2; # managed by Certbot
    server_name www.firstdomain.com;
    error_log /var/log/nginx/error.log;

    proxy_read_timeout  90;
    expires $expires;

    location / {
        include proxy_params;
        proxy_read_timeout  90;
        proxy_pass http://xx.xxx.xxx.xxx:8001;

    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        alias /path/to/static/;
    }

    location /media/ {                        
        alias /path/to/media/;
    }

    location  /robots.txt {
        alias  /path/to/robots.txt;
    }

    ssl_certificate /etc/letsencrypt/live/firstdomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/firstdomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    if ($host = 'firstdomain.com') {
        return 301 https://www.firstdomain.com$request_uri;
    }

    if ($scheme != "https") {
        return 301 https://www.firstdomain.com$request_uri;
    } # managed by Certbot
}

這是我的第二個網域,儲存在可用網站的單獨檔案中:

server {
    listen 80;
    server_name www.second-domain.com second-domain.com;
    error_log my-domain-path/nginx.log;

    location /
    {
        proxy_pass http://www.second-domain.com:8002;
    }
}

這兩個應用程式均由主管管理,並且都成功啟動。但是,當我導航到 www.second-domain.com 時,我得到了 firstdomain.com 的內容。

我確信第二個網域內容可以正常工作並運行,因為如果我導航到 IP:PORT 位置,我可以看到它,但它不適用於網域。

為什麼?

編輯:根據請求 nginx -t 的結果

 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
 nginx: configuration file /etc/nginx/nginx.conf test is successful

相關內容