Nette $basePath problem when nette is running inside sub-directory

steelbull
Member | 241
+
0
-

Example nette app is running without any problem inside Docker (with NGINX and PHP-FPM) container. Docker container is configured to expose nette app on 127.0.0.1:9090 port.

Next I have configured on localhost NGINX server which is running as a proxy and makes available the nette app on: https://example.com And nette works without any problem.

THIS CONFIGURATION WORKS:

server {
	listen 443 ssl default_server;
	listen [::]:443 ssl default_server;

	ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
	ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;

	server_name _;

        location / {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_cache_bypass $http_upgrade;

                proxy_pass http://localhost:9090/;
        }
}

Problem starts, when I would like to run this nette app in subdirectory for example https://example.com/nette . Nette application works, but problem is with all assets, which are not correct, because $template->basePath is set to “/” instead of “/nette”, which is not correct.

THIS CONFIGURATION IS NOT WORKING

server {
	listen 443 ssl default_server;
	listen [::]:443 ssl default_server;

	ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
	ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;

	server_name _;

        location /nette { # <-- here is the difference
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_cache_bypass $http_upgrade;

                proxy_pass http://localhost:9090/;
        }
}

The main point is, how to tell or setup to nette correct $basePath

Thanks a lot for an each answer.

steelbull
Member | 241
+
0
-

I finally solved it! Somewhere to the blog I will write deeper info.

Carrousel4695
Member | 2
+
0
-

Hello! Would you mind sharing your solution?
I found an older Nette application of mine, made it into a Docker image hosted with the built-in standalone PHP server (it's a tool for me, not something public, so I don't think I need nginx/php-fpm) and now I'm trying to make it work under a sublocation in nginx, like /some/app. The $basePath is set to /, so all the images are being fetched outside of the sublocation (/img.png instead of /some/app/img.png). All the links are broken the same way.
Thanks!

m.brecher
Generous Backer | 864
+
0
-

@Carrousel4695

The $basePath is set to /

In Nette there are two different $basePaths. One commonly used is in latte templates in variable $basePath and never ends with ‘/’. Another one is in \Nette\Http\Request where you may get it with $request->getUrl()->getBasePath() but this one $basePath ends with ‘/’

So if your $basePath really ends with / the question I submit to You is – which one $basePath do you mean ??

m.brecher
Generous Backer | 864
+
0
-

@steelbull

Somewhere to the blog

Please which one blog do you mean ?

Carrousel4695
Member | 2
+
0
-

I meant the {$basePath} in templates. When I had the application served via the built-in PHP server, this one basePath was always set to /, no matter what I tried. There were several links to some files, and due to them being located at {$basePath}/src/..., I ended up with 404, because it was proxied under a nginx location /something/ and instead of the paths being relative, it tried to access /src/... instead of /something/src/....

After wasting half of the day, I decided to use NGINX + PHP-FPM instead. Made a docker image so that I can set the location path via an environment variable and it would be available under that path. The config is as follows, if anyone in the future may need it:

server {
	listen 80;
	server_name default_server;
	location <SED_BASE_PATH> {
	alias /path/to/project/www/;
	index index.php;
	try_files $uri $uri/ <SED_BASE_PATH><SED_BASE_PATH>index.php$is_args$args;
	location ~ \.php$ {
		fastcgi_pass unix:/run/php-fpm83/php.sock;
		fastcgi_param SCRIPT_FILENAME $request_filename;
		include /etc/nginx/fastcgi_params;
	}
}

With this config, you can replace <SED_BASE_PATH> with /, /some/thing/, /any/thing/ and it would work just fine.

  • try_files works fine if the location root is set, however if alias is needed instead as in my case, the <SED_BASE_PATH> must be there twice, due to an nginx bug.
  • A nice Alpine BusyBox-version sed can replace <SED_BASE_PATH> with the value of an environment variable ${BASE_PATH} like this: sed -i "s@<SED_BASE_PATH>@${BASE_PATH}@g" nginx.conf

Pěkný den :)