2013-04-25 82 views
9

我正嘗試在兩個端口上配置nginx,例如在端口80和端口81上,但目前還沒有運行。這裏是什麼,我試圖做一個例子:如何在多個端口上運行Nginx

worker_processes 1; 

events { 
    worker_connections 1024; 
} 

http { 
    include  mime.types; 
    default_type application/octet-stream; 
    sendfile  on; 
    keepalive_timeout 65; 

    server { 
     listen 80; 
     server_name chat.local.com; 

     location/{ 
      proxy_pass http://127.0.0.1:8080; 
      proxy_http_version 1.1; 
      proxy_set_header Upgrade $http_upgrade; 
      proxy_set_header Connection "Upgrade"; 
      proxy_set_header Host $host; 
      proxy_buffering off; 

     } 
     error_page 500 502 503 504 /50x.html; 
     location = /50x.html { 
      root html; 
     } 

    } 

    server { 
     listen 81; 
     server_name console.local.com; 
     location/{ 
      proxy_pass http://127.0.0.1:8888; 
      proxy_http_version 1.1; 
      proxy_set_header Upgrade $http_upgrade; 
      proxy_set_header Connection "Upgrade"; 
      proxy_set_header Host $host; 
      proxy_buffering off; 
     } 
    } 
} 

當我嘗試運行console.local.com,它顯示了從chat.local.com內容。有沒有辦法讓nginx運行在兩個端口上?提前致謝!

回答

7

你的配置看起來不錯

我認爲這個問題是這樣的(糾正我,如果我錯了):

  • 你console.local.com 81端口監聽,
  • 那意味着你需要訪問它作爲http://console.local.com:81/
  • 當你訪問它作爲http://console.local.com/(沒有顯式的端口,所以默認爲端口80) nginx將檢查,注意到注意到正在監聽端口80爲該server_name,並利弊經常會將請求傳遞給默認的服務器塊。由於默認的服務器塊是第一個(在沒有配置來改變它的情況下),你最終將進入chat.local.com處理。

在所有的情形產生,你想改變你的console.local.com到偵聽端口80還因爲:

  • 的服務器名指令在兩個serverblocks足以區分
  • 避免請求你必須在請求中始終添加:81到域名
+0

嗨Cobaco,我完全忘了,我需要用端口訪問頁81.感謝您指出該點的點。 – Uday 2013-04-25 07:17:05

3

你可以添加2個簡單的listen語句;像下面
聽80;
listen 81;

這應該與nginx的工作

相關問題