Use Nginx to access the picture report 404

Preface: Due to a demand problem, it was necessary to upload the picture to the server and then access it, but my backend uses the SpringSecurity security framework.

The request path is directly intercepted, and the release cannot be released. The request path of the Java internal program is released, and then I use Nginx to access the image

I have been using root to parse before, so I have not been able to access

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-ZFWPOVel-1680057666876) (C:\Users\11\AppData\Roaming\Typora\typora-user-images\ image-20230329102705621.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-fiKCmUMi-1680057666877) (C:\Users\11\AppData\Roaming\Typora\typora-user-images\ image-20230329102731399.png)]

Then use alias to access again

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-LvW4fKUS-1680057666878) (C:\Users\11\AppData\Roaming\Typora\typora-user-images\ image-20230329103013087.png)]

This is because the two are different

# common ground:

Specifies the file path.

# difference:

The URL behind the location is included when root is parsed;

The location does not contain the following url when alias is parsed;

访问图片方式 http://ip:端口/根路径/图片存储文件夹/图片.jpg
# root修改前,这个是访问不了的,会报404,因为这个相当于把images往URL路径后面拼接了,变成/home/mj_admin/images/images/
    然而我们的实际路径是 /home/mj_admin/images/ 所以报404错误
  location /images/ {
    
    
      root  /home/mj_admin/images/;
  }

  root修改后
  location /images/ {
    
    
      root  /home/mj_admin/;
  }

# alias 不会改变路径,直接通过/images/访问/home/mj_admin/images/路径
  location /images/ {
    
    
       alias  /home/mj_admin/images/;
  }

Note: Port problems may also lead to inability to access. In fact, our configuration is correct. There may be some problems with the port. We might as well try another port.

Guess you like

Origin blog.csdn.net/ITKidKid/article/details/129832203