nginx does not execute the index.php file in http://localhost root and just downloads the index.php file

StackOverflow https://stackoverflow.com/questions/22274470

Question

nginx does not execute an index.php file in the http://localhost root. I also changed the default_type application/octet-stream; to default_type application/html; but no luck. Below is the nginx.conf file:

user  nobody;
worker_processes  1;
error_log /usr/local/Cellar/nginx/1.4.6/logs/error.log;
pid       /usr/local/Cellar/nginx/1.4.6/logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /usr/local/etc/nginx/mime.types;
    include       /usr/local/etc/nginx/fastcgi.conf;
    default_type  application/octet-stream;
    access_log    /usr/local/var/log/nginx/access.log;

    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
    server {
        listen       80;
        server_name  localhost;
        access_log  /usr/local/Cellar/nginx/1.4.6/logs/localhost.access.log  combined;

        location / {
            root   /Users/apiah/Websites/test;
            index  index.html index.htm index.php;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        /usr/local/etc/nginx/fastcgi_params;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

Also, here is the information from nginx -V command in terminal:

TLS SNI support enabled configure arguments: --prefix=/usr/local/Cellar/nginx/1.4.6 --with-http_ssl_module --with-pcre --with-ipv6 --sbin-path=/usr/local/Cellar/nginx/1.4.6/bin/nginx --with-cc-opt='-I/usr/local/Cellar/pcre/8.34/include -I/usr/local/Cellar/openssl/1.0.1f/include' --with-ld-opt='-L/usr/local/Cellar/pcre/8.34/lib -L/usr/local/Cellar/openssl/1.0.1f/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-http_gzip_static_module

Thanks for your help.

Was it helpful?

Solution

First of all, root directive inside location is perfectly valid, but there are few cases when it's appropriate and this one is definitely not the case. Read common pitfalls.

Second, relative root path (root html; in your case) is relative to nginx default path. Do you know what it is? Yes, it's /usr/local/Cellar/nginx/1.4.6 as stated in your post. So root html; equivalent root /usr/local/Cellar/nginx/1.4.6/html;. I guess, there is no PHP file.

Third, nginx does not execute anything. In this case nginx proxies your request to fastcgi application on port 9000 (I guest, it's php-fpm or something like this).

Also, there are lot of useful messages in error log. Try to look into it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top