Django(uWSGI)和php(fastcgi)NGINX

我是configuration服务器的新手,我有问题让PHP使用我的django应用程序在nginx上使用UWSGI。 Django的伟大作品使用uwsgi和PHP伟大的时候,我删除了Django网站的conf,但是当我一起使用Django和PHP一起PHP文件不工作(浏览器只是下载它们),任何想法(请具体我是新来的东东)? 我接受build议,在这里变得有点绝望。 谢谢

这里是我的nginx的django站点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 } } 

这里是我运行的默认configuration文件(与PHP的快速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; #} } 

从我在configuration中看到的,PHP不再被pipe理,导致页面被下载。

在工作之一,这里定义了一个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 } 

如果你尝试这个configuration,它应该工作:

  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.如果使用套接字,请将套接字pathreplace为127.0.0.1:9000)。 让我们知道是否一切正常。

第2部分

你的phpcgi进程如何pipe理,通过文件(套接字)或networking端口套接字?

我错过了你的文章,nginx的configuration;

 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过程。

如果是的话,你可以试试这个configuration(如果它没有出现在nginxconfiguration中,就添加它)?

 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/文件夹中

让我知道如果这工作。

我认为这里的问题是你的server_nameconfiguration项,它指定了应该匹配使用特定configuration的域名。

PHP的默认configuration仅适用于localhost,而Djangoconfiguration适用于您指定的IP地址。

我假设你想在同一个虚拟主机/网站上运行PHP / Django。 然后,您需要在与Djangoconfiguration相同的configuration中应用PHP位置块,然后确保您的server_name设置已被注释掉或与您的网站域匹配。

那么你必须禁用其他configuration。