Hey there,
I have just upgraded my service from 7.3 (Ubuntu18) to 8.2 (Ubuntu22) on App Engine Flexible; I am having troubles with serving static files on PHP82 Flexible; all of my resources are being routed to index.php.
For example
Browsing to a static file on my browser /js/app.js is interpreted as index.php/js/app.js by the server or /css/main.css is sent through fastcgi as index.php/css/main.css - when it should just be served directly.
I pulled the image from the container registry and had a look at the nginx conf
It looks like in layers/google.php.webconfig/webconfig/nginxserver.conf
- server_name is empty in the nginx conf (can't find out how to add anything there via gcloud app deploy) - I understand that if there is only 1 server block it will default to the first one if no server_name could be matched
- there is a rewrite rule early on in server rewrite ^/(.*)$ /index.php$uri; - however I am unsure if this is the culprit at this stage due to lack of server_name - I tried poking around to see if there are any other server_name's however I could not see any so could very well be the case.
server {
listen 8080 default_server;
listen [::]:8080 default_server;
server_name "";
root /workspace/public;
rewrite ^/(.*)$ /index.php$uri;
location ~ ^/index.php {
...
}
include /workspace/nginx-app.conf;
}
My current solution was to check if the file exists in index.php and then pass the correct content.
This was working fine on App Engine Flexible using PHP 7.3. It looks like the lack of server_name is causing the nginx rules to not validate; otherwise the rewrite is causing havoc with how static resources are accessed.
From my tests using the nginxserver.conf that I extracted from the image, I am able to match if I set a server_name and my JS static file serve with a simple
location / {
try_files $uri index.php$is_args$args
}
HI,
I am also getting this same issue. Did u get any solution .Still I didn't get any solution for this.
Hi,
Did you get Solution for this issue
No I raised it up with Google support, with a very detailed post explaining the nginx config and the rewrite rule causing havoc
rewrite ^/(.*)$ /index.php$uri;
The support person pretty much just ignored me and said to come back here. Thank you for your amazing support google.
I am currently routing all files through index.php which is adding a very large noticeable overhead.
Hi,
Can you pls show me ur index.php file.
My sample file is
<html>
<head>
<script src='JS/jquery-3.6.4.min.js'></script>
<link rel="stylesheet" href ="CSS/StyleSheet.css" />
<script>
$(document).ready(function(){
alert('inside');
})
</script>
</head>
<body>
</body>
</html>
In my index , I have to include my JS and CSS files , the only ny application will run.
Basically this is the modification I am using; if /resources/ is detected in the URL (which are where my static files are stored) it will do a readfile instead. You will need to modify the following excerpt and put it on the top of your index.php. I am not happy with this at all and it is adding a noticeable overhead; but this will have to do before I start looking at rolling my own custom runtime solution.
if(strstr($_SERVER['SCRIPT_NAME'], '/resources/') !== false):
$e = pathinfo( $_SERVER['SCRIPT_NAME'] );
switch(strtolower($e['extension'])):
case 'css':
case 'less':
header('Content-Type: text/css');
break;
case 'png':
header('Content-Type: image/png');
break;
case 'jpeg':
case 'jpg':
case 'dng':
case 'heic':
case 'heif':
header('Content-Type: image/jpg');
break;
case 'svg':
header('Content-Type: image/svg+xml');
break;
case 'js':
header('Content-Type: text/plain');
break;
default:
header(sprintf(
'Content-Type: %s',
mime_content_type(
realpath(__DIR__) . $_SERVER['SCRIPT_NAME']
)
));
endswitch;
readfile(realpath(__DIR__) . $_SERVER['SCRIPT_NAME']);
die();
endif;
Hi,
ReadFile how will include our JS & CSS in our application.Can You show me.
Like this i am including my files .If i put this path in readfile , nothing will happen
<script src='JS/jquery-3.6.4.min.js'></script>
<link rel="stylesheet" href ="CSS/StyleSheet.css" />
You will have to test on your local machine first and explore, I can't help you any further unfortunately as I am time-poor. Experiment with using XDebug and step-through the code. If you don't know how to set up XDebug there are plenty of resources available online to point you in the right direction
HI,
How to download NGINX config file. Can you tell me.For php 8.2 deployment I need to check that one.
Hi,
Is Php 8.2 felxible Environment working for you.
Hello Community Member,
For PHP 8.2 GAE Flexible Environment only work ,if I change nginx-app.config file only.
Can you please tell is there any issue will come if we change that config file.
We need to migrate ouer application to php 82 ASAP.
I worked around it by replacing the whole nginx.conf file.
nginx_conf_override: nginx.conf # Instead of nginx_conf_include: nginx-app.conf