Spring Boot upload file 404

The server is using Nginx. In
Insert picture description here
this case, it is possible to use image.gshop.com to get the file.

Solution:

server{
		listen 80;
		server_name api.gshop.com;
		
		proxy_set_header X-Forworded-Host $host;
		proxy_set_header X-Forworded-Server $host;
		proxy_set_header X-Forworded-For $proxy_add_x_forwarded_for;
		
		location /api/upload {
			proxy_pass http://127.0.0.1:8084;
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
			
			rewrite:"^/api/(.*)" /$1 break;
		}
		
		location / {
			proxy_pass http://127.0.0.1:8082;
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
		}
	}
	server{
		listen 80;
		server_name image.gshop.com;
		
		proxy_set_header X-Forworded-Host $host;
		proxy_set_header X-Forworded-Server $host;
		proxy_set_header X-Forworded-For $proxy_add_x_forwarded_for;
		
		location / {
			root G:\\gshop\\image;
		}
	}
  • First, the mapping path /api/upload, and the next mapping path is /, according to the longest path matching principle, /api/upload has a higher priority. In other words, any path starting with /api/upload will be processed by the first configuration.
  • proxy_pass: Reverse proxy, this time we proxy to port 8084, which is the upload-service service.
  • rewrite: Rewrite path :
    • "^/api/(.*)$" : The regular expression that matches the path, using the grouping syntax, and treats /api/all subsequent parts as a group
    • /$1 : The rewritten target path. Here, $1 is used to refer to the group matched by the previous regular expression (the group number starts from 1), that /api/is, all the following. In this way, the new path is to remove /api/everything except for /apithe purpose of removing the prefix.
    • break : instruction, there are 2 commonly used instructions, namely: last, break
      • last : After the rewrite path is over, the obtained path will be matched again
      • break : After rewriting the path, the path will not be matched again.
        You cannot choose last here, otherwise the new path **/upload/image** will be used to match.
    • Will not be correctly matched to port 8082

After the modification is complete, enter the nginx -s reloadcommand to reload the configuration. Then try uploading again.
Insert picture description hereAfter the modification is completed: an error status code appears in the uploaded file 400. 400 (Bad request) The server does not understand the syntax of the request. It means that the corresponding file type was not found.

 private static final List<String> CONTENT_TYPE = Arrays.asList("image/jpeg", "image/png", "image/gif");

Guess you like

Origin blog.csdn.net/weixin_42789301/article/details/105662215