无法让nginx在OSX上加载PHP

时间:2016-09-05 23:39:36

标签: php macos nginx

所以我试图让nginx和php在OSX上运行。我按照这个指南: https://www.sylvaindurand.org/setting-up-a-nginx-web-server-on-os-x/

但是设置root指向我的主文件夹中的Sites并在其中放入两个简单文件:

的index.htm

<body>
    <h1>Here I am!</h1>
    <p>NGINX is working.</p>
</body>

的index.php

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>PHP is working!</p>'; ?> 
 </body>
</html>

当我将浏览器直接指向html文件或配置nginx.conf以加载它时,它可以正常工作。但是我无法加载任何php文件,现在它只是说“找不到文件”。我环顾四周,发现有类似问题的人,但我发现的解决方案都没有解决我的问题。我是PHP的新手,所以我在黑暗中fla fla。我做错了什么想法?

这是我的nginx.conf:

user  Maddux staff;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root  /Users/Maddux/Sites/;
            index  index.php index.html index.htm;
    }

        #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   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           /Users/Maddux/Sites/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

1 个答案:

答案 0 :(得分:1)

location ~ \.php$阻止您设置root时出现混乱,并立即覆盖它。

如果您希望PHP-FPM使用与nginx相同的文档根目录来查找PHP脚本,则SCRIPT_FILENAME的值应设置为:

fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

或:

fastcgi_param  SCRIPT_FILENAME $request_filename;

在您的情况下,两个值都相同。

此外,没有必要为每个root块添加相同的location值,因为root是继承的。通常在root块的顶部附近放置一个server指令,并且只在root块中放置location指令,其中值不同。有关详情,请参阅this document