The nginx reverse proxy service encountered 404 when accessing the resource due to a configuration file error

  • 2020-05-14 06:08:15
  • OfStack

Recently, during the test of the project in hand, a 404 error occurred in the resource accessing the server. This should not be a problem, because it is no problem after the test before. The details are as follows:

1) the company's servers are all nginx reverse agents

2) the access path is a virtual path configured in tomcat

3) the server did disk recovery a few days ago

Of course, if you have encountered this problem, you can refer to 1 if you have not solved it. If you have solved it, you can refer to 1 if there is any problem with my solution.

When this problem occurs, I first consider that there should be a problem with the path, and then modify the virtual path in the configuration file server.xml in tomcat. And then you test it again, again 404;

imgss is the virtual path configured by server.xml, 1444... png this is the image to access; There is no problem with the path ( 但是到nginx那里的时候就有问题,后面会贴出nginx的配置文件)这个是nginx返回的1个404页面(当时没注意到这个是nginx的);

This is strange, when all the files in the server are there, how can you not access them, and then change the modified path back (because this path has been tested before, and there is no problem in accessing) and then access other files in this path,

Found that the access is not a problem, can be normal online preview; After that, it was found that files in jpg, png and other formats could not be accessed, and other files could be accessed normally. Again, the server to access the resources in the server to delete, in addition to jpg, png files, and then re-launch the access, a normal 404;

imgss is a virtual path configured by server.xml, while b.PDF does not exist. So 404 is normal;

Baidu 1 information, did not find a solution;

Check local jdk and jdk on the server again, no problem; Some people say it's a packaging problem, but it's the same problem when I repackage and publish to the server for testing. Finally, think about the nginx configuration file, and then go to nginx configuration file localhost; This is the configuration file path /etc/nginx/ sites-available /localhost

The # comment part is appended after, but not before. This is the configuration file of nginx reverse proxy.

Notice this paragraph: location ~ .*\.(jpg|js|html|mp3|gif|jpeg|png|bmp|swf|ico|css)$ #设定访问静态文件直接读取不经过tomcat

So if you look at this, and you know what the problem is, when you file it in a.jpg /.png format, it maps the path to /var/lib/tomcat7/webapps/ROOT ; There are no resources that you have access to in this path and of course you will return nginx's 404 error page; Then try to delete jpg/png, restart the service, access again, normal; This problem should be the reason why the configuration file of nginx was not modified after disk recovery a few days ago.

The best thing to do, of course, is not to delete.jpg /png; Add this code directly above:


location ^~ /imgss/
    {
      #  root /data/customfiles/Files/;
      #  expires 30d;
      proxy_set_header  Host $host;
      proxy_pass http://127.0.0.1:8080;
    }
  location ^~ /attachments/
    {
      #  root /data/
      #  expires 30d;
      proxy_set_header  Host $host;
      proxy_pass http://127.0.0.1:8080
    }

/imgss/ and /attachments/ are both virtual paths configured in the tomcat profile; nginx will 1 layer down match, match to the appropriate will configure the path map;

server.xml virtual path:


 <Context path="/imgss" docBase="/data/customfiles/Files/" debug="0" reloadable="true"/>
  <Context path="/attachments" docBase="/data/" debug="0" reloadable="true"/>

Such as we want to access path is: http: / / 192.168.1.104, / imgss/(A) / 1444639216522781. png;

Through nginx reverse proxy to: http: / / 192.168.1.104, data customfiles Files/(A) / 1444639216522781. png is the resource in the actual location of the server;

After the modification, access again, the image can be successfully loaded; Of course, I have just come into contact with nginx. If you make any mistake, please kindly advise


Related articles: