Nginx php issue

Hi,

I am using openwrt24.10.1 vm in virtualbox. I installed nginx-full and all php8 packages. I placed very simple test file test.html and phpinfo.php in /www folder. In browser I type 192.168.56.2/test.html, it works properly, but when I type 192.168.56.2/phpinfo.php, it shows "File not found". Wonder if someone could advise how to make php work. Thanks.

/etc/config/nginx:

config main 'global'
	option uci_enable 'true'

config server '_lan'
	list listen '443 ssl default_server'
	list listen '[::]:443 ssl default_server'
	option server_name '_lan'
	list include 'restrict_locally'
	list include 'conf.d/*.locations'
	option uci_manage_ssl 'self-signed'
	option ssl_certificate '/etc/nginx/conf.d/_lan.crt'
	option ssl_certificate_key '/etc/nginx/conf.d/_lan.key'
	option ssl_session_cache 'shared:SSL:32k'
	option ssl_session_timeout '64m'
	option access_log 'off; # logd openwrt'

config server '_redirect2ssl'
	list listen '80'
	list listen '[::]:80'
	option server_name '_redirect2ssl'
	option return '302 https://$host$request_uri'


/etc/nginx/conf.d/php.locations

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    # Mitigate https://httpoxy.org/ vulnerabilities
    fastcgi_param HTTP_PROXY "";
    #error_log /dev/null;
    fastcgi_connect_timeout 300s;
    fastcgi_read_timeout 300s;
    fastcgi_send_timeout 300s;
    fastcgi_buffer_size 32k;
    fastcgi_buffers 4 32k;
    fastcgi_busy_buffers_size 32k;
    fastcgi_temp_file_write_size 32k;
    client_body_timeout 10s;
    send_timeout 60s; # default, increase if experiencing a lot of timeouts.
    output_buffers 1 32k;
    fastcgi_index index.php;
    fastcgi_pass unix:/var/run/php8-fpm.sock;
    # include the fastcgi_param setting
    include fastcgi_params;

    # SCRIPT_FILENAME parameter is used for PHP FPM determining
    #  the script name. If it is not set in fastcgi_params file,
    # i.e. /etc/nginx/fastcgi_params or in the parent contexts,
    # please comment off following line:
    # fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
}

log shows "daemon.err nginx[4403]: 2025/05/01 20:21:58 [error] 9046#0: *14 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.56.1, server: _lan, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php8-fpm.sock:", host: "192.168.56.2".

Uncomment this line, restart the nginx service and try again.

Thank you. That works! Could you explain a little bit about this?

The SCRIPT_FILENAME parameter (which also specifies the nginx document root directory) is not defined in /etc/nginx/fastcgi_params.

The defined SCRIPT_NAME parameter uses a relative path, FastCGI looks for the file in the wrong location and as a result the file cannot be found.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.