【问题标题】:nginx on debian 8: multiple locations with and without phpdebian 8 上的 nginx:有和没有 php 的多个位置
【发布时间】:2017-03-15 23:13:03
【问题描述】:

我正在使用 PHP7 和 nginx 1.11.10 开发 debian 8。我拥有一个 VPS 和一个域名。

我想要做的似乎很简单:我有 2 个网站要通过 nginx 绑定到我的域。

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

我的问题是我无法让 PHP-FPM 工作,我总是遇到 502: Bad Gateway 问题 (already checked this)。

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

使用位置

这是我尝试过的配置之一:

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 文件以向这篇文章添加更多详细信息,但可能存在一些巨大的错误。

【问题讨论】:

    标签: php nginx debian


    【解决方案1】:

    您正在尝试做的是基于命名的虚拟主机,其中您有 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 请求。

    【讨论】:

    • 我试过这个配置,我认为我的问题更多是关于 php-fpm 的配置,而不是设置多个服务器块/子域。我会尽快更新我的帖子,对不起!
    猜你喜欢
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多