Debian 8 上的 nginx:多个位置有和没有 php



我正在开发带有PHP7和nginx 1.11.10的debian 8。我拥有VPS和域名。

我正在尝试做的事情似乎很简单:我有 2 个网站可以通过 nginx 绑定到我的域。

让我们假设一个在/var/www/web1下,另一个在/var/www/web2.第一个只是一个ng2应用程序,第二个需要php。

我的问题是我无法让 PHP-FPM 工作,我总是遇到 502:坏网关问题(已经检查过了)。

我认为有两种方法可以解决这个问题:使用子域和使用子位置。

使用位置

这是我尝试过的会议之一:

server {
listen 80;

root /var/www/web1;
index  index.html index.php index.htm;
# static js / html / css files
location / {
#root /var/www/lazycoding.io;
#index  index.html index.htm;
}
# needs PHP
location /web2 {
alias /var/www/web2;
#root /var/www/web2;
#index  index.php index.html index.htm;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#fastcgi_pass   127.0.0.1:9000;
#include        fastcgi_params;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
}
}

对我来说,仅在location中指定root似乎更好,但这不起作用。这就是为什么我目前正在尝试使用alias.php-fpm 套接字的路径是正确的,权限似乎也正确:

> ls -lFa /run/php
total 4
drwxr-xr-x  2 www-data www-data  80 Mar 15 21:35 ./
drwxr-xr-x 21 root     root     760 Mar 15 23:41 ../
-rw-r--r--  1 root     root       5 Mar 15 21:35 php7.0-fpm.pid
srw-rw----  1 www-data www-data   0 Mar 15 21:35 php7.0-fpm.sock=

使用子域

另一种(更性感)方法是使用子域。

  • mydomain.com重定向到 web1
  • sub.mydomain.com重定向到 web2(使用 php)

在此配置中,我似乎需要两个服务器块,每个服务器块都有一个具有先前给定属性集的location /

在此配置中,只有mydomain.com响应,而两个子域都返回 404 错误。


在这两种情况下,nginx和php-fpm服务都运行良好。此外,我总是使用nginx -t测试 nginx 的配置,并在进行更改时重新启动服务。

我对nginx/Web服务器配置没有经验,我不知道哪种方式是最好的。

我正在重写我的旧 conf 文件以向这篇文章添加更多详细信息,但也许有一些巨大的错误。

您尝试做的是基于命名的虚拟主机,其中您有 2 个共享相同 IP 地址和端口的域。以下是如何在nginx配置中进行设置的示例:

server {
listen       80;
server_name  mydomain.com;
location / {
root   /var/www/mydomain.com/public_html/;
index  index.html index.htm;
}
}
server {
listen       80;
server_name  sub.mydomain.com;
location / {
root   /var/www/sub.mydomain.com/public_html/;
index  index.html index.htm;
}
}

然后,您需要设置php-fpm,然后将服务器配置为使用它来处理 php 请求。

最新更新