How to get a flexible default to index.html or index.php?

Dear All,

I’m playing around here with nginx server settings on a local Windows machine (learning how it all works), and am just wondering how to set the system up so that it will default to index.html, but will accept index.php if index.html isn’t available. I copied the settings from the setup walked through on this Github tutorial.The only thing that I’ve changed is the third line, adding index.html ahead of index.php, and it’s clearly not enough, as I get a 404 Not Found error when I remove the .php file. Also, even when there’s no index.php file, a file with a name such as info.php still takes priority over index.html.

I hope that was clear. Anyway, what else do I need to do in the settings?

(the reason that I’m wanting to do this, by the way, is just to allow myself the future flexibility of switching between Hugo for some parts of my site, and a PHP/MySQL-based CMS for others - I hope that makes sense!!!)

((Oh, and I’ll be trying to do the same thing with Apache - just to compare them, and figure out which one I’m going to use for the actual site!))

 server {
    listen 80;
    server_name localhost;
    index index.html index.php;
    error_log d:/www_servers/nginx/logs/localhost.error.log;
    access_log d:/www_servers/nginx/logs/localhost.access.log;
    root d:/www;

    location / {
        try_files $uri /index.php$is_args$args;
	}

    location ~ \.php {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9123;
    }
}

I am an Nginx beginner. I managed to get Nginx to host (reverse proxy; I still do not know what that means) an ASP.Net Core (Kestrel) application, including use of SSL certificates, but otherwise I know very little. It took me a few months of studying and researching.

Do you know if you are using fastcgi or PHP-FPM? I barely know the difference and all I know is from searching. Apparently the $is_args$args in the try_files is for PHP-FPM.

When I search for articles about configuring Nginx for PHP I find other configurations, some much simpler. PHP FastCGI Example | NGINX looks quite different. Also PHP: Nginx 1.4.x on Unix systems - Manual looks simpler to me.

No idea, to be honest. It certainly is all a bit confusing, but I’m determined to understand eventually.

I’m going to start learning Go, anyway, as advised on the previous question. I’ll start that in earnest this coming week.

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