Nginx,提供錯誤的站點

Nginx,提供錯誤的站點

我的網站 IP 為 1.2.3.4

在我的網域供應商上,onesite.com 和 anothersite.com 都指向 1.2.3.4

使用 Nginx,我設定了兩個網站:

server {
    listen 1.2.3.4:80;
    server_name www.oneserver.com;
    rewrite ^(.*) http://onserver.com$1 permanent;
}

server {
    listen 1.2.3.4:80;
    server_name onserver.com;

    location / {
        fastcgi_pass 127.0.0.1:8878;

    [..]

和:

server {
    listen 1.2.3.4:80;
    server_name myapp.anotherserver.com;

    location / {
        fastcgi_pass unix:/tmp/myapp.sock;

    [..]

當我造訪 myapp.anotherserver.com 時,我被重新導向到 oneserver.com

有什麼幫助嗎?

答案1

如所user186340指出的,您的配置片段看起來不錯,並且myapp.anotherserver.com連接埠存取80應該在您提供的第三個區塊中提供。如果它不像您描述的那樣工作,可能是因為它沒有加載。

  1. 確保 nginx 找到/載入了您在此處顯示的所有配置
  2. 用於nginx -t驗證您的配置
  3. 當您發送錯誤訊息時監控您的錯誤日誌HUP訊號到 nginx 主程序以發現彈出的任何錯誤訊息
  4. 如果您使用的是 nginx v1.9.2+,您可能想要使用nginx -T將載入的配置轉儲到標準輸出

如果以下所有內容均正常,則與您實際使用的配置相比,您可能修改了顯示的配置。

答案2

問題是 Nginx 選擇預設伺服器並為沒有明確定義伺服器的任何請求提供服務。我的解決方法是定義一個不執行任何操作的預設伺服器。

# This just prevents Nginx picking a random default server if it doesn't know which
# server block to send a request to
server {
    # You can add 443/ssl if you need to
    listen      80 default_server; 
    server_name _;

    access_log off; log_not_found off;

    # "I'm a teapot", effectively "go away" https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
    # Code 403 (forbidden), 410 (gone) or 501 (not implemented) is probably a better choice https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
    return 418; 
}

相關內容