My nginx webserver server serves well html but fails with php

Code:
cat nginx.conf
user  www www;
worker_processes  auto;

# This default error log path is compiled-in to make sure configuration parsing
# errors are logged somewhere, especially during unattended boot when stderr
# isn't normally logged anywhere. This path will be touched on every nginx
# start regardless of error log location configured here. See
# https://trac.nginx.org/nginx/ticket/147 for more info.
#
#error_log  /var/log/nginx/error.log;
#

#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; # Change this to your domain name or IP address

    # Default web root
    root         /usr/local/www/nginx; # Or your desired web root, e.g., /usr/local/www/mywebsite.com

    # Add index.php to the index directive so Nginx serves it automatically
    index        index.html index.htm index.php;

    # Location block for static files (optional, but good practice)
    location / {
        try_files $uri $uri/ =404;
    }

    # Pass PHP scripts to PHP-FPM via TCP
    location ~ \.php$ {
        alias /usr/local/www/nginx/;
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000; # This is the crucial line for TCP socket
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params; # Includes common FastCGI parameters
        autoindex on; # This enables directory listing
        # Optional: Further configure autoindex behavior
        autoindex_exact_size off; # Display sizes as KB, MB, GB
        autoindex_format html; # (default) or xml, json, jsonp
        autoindex_localtime on; # Use local time for file modification dates
    }

    # Error pages
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/www/nginx-dist;
    }

}


}
 
The idea is once i have this running , i can server zabbix. But the struggle is hard :). Have to start first with a working nginx+php_fpm+tcp(no socket) , working configuration. Any guidelines to debug ? checks ?
 
there are several things to check, or add to your problem.

You have an index order, any change you have index.html/htm and index.php in there?
there is no need for an 'alias' in the .php block

Code:
    location ~ \.php$ {
        try_files $uri $uri/ =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_index index.php;
    }

But above all, you forget to mention what actually happens when you do request a php file. e.g. a phpinfo.php with <? phpinfo(); inside
 
Nginx is very chatty and specific about errors - check the logs.
Same goes for php-fpm - check its logs. Is it even running? (-> sockstat -l)
Also: for local connections just use unix sockets, they are faster and also work in jails that don't have their own loopback interfaces (e.g. non-vnet jails).
 
Currently nginx serves php,
I give my config file,

Code:
cat nginx.conf
user  www www;
worker_processes  auto;

# This default error log path is compiled-in to make sure configuration parsing
# errors are logged somewhere, especially during unattended boot when stderr
# isn't normally logged anywhere. This path will be touched on every nginx
# start regardless of error log location configured here. See
# https://trac.nginx.org/nginx/ticket/147 for more info.
#
#error_log  /var/log/nginx/error.log;
#

#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 myfreebsd; # Change to your domain name or IP address
    root /usr/local/www/nginx; # Your web root

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    # This block processes PHP files
    location ~ \.php$ {
        root /usr/local/www/nginx; # Ensure this matches your web root
        fastcgi_pass 127.0.0.1:9000; # Use the IP address and port here
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

}
root@:/usr/local/etc/nginx #
 
Back
Top