如何在Railway中修改nginx配置?

huangapple go评论102阅读模式
英文:

How can I modify the nginx configuration in Railway?

问题

我遇到了“413 请求实体过大”错误,当我尝试将图片上传到使用Nixpacks的Railway部署的Laravel应用时。

我看到了其他问题,可以通过修改/etc/nginx/nginx.conf中的“client_max_body_size 20M;”来解决这个问题。

然而,我不知道如何访问Railway的这个配置文件。

我该如何在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've seen other questions 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?

答案1

得分: 0

要使用 Nixpacks 修改 nginx 配置,您可以在应用的根目录中创建一个 nginx.conf 或 nginx.template.conf 文件,并在其中添加您的配置。文件内容如下:

worker_processes 5;
daemon off;

worker_rlimit_nofile 8192;

events {
  worker_connections  4096;  # 默认值: 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; # 对一些虚拟主机似乎是必需的

    client_max_body_size 20M;  # 根据您的需要修改此值

    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;
        }
    }
}

我仍然在处理图片上传方面遇到问题,但这解决了最初的问题。

字体:

英文:

To modify nginx configuration using Nixpacks you can create a nginx.conf or nginx.template.conf file in the root folder of your app and add there your configurations. The content of the file for this case would be:

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;
}
}
}

I'm still experiencing problems with the image upload, but this solve the original question.

Fonts:

https://nixpacks.com/docs/providers/php
https://github.com/railwayapp/nixpacks/blob/main/src/providers/php/nginx.template.conf

huangapple
  • 本文由 发表于 2023年5月10日 16:42:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定