Php

Django (uWSGI) 和 php (fastcgi) NGINX

  • February 27, 2014

我是配置伺服器的新手,在 nginx 上使用 UWSGI 讓 php 與我的 django 應用程序一起工作時遇到問題。當我刪除 django 站點 conf 時,Django 使用 uwsgi 效果很好,而 php 效果很好,但是當我同時使用 django 和 php 時,php 文件不起作用(瀏覽器只是下載它們),任何想法(請具體說明我是新手東西)?我願意接受建議,在這裡有點絕望。謝謝

這是我用於 nginx 的 django-site conf 文件:

# mysite_nginx.conf
####################

# the upstream component nginx needs to connect to
upstream django {
# server unix:///home/appdeploy/uwsgi-tutorial/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
# the port your site will be served on
listen      80;
# the domain name it will serve for
server_name 173.10.235.37; # substitute your machine's IP address or FQDN
charset     utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste

# Django media
location /media  {
   alias /home/appdeploy/v1/website/media;
 # your Django project's media files - amend as required
}

location /static {
   alias /home/appdeploy/v1/website/static;
 # your Django project's static files - amend as required
}

# Finally, send all non-media requests to the Django server.
location / {
   uwsgi_pass  django;
   include     /home/appdeploy/v1/website/uwsgi_params; # the uwsgi_params file you             installed
}
error_page  404              /404.html;
location = /404.html {
 root /home/appdeploy/v1; ## or whereever You put it - will not be needed if 404.html    is where index.html is placed
}

}

這是我執行的預設 conf 文件(使用 php fast cgi):

# default.conf

server {
listen       80;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

location / {
   root   /home/appdeploy/v1/website;
   index  index.html index.htm index.php;
}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
   root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#    root           html;
   fastcgi_pass   127.0.0.1:9000;
   fastcgi_index  index.php;
   fastcgi_param  SCRIPT_FILENAME  /home/appdeploy/v1/website$fastcgi_script_name;
   include        fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}

從我在您的配置中看到的,不再管理 PHP 導致頁面被下載。

在工作中,這裡定義了一個 PHP 解釋器:

location ~ \.php$ {
#    root           html;
   fastcgi_pass   127.0.0.1:9000;
   fastcgi_index  index.php;
   fastcgi_param  SCRIPT_FILENAME  /home/appdeploy/v1/website$fastcgi_script_name;
   include        fastcgi_params;
}

在新的上,沒有針對 php 頁面的特定參數。僅適用於預設根:

location / {
   uwsgi_pass  django;
   include     /home/appdeploy/v1/website/uwsgi_params; # the uwsgi_params file you             installed
}

如果您嘗試使用此配置,它應該可以工作:

   location / {
   #uwsgi_pass  django;
   fastcgi_pass   127.0.0.1:9000;
   include     /home/appdeploy/v1/website/uwsgi_params; # the uwsgi_params file you             installed
}

(確保 fastcgi 程序偵聽埠 9000。如果它使用套接字,請將套接字路徑而不是 127.0.0.1:9000)。如果一切正常,請告訴我們。

第2部分

您的 phpcgi 程序是如何通過文件(套接字)或 Web 埠套接字管理的?

我錯過了您的文章中的一些內容,即 nginx 配置;

upstream django {
# server unix:///home/appdeploy/uwsgi-tutorial/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

執行 lsof -i:8001,如果有東西在聽,應該是 fastcgi 程序。

如果是,您可以嘗試使用此配置(如果它沒有出現在 nginx 配置中,請添加它)?

location ~ \.php$ {
fastcgi_pass   127.0.0.1:8001;
fastcgi_index  index.php; 
# or your index name
fastcgi_param  SCRIPT_FILENAME  /home/appdeploy/v1/website$fastcgi_script_name;
include        fastcgi_params;
}

如果沒有,您的伺服器可能使用 cgi 套接字。您可以檢查套接字文件是否存在:

ps aufx | grep mysite.sock

在這種情況下,取消註釋上游的第一行並註釋第二行:

upstream django {
server unix:///home/appdeploy/uwsgi-tutorial/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

並將之前的 php 塊修改為:

location ~ \.php$ {
fastcgi_pass   django;
fastcgi_index  index.php; 
# or your index name
fastcgi_param  SCRIPT_FILENAME  /home/appdeploy/v1/website$fastcgi_script_name;
include        fastcgi_params;
}

必須啟動此套接字,檢查它們是否在 /etc/init.d/ 文件夾中

讓我知道這是否有效。

引用自:https://serverfault.com/questions/577754