簡體   English   中英

Nginx重定向到錯誤的vhost

[英]Nginx redirecting to wrong vhost

我在一個nginx conf文件中有大約1300vhosts。 全部具有以下布局(它們在vhost文件中彼此列出)。

現在我的問題是有時我的瀏覽器會將site2重定向到site1。 出於某種原因,雖然域名沒有事件匹配。

看起來nginx總是重定向到vhosts文件中的第一個站點。

有人知道這個問題是什么嗎?

server {
    listen   80;

    server_name site1.com;
    rewrite ^(.*) http://www.site1.com$1 permanent;
}

server {
    listen   80;

    root /srv/www/site/public_html/src/public/;
    error_log /srv/www/site/logs/error.log;
    index index.php;

   server_name www.site1.com;

    location / {
        if (!-e $request_filename) {
            rewrite ^.*$ /index.php last;
        }
    }

    location ~ .(php|phtml)$ {
        try_files $uri $uri/ /index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/site/public_html/src/public$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

server {
    listen   80;

    server_name site2.com;
    rewrite ^(.*) http://www.site2.com$1 permanent;
}

server {
    listen   80;

    root /srv/www/site/public_html/src/public/;
    error_log /srv/www/site/logs/error.log;
    index index.php;

   server_name www.site2.com;

    location / {
        if (!-e $request_filename) {
            rewrite ^.*$ /index.php last;
        }
    }

    location ~ .(php|phtml)$ {
        try_files $uri $uri/ /index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/site/public_html/src/public$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

編輯也許另外要提的是,我使用nginx -s reload每2分鍾重新加載所有這些虛擬主機。

在第一次測試時看起來重定向僅在重新加載時發生...要做更多的測試,但這可能會有所幫助..

參考(nginx如何處理請求): http//nginx.org/en/docs/http/request_processing.html

在此配置中,nginx僅測試請求的標頭字段“Host”,以確定請求應路由到哪個服務器。 如果其值與任何服務器名稱都不匹配,或者請求根本不包含此標頭字段,則nginx會將請求路由到此端口的默認服務器。

默認服務器是第一個 - 這是nginx的標准默認行為

你能檢查那些不良請求的主機頭嗎?

您還可以創建顯式默認服務器來捕獲所有這些錯誤請求,並將請求信息(即$ http_host)記錄到不同的錯誤日志文件中以供調查。

server {
    listen       80  default_server;
    server_name  _;
    error_log /path/to/the/default_server_error.log;

    return       444;
}

[ 更新 ]當您正在進行nginx -s reload並且在該nginx conf文件中有這么多域時,可能存在以下情況:

重裝是這樣的

使用新配置啟動新的工作進程,正常關閉舊工作進程

所以老工人和新工人可以共存一段時間。 例如,當您向配置文件添加新的服務器塊(使用新域名)時,在重新加載時,新工作人員將擁有新域,舊域將不會。 當請求恰好由舊工作進程發送時,它將被視為未知主機並由默認服務器提供服務。

你說它每2分鍾完成一次。 你能跑嗎?

ps aux |grep nginx

並檢查每個工人的運行時間? 如果超過2分鍾,重新加載可能無法按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM