How can I modify the nginx configuration in Railway?

I'm getting a "413 Request Entity Too Large" when uploading images to my Laravel app deployed with Railway (which is using Nixpacks). I think that this can be solved by modifying the "client_max_body_size 20M;" in in /etc/nginx/nginx.conf. However, I can't figure out how can I access this configuration file Railway. How can I make such a configuration in Railway?
15 Replies
Percy
Percy2y ago
Project ID: 49a4d477-fb25-4e0e-94a5-120cedd527f2
nilsuria
nilsuriaOP2y ago
49a4d477-fb25-4e0e-94a5-120cedd527f2
njoguamos
njoguamos2y ago
Create a nginx.template.config file in the root of your application. Add the content as follows:
worker_processes 5;
daemon off;

worker_rlimit_nofile 8192;

events {
worker_connections 4096; # Default: 1024
}

http {
include $!{nginx}/conf/mime.types;
index index.html index.htm index.php;

default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stdout;
error_log /dev/stdout;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128; # this seems to be required for some vhosts


client_max_body_size 20M; # Modify this to fit you needs

server {
listen ${PORT};
listen [::]:${PORT};
server_name localhost;

$if(NIXPACKS_PHP_ROOT_DIR) (
root ${NIXPACKS_PHP_ROOT_DIR};
) else (
root /app;
)

add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";

index index.php;

charset utf-8;

$if(IS_LARAVEL) (
location / {
try_files $uri $uri/ /index.php?$query_string;
}
) else ()

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

$if(IS_LARAVEL) (
error_page 404 /index.php;
) else ()

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include $!{nginx}/conf/fastcgi_params;
include $!{nginx}/conf/fastcgi.conf;
}

location ~ /\.(?!well-known).* {
deny all;
}
}
}
worker_processes 5;
daemon off;

worker_rlimit_nofile 8192;

events {
worker_connections 4096; # Default: 1024
}

http {
include $!{nginx}/conf/mime.types;
index index.html index.htm index.php;

default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stdout;
error_log /dev/stdout;
sendfile on;
tcp_nopush on;
server_names_hash_bucket_size 128; # this seems to be required for some vhosts


client_max_body_size 20M; # Modify this to fit you needs

server {
listen ${PORT};
listen [::]:${PORT};
server_name localhost;

$if(NIXPACKS_PHP_ROOT_DIR) (
root ${NIXPACKS_PHP_ROOT_DIR};
) else (
root /app;
)

add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";

index index.php;

charset utf-8;

$if(IS_LARAVEL) (
location / {
try_files $uri $uri/ /index.php?$query_string;
}
) else ()

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

$if(IS_LARAVEL) (
error_page 404 /index.php;
) else ()

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include $!{nginx}/conf/fastcgi_params;
include $!{nginx}/conf/fastcgi.conf;
}

location ~ /\.(?!well-known).* {
deny all;
}
}
}
njoguamos
njoguamos2y ago
PHP | Nixpacks
App source + Nix packages + Docker = Image
GitHub
nixpacks/nginx.template.conf at main · railwayapp/nixpacks
App source + Nix packages + Docker = Image. Contribute to railwayapp/nixpacks development by creating an account on GitHub.
nilsuria
nilsuriaOP2y ago
Thanks a lot for the support. This is a point in the right direction, but it's still not uploading the file. I figured out myself that the correct file name is nginx.template.conf and not .config, but now I have this error: "POST Content-Length of 9218627 bytes exceeds the limit of 8388608 bytes". I guess this should be modified in the php.ini file but, again, I can't see where I can modify that in Railway. Finally I could change the php.ini configurations adding this code to the web.php file: Route::group(['middleware'=>'custom_middleware'], function () { ini_set('upload_max_filesize', '5000M'); ini_set('post_max_size', '5000M'); }); Nevertheless, it's still not working as now it's showing the following error: " The "" file does not exist or is not readable."
nilsuria
nilsuriaOP2y ago
I see that this could be related to the permissions of the temporary path that nginx is using to store large images as pointed here: https://github.com/laravel/framework/issues/31249
GitHub
The "" file does not exist or is not readable. · Issue #31249 · lar...
Laravel Version: 6.12.0 PHP Version: 7.2.0 Database Driver & Version: 10.1.35-MariaDB Description: I get this error: Symfony\Component\Mime\FileinfoMimeTypeGuesser::guessMimeType:50 vendor/symf...
nilsuria
nilsuriaOP2y ago
But I cant see how to modify the permissions on this path (/var/lib/nginx/tmp/client_body) as pointed in the answers to the post
njoguamos
njoguamos2y ago
Apologies for giving the wrong file name. It is unfortunate that modifying the nginx.template.conf did not work for you. All the best.
nilsuria
nilsuriaOP2y ago
It worked! But it's still something missing there. I guess now the problem is related to the permissions of the temporary path that nginx uses to store the images.
njoguamos
njoguamos2y ago
Are you running Laravel 6?
nilsuria
nilsuriaOP2y ago
No, I'm runing Laravel 9 9.52.7 to be exact
nilsuria
nilsuriaOP2y ago
nilsuria
nilsuriaOP2y ago
After many changes, I see that the code I added to change the value of 'upload_max_filesize' is not really changing the value in php.ini. How I can modify this value in Railway?
njoguamos
njoguamos2y ago
Try deploying your app using docker instead of Nixpacks.
nilsuria
nilsuriaOP2y ago
I've been trying to do so, but I can't get the correct Dockerfile configuration. The template provided by Railway is missing the Dockerfile: https://github.com/railwayapp-templates/laravel
GitHub
GitHub - railwayapp-templates/laravel
Contribute to railwayapp-templates/laravel development by creating an account on GitHub.
Want results from more Discord servers?
Add your server