【问题标题】:i cant upload file bigger than 1mb using nginx我无法使用 nginx 上传大于 1mb 的文件
【发布时间】:2021-12-07 18:09:51
【问题描述】:

我使用 spring boot 应用程序作为服务器端,nginx 作为 web 服务器,我尝试上传大于 1 兆字节的文件,但我不能因为 nginx 规则我们阻止了这种情况,但是在添加此命令时找到解决方案@ 987654321@,但是在找到第一个解决方案后我遇到了新问题,现在我不知道当我发送文件大于 1 兆字节的请求时会发生什么,向我提供错误的请求 400 状态代码而没有任何关于此问题的解释 看看我的默认配置:

server
{
  server_name shopbia_shop;
  # return 302 https://$server_name$request_uri;
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server ipv6only=on;

  ssl_certificate /root/ssl/shopbia_shop1.crt;
  ssl_certificate_key /root/ssl/shopbia_shop.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers HIGH:!aNULL:!MD5;
  location /
  {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://localhost:8080;

  }

这里是我的 nginx.confg

    ##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server{
         server_name shopbia_shop;
   # return 302 https://$server_name$request_uri;
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server ipv6only=on;

        ssl_certificate /root/ssl/shopbia_shop1.crt;
        ssl_certificate_key /root/ssl/shopbia_shop.key;
ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;        
 location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                 proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://localhost:8080;

        }

location /auth {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                 proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass https://localhost:9443;

        }



}



server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

       return 302 https://$host$request_uri;
        location /.well-known/pki-validation/88AC11A82DD26892E68223B4504E7514.txt
           {
               alias /var/www/html/88AC11A82DD26892E68223B4504E7514.txt;
            }

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
         proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://localhost:8080;

    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        #server_names_hash_bucket_size 64;
        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
 # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/default;

}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
# 
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

【问题讨论】:

    标签: php ubuntu nginx server devops


    【解决方案1】:

    将 client_max_body_size 添加到您的 http/server/location 块

    server
    {
      server_name shopbia_shop;
      # return 302 https://$server_name$request_uri;
      listen 443 ssl http2 default_server;
      listen [::]:443 ssl http2 default_server ipv6only=on;
    
      ssl_certificate /root/ssl/shopbia_shop1.crt;
      ssl_certificate_key /root/ssl/shopbia_shop.key;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers HIGH:!aNULL:!MD5;
    
      client_max_body_size 1024M;
    
      location /
      {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8080;
    
      }
    }
    

    【讨论】:

    • 我添加了它但没有任何改变
    • 你重启过nginx了吗?
    • 是的,我确实重启了 nginx。
    • 那么您的问题可能不在 nginx 中 - 您的错误也可能来自 springboot。 Maybe this can help。我无法在 springboot 方面提供帮助 - 因为我不知道它是什么:P
    • 当我跨 localhost 发送请求时,我没有这样的错误
    【解决方案2】:

    不是nginx的问题,而是springboot网关服务的问题,网关服务在到达处理服务之前也会中断请求
    只是我在网关服务内的yml文件中添加了这个conig

     spring:
      servlet:
        multipart:
          Max-file-size: 512MB # Single data size
                 Max-Request-Size: 1024MB # total data size
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2014-04-21
      • 2021-01-18
      • 1970-01-01
      • 2019-04-06
      相关资源
      最近更新 更多