fangwz0577
fangwz0577

Reputation: 101

php can only access from local machine, it does not result from firewall

My server is running on CentOS 6.3 + Nginx1.4.1 + PHP 5.3.3,

The default configuration of nginx is /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;
    #}
}

I write a test script on /usr/share/nginx/html/phpinfo.php with

<?php
    phpinfo();
?>

when I execute

curl localhost/phpinfo.php

it returns the php information as follows(partial):

<!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>

However, when I input ServerIP/phpinfo.php on my computer, the browser can't open the page. I have stopped iptables service to exclude the influence of firewall.

Does anyone know how to fix this problem? Which error logs may provide some clues to help me find the reason ? I have read the error log of nginx and php-fpm on /var/log but have no ideas. :-(

/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(partial):

[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

Upvotes: 0

Views: 1809

Answers (4)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42909

Put the IP and all the names in the server_name

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

of course xx.xx.xx.xx indicate the IP you access your server with, and example.com could be a name that points to your server, add as many as you need in the same line.

Upvotes: 0

fangwz0577
fangwz0577

Reputation: 101

I always use phpinfo() to test the php environment before. A moment ago, I use other php scripts and can access the page successfully. It's weird, I can access phpinfo page when it is on my machine but can't on the server, maybe it results from the system's timezone settings. After all, thanks for everyone's attention!

Upvotes: 0

AD7six
AD7six

Reputation: 66358

You need to provide a default_server

In the question, the server is listening only to port 80 and only if the hostname is localhost. As indicated in nginx's docs, to define one server as the default - use the default server parameter in the listen directive:

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

Alternatively - use the * wildcard and full address[:port] syntax such that it will match any hostname:

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

Or, since this is the default value anyway - just remove it:

server {
    server_name  localhost;
    ...
}

Upvotes: 0

exussum
exussum

Reputation: 18578

nginx is only listening to localhost

server_name  localhost;

you need to change that to either

 server_name  *;

or

server_name  youraddress.com;

Upvotes: 1

Related Questions