訪問網站提示500錯誤,通過查看日誌,獲得錯誤資訊為:
2012/12/06 16:10:52 [alert] 11679#0: *9189 1024 worker_connections are not enough while connecting to upstream, client: 127.0.0.1, server: xxx.netingcn.com, request: "GET / HTTP/1.0", upstream: "http://127.0.0.1:80/", host: "xxx.netingcn.com"
對於上述錯誤網上很多人提到,大部分情況是在生產環境中由於並發太多造成的。解決辦法就是修改設定檔中的worker_connections值,將其調大。但是今天在原生nginx中配置一個proxy,完全沒有外界的訪問的情況也提示上述錯誤,試著修改worker_connections也沒有解決問題。proxy配置大致如下:
server { listen 80; server_name yyy.netingcn.com; location / { root /var/www/netingcn.com; index index.html index.php; }}server { listen 80; server_name xxx.netingcn.com; location / { proxy_pass http://yyy.netingcn.com/; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; }}
原本的目的是想把所有請求xxx.netingcn.com代理到yyy.netingcn.com,也許你會納悶,幹嘛要這麼麻煩,直接在第一個的server_name中添加xxx.netingcn.com不就完事。這裡只是一個舉例,由於正式環境下有特殊情況,不能那麼配置,所有才會想到用proxy的方式。
上述配置造成的500錯誤可以肯定排除是worker_connections太小的原因,仔細配置,發現“proxy_set_header Host $http_host;”這個才是罪魁禍首,因為它造成死迴圈了,當請求xxx時,由於proxy到yyy,本來nginx是交給server yyy,來處理的,但是header裡面的host還是xxx,nginx根據配置裡面的server_name,找到 xxx,因此就顯示了死迴圈,也就會報前面提到的那個錯了。問題已經定位到了,解決就很簡單,直接去掉proxy_set_header Host $http_host;這行配置就行了。