0

我按照这个说明为 Debian Wheezy设置了一个Vagrant VirtualBox 盒子。

我在这个虚拟机上安装了 nginx 和 php5-fpm。我可以通过127.0.0.1:8080主机访问我的访客机器。它还可以提供 php 文件并且也可以phpinfo()正常工作。

但是,当我尝试从 php 文件访问远程 MySQL 服务器时,请求总是超时并且出现504 Gateway Timeout错误。

我注意到以下几点。

  • 在我的 nginx conf 文件中,我有这一行fastcgi_pass unix:/var/run/php5-fpm.sock;
  • /etc/php5/fpm/pool.d/www.conf,我有listen = /var/run/php5-fpm.sock
  • php5-fpm.sock存在于/var/run/.
  • 如果我使用127.0.0.1:9000而不是套接字,我仍然会收到504 Gateway Timeout错误,但我会立即收到错误而无需等待。
  • proxy_read_timeout 300;在我的nginx conf中添加了,但这并没有解决问题。

我的流浪文件

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "wheezy32"

  config.vm.provision :shell, :path => "dev/bootstrap.sh"

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  config.vm.network :forwarded_port, guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network :private_network, ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network :public_network
end

我的 nginx 配置

server {
    root /var/www/sites/mysite/public_html;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name localhost;

    access_log /var/www/logs/mysite/mysite.access_log;
    error_log /var/www/logs/mysite/mysite.error_log;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules

            proxy_read_timeout 300;
    }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        allow ::1;
        deny all;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

/var/www/logs/mysite/mysite.error_log

2013/06/16 23:47:27 [error] 2567#0: *23 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 10.0.2.2, server: localhost, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock", host: "127.0.0.1:8080"
2013/06/16 23:47:27 [error] 2567#0: *23 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 10.0.2.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:8080"

这是我尝试连接到远程 MySQL 服务器的方式。

require_once('/var/www/sites/mysite/includes/db_constants.php');

try {
        $dsn = 'mysql:host=172.16.0.51;dbname=' . DB_NAME . ';charset=utf8';

        $db = new PDO($dsn,DB_USER,DB_PASS);

        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    } catch (PDOException $e) {
        header('HTTP/1.1 500');
        exit;
    } catch (Exception $e) {
        header('HTTP/1.1 500');
        exit;
    }

我错过了什么?

4

1 回答 1

0

正如@cmur2 所说,我使用私有 IP 连接到删除服务器,这就是它不起作用的原因。我将其更改为公共 IP,现在它可以正常工作。

于 2013-06-17T08:18:43.227 回答