nginx proxy 配置不當導致500錯誤

來源:互聯網
上載者:User

訪問網站提示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;這行配置就行了。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.