2016-10-20 61 views
0

我正在嘗試使用django,gunicorn和nginx設置一個項目,並且遇到了nginx配置的問題。更確切地說,當我使用try_filesnginx的try_files設置的問題

如果我使用if (!-f $request_filename) {...}一切工作正常,但如果使用 try_files ... Django的生成異常:

Invalid HTTP_HOST header: 'myproject_server'. The domain name provided is not valid according to RFC 1034/1035.

一旦一切正常使用if ...我認爲其他設置 (gunicorn等)是正確的。

我使用的配置文件是:

/home/myproject/myproject/settings.py(django的)

... 
ALLOWED_HOSTS = [192.168.200.100, ] 
... 

的/ etc/nginx的/ sites- available/myproject(this one WORKS)

upstream myproject_server { 
    unix server:/home/myproject/run/gunicorn.sock fail_timeout = 0; 
} 
server { 
    listen 80; 
    server_name 192.168.200.100; 
    root /home/myproject; 
    location /media/ {} 
    location /static/ {} 
    location/{ 

     proxy_set_header Host $HTTP_HOST; 
     proxy_set_header X-Real-IP $REMOTE_ADDR; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Forwarded-Proto $scheme; 

     if (!-f $request_filename) { 
      proxy_pass http://myproject_server; 
      break; 
     } 
    } 
} 

的/ etc/nginx的/網站可用/ myproject的(這其中不工作)

upstream myproject_server { 
    unix server: /home/myproject/run/gunicorn.sock fail_timeout = 0; 
} 
server { 
    listen 80; 
    server_name 192.168.200.100; 
    root /home/myproject; 
    location /media/ {} 
    location /static/ {} 
    location/{ 

     proxy_set_header Host $HTTP_HOST; 
     proxy_set_header X-Real-IP $REMOTE_ADDR; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Forwarded-Proto $scheme; 

     try_files $uri @myproject_backend; 
    } 
    location @myproject_backend { 
     proxy_pass $scheme://myproject_server; 
    } 
} 

我在做什麼錯?

在此先感謝任何幫助。

PS:英語不是我的母語,所以我很抱歉(很多)錯誤。

+0

你應該設置'proxy_set_header'-s到最後'位置' –

+0

嗨阿列克謝十,現在它工作正常!謝謝。 – 3WZ

回答

2

proxy_set_header應與proxy_pass位於相同的位置。

location/{ 
    try_files $uri @myproject_backend; 
} 

location @myproject_backend { 
    proxy_set_header Host $HTTP_HOST; 
    proxy_set_header X-Real-IP $REMOTE_ADDR; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_pass http://myproject_server; 
}