0

Attempting to update an Azure Linux Web App to Craft 4. So I updated the Linux Web App to PHP 8. But now it’s using Nginx instead of Apache (would have been nice to know they change that when you switch PHP versions...)

Craft installed fine from the command line, and is running. But the default Nginx config from Azure returns 404 errors. Here is the default config. How can I fix it to get Craft to work?

server {
    #proxy_cache cache;
        #proxy_cache_valid 200 1s;
    listen 8080;
    listen [::]:8080;
    root /home/site/wwwroot;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com; 

    location / {            
        index  index.php index.html index.htm hostingstart.html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /html/;
    }
    
    # Disable .git directory
    location ~ /\.git {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Add locations of phpmyadmin here.
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_connect_timeout         300; 
        fastcgi_send_timeout           3600; 
        fastcgi_read_timeout           3600;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }
}

1 Answer 1

1

Typically a webserver knows how to serve static/already existing files just fine but Craft (and other CMS for that matter) are dealing with things like /my-entry or whatever that don't actually "exist" on the filesystem, which is why you're getting a 404 error. So we need to tell nginx how to serve those sorts of requests.

The main thing you'll want to edit is near the top under location / {. It should be:

location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

So now when a request comes in, nginx will see if a request is actually a file that already exists and since it is not, pass it back to PHP.

That will Craft working. If you're doing localization or other things, you may need to tweak your config a bit.

When you hit production you may want to look for more robust configs that will deal with turning off logging for static files, effective headers for caching, etc.

2
  • 1
    Thank you very much for the assistance! Through this, I got better acquainted with how the nginx config works, and was able to get everything up and running.
    – M Smith
    Commented Aug 23, 2022 at 18:48
  • Great - You bet! Commented Aug 24, 2022 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.