php只能从本地机器访问,它不是防火墙造成的

时间:2013-06-05 08:20:15

标签: nginx php

我的服务器在CentOS 6.3 + Nginx1.4.1 + PHP 5.3.3上运行,

nginx的默认配置是/etc/nginx/default.conf:

server {
    listen       80;
    server_name  localhost;

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

    location / {
        root   /usr/share/nginx/html;
        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  /usr/share/nginx/html$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;
    #}
}

我在/usr/share/nginx/html/phpinfo.php上写了一个测试脚本

<?php
    phpinfo();
?>

执行时

curl localhost/phpinfo.php

它返回php信息如下(部分):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html><head>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
body, td, th, h1, h2 {font-family: sans-serif;}
pre {margin: 0px; font-family: monospace;}
a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
a:hover {text-decoration: underline;}
table {border-collapse: collapse;}
.center {text-align: center;}
.center table { margin-left: auto; margin-right: auto; text-align: left;}
.center th { text-align: center !important; }
td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
h1 {font-size: 150%;}
h2 {font-size: 125%;}
.p {text-align: left;}
.e {background-color: #ccccff; font-weight: bold; color: #000000;}
.h {background-color: #9999cc; font-weight: bold; color: #000000;}
.v {background-color: #cccccc; color: #000000;}
.vr {background-color: #cccccc; text-align: right; color: #000000;}
img {float: right; border: 0px;}
hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}
</style>
<title>phpinfo()</title><meta name="ROBOTS" content="NOINDEX,NOFOLLOW,NOARCHIVE" /></head>
<body><div class="center">
<table border="0" cellpadding="3" width="600">
<tr class="h"><td>
<a href="http://www.php.net/"><img border="0" src="/phpinfo.php?=PHPE9568F34-D428-11d2-A769-00AA001ACF42" alt="PHP Logo" /></a><h1 class="p">PHP Version 5.3.3</h1>
</td></tr>
</table><br />
<table border="0" cellpadding="3" width="600">
<tr><td class="e">System </td><td class="v">Linux localhost.localdomain 2.6.32-358.el6.i686 #1 SMP Thu Feb 21 21:50:49 UTC 2013 i686 </td></tr>
<tr><td class="e">Build Date </td><td class="v">Feb 22 2013 02:38:57 </td></tr>

但是,当我在计算机上输入ServerIP / phpinfo.php时,浏览器无法打开该页面。我已经停止了iptables服务以排除防火墙的影响。

有谁知道如何解决这个问题?哪些错误日志可能提供一些线索来帮助我找到原因?我已经在/ var / log上读过nginx和php-fpm的错误日志,但是没有想法。 : - (

/var/log/php-fpm/error.log:

[05-Jun-2013 15:53:39] NOTICE: fpm is running, pid 19292
[05-Jun-2013 15:53:39] NOTICE: ready to handle connections
[05-Jun-2013 16:02:32] NOTICE: Terminating ...
[05-Jun-2013 16:02:32] NOTICE: exiting, bye-bye!
[05-Jun-2013 16:02:32] NOTICE: fpm is running, pid 19387
[05-Jun-2013 16:02:32] NOTICE: ready to handle connections

/var/log/php-fpm/www-error.log(部分):

[05-Jun-2013 16:37:54] PHP Warning:  phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Chongqing' for 'CST/8.0/no DST' instead in /usr/share/nginx/html/index.php on line 2

4 个答案:

答案 0 :(得分:1)

nginx只是在听localhost

server_name  localhost;

您需要将其更改为

 server_name  *;

server_name  youraddress.com;

答案 1 :(得分:0)

您需要提供default_server

在问题中,如果主机名为localhost,则服务器仅到端口80,。如nginx's docs所示,要将一台服务器定义为默认服务器,请使用listen指令中的default server参数:

server {
    listen       80  default_server;
    server_name  example.org;
    ...
}

或者 - 使用*通配符和完整address[:port]语法,使其匹配任何主机名:

server {
    listen       *:80;
    server_name  localhost;
    ...
}

或者,因为这是默认值 - 只需删除它:

server {
    server_name  localhost;
    ...
}

答案 2 :(得分:0)

我总是使用phpinfo()来测试php环境。刚才,我使用其他php脚本,可以成功访问该页面。这很奇怪,我可以访问phpinfo页面,当它在我的机器上,但不能在服务器上,也许它系统时区设置的结果。毕竟,感谢大家的关注!

答案 3 :(得分:0)

将IP和所有名称放在server_name

server_name: localhost xx.xx.xx.xx example.com;

当然xx.xx.xx.xx表示您访问服务器的IP,example.com可以是指向您服务器的名称,在同一行中添加所需数量。